/[PAMELA software]/DarthVader/S4Level2/src/S4Core.cpp
ViewVC logotype

Diff of /DarthVader/S4Level2/src/S4Core.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by mocchiut, Fri Nov 17 10:08:10 2006 UTC revision 1.30 by mocchiut, Thu Aug 28 13:43:44 2014 UTC
# Line 1  Line 1 
1  //  //
2    // Given a calibration and a data file this program create an ntuple with LEVEL2 S4 variables
3    //
4    //
5  // C/C++ headers  // C/C++ headers
6  //  //
7  #include <fstream>  #include <fstream>
# Line 29  Line 32 
32  #include <stdlib.h>  #include <stdlib.h>
33  #include <math.h>  #include <math.h>
34  //  //
35    // RunInfo header
36    //
37    #include <RunInfo.h>
38    #include <GLTables.h>
39    //
40  // YODA headers  // YODA headers
41  //  //
42  #include <PamelaRun.h>  #include <PamelaRun.h>
# Line 37  Line 45 
45  #include <EventHeader.h>  #include <EventHeader.h>
46  #include <CalibS4Event.h>  #include <CalibS4Event.h>
47  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
 //#include <yodaUtility.h>  
 //  
 // RunInfo header  
 //  
 #include <RunInfo.h>  
 #include <GLTables.h>  
48  //  //
49  // This program headers  // This program headers
50  //  //
51  #include <S4Level2.h>  #include <S4Level2.h>
52  #include <S4Core.h>  #include <S4Core.h>
53  #include <S4Verl2.h>  #include <S4Verl2.h>
   
 using namespace std;  
   
 //  
54  //  //
55    using namespace std;
56  //  //
57  /*  /*
58   * Fit function Received from Valeria Malvezzi 06/02/2006   * Fit function Received from Valeria Malvezzi 06/02/2006
# Line 72  TArrayD *S4_paramfit(UInt_t atime, TSQLS Line 71  TArrayD *S4_paramfit(UInt_t atime, TSQLS
71    //    //
72    GL_S4_CALIB *glS4calib = new GL_S4_CALIB();    GL_S4_CALIB *glS4calib = new GL_S4_CALIB();
73    //    //
74    if ( !dbc->IsConnected() ) throw -504;    //  if ( !dbc->IsConnected() ) throw -504;
75    glS4calib->Query_GL_S4_CALIB(atime, dbc);    Int_t s4sig = glS4calib->Query_GL_S4_CALIB(atime, dbc);
76      if ( s4sig != -57 && s4sig < 0 ){
77        parametri->AddAt(0.,0);
78        parametri->AddAt(0.,1);
79        return parametri;
80      };
81      if ( s4sig < 0 ) throw s4sig;
82    //    //
83    GL_ROOT *glroot = new GL_ROOT();    GL_ROOT *glroot = new GL_ROOT();
84    if ( !dbc->IsConnected() ) throw -504;    //  if ( !dbc->IsConnected() ) throw -504;
85    glroot->Query_GL_ROOT(glS4calib->ID_ROOT_L0,dbc);    glroot->Query_GL_ROOT(glS4calib->ID_ROOT_L0,dbc);
86    //    //
87    stringstream ftmpname;    stringstream ftmpname;
# Line 100  TArrayD *S4_paramfit(UInt_t atime, TSQLS Line 105  TArrayD *S4_paramfit(UInt_t atime, TSQLS
105    tr->SetBranchAddress("CalibS4", &S4CalibEvent);    tr->SetBranchAddress("CalibS4", &S4CalibEvent);
106    if ( tr->GetEntries() < glS4calib->EV_ROOT ) return(NULL);    if ( tr->GetEntries() < glS4calib->EV_ROOT ) return(NULL);
107    //    //
108    tr->GetEntry(glS4calib->EV_ROOT);    if ( tr->GetEntry(glS4calib->EV_ROOT) <= 0 ) throw -36;
109    //    //
110    //----------- variable initialization -------------------------------------------------    // Variables initialization
111    //    //
112    Double_t mip[3]={1, 30, 300};    Double_t mip[3]={1, 30, 300};
113    Double_t adc[3] = {0.,0.,0.};    Double_t adc[3] = {0.,0.,0.};
114    //    //
115    //------------ Fit calibrations and find parameters to calibrate data ------------------    // Fit calibrations and find parameters to calibrate data
116    //    //
117    pamela::S4::S4Event  *s4Record = 0;    pamela::S4::S4Event  *s4Record = 0;
118    //    //
# Line 136  TArrayD *S4_paramfit(UInt_t atime, TSQLS Line 141  TArrayD *S4_paramfit(UInt_t atime, TSQLS
141    adc[2]=adc[2]/128;      adc[2]=adc[2]/128;  
142    //  if ( IsDebug() ) printf(" adc1 = %g adc2 = %g adc3 = %g\n",adc[0],adc[1],adc[2]);    //  if ( IsDebug() ) printf(" adc1 = %g adc2 = %g adc3 = %g\n",adc[0],adc[1],adc[2]);
143    TGraph *fitpar = new TGraph (3, adc, mip);    TGraph *fitpar = new TGraph (3, adc, mip);
144    TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // definizione della funzione, 2 = num. parametri    TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // function definition with 2 parameters
   //  TF1 *func = new TF1("fitf", fitf, -0., 1000., 2); // definizione della funzione, 2 = num. parametri  
145    //      //  
146    func->SetParameters(0.3,1.);        //inizializzazione dei parametri a 1    func->SetParameters(0.3,1.); // parameters initialization to 1
147    func->SetParNames("m","q");      //definisce il nome dei parametri    func->SetParNames("m","q"); //parameter's name
148    fitpar->Fit(func,"qr");          //fitta fitpar con la funzione func nel range definito nella funzione    fitpar->Fit(func,"qr"); //function fit
   //fitpar->Fit(func,"r");          //fitta fitpar con la funzione func nel range definito nella funzione  
149    parametri->AddAt(func->GetParameter(0),0);    parametri->AddAt(func->GetParameter(0),0);
150    parametri->AddAt(func->GetParameter(1),1);    parametri->AddAt(func->GetParameter(1),1);
151    //  if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.7 || parametri[1] > 1.1 ) valid = 0;    //  if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.7 || parametri[1] > 1.1 ) valid = 0;
# Line 160  TArrayD *S4_paramfit(UInt_t atime, TSQLS Line 163  TArrayD *S4_paramfit(UInt_t atime, TSQLS
163  // CORE ROUTINE  // CORE ROUTINE
164  //  //
165  //  //
166  int S4Core(UInt_t run, TFile *file, TSQLServer *dbc, Int_t S4argc, char *S4argv[]){  int S4Core(UInt_t run, TFile *file,GL_TABLES *glt, Int_t S4argc, char *S4argv[]){
   Int_t i = 0;  
167    //    //
168    TString processFolder = "S4Folder";    // Set these to true to have a verbose output.
169    //    //
170    Bool_t debug = false;    Bool_t debug = false;
   //  
171    Bool_t verbose = false;    Bool_t verbose = false;
172    //        //    
173      //
174      // Output directory is the working directoy.
175      //
176      const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
177      //
178      Int_t i = 0;
179      TString processFolder = Form("S4Folder_%u",run);
180    if ( S4argc > 0 ){    if ( S4argc > 0 ){
181      i = 0;      i = 0;
182      while ( i < S4argc ){      while ( i < S4argc ){
# Line 181  int S4Core(UInt_t run, TFile *file, TSQL Line 189  int S4Core(UInt_t run, TFile *file, TSQL
189        };        };
190        if ( (!strcmp(S4argv[i],"--debug")) || (!strcmp(S4argv[i],"-g"))) {        if ( (!strcmp(S4argv[i],"--debug")) || (!strcmp(S4argv[i],"-g"))) {
191          verbose = true;          verbose = true;
192            debug = true;
193        };        };
194        if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) {        if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) {
195          verbose = true;          verbose = true;
# Line 188  int S4Core(UInt_t run, TFile *file, TSQL Line 197  int S4Core(UInt_t run, TFile *file, TSQL
197        i++;        i++;
198      };      };
199    };    };
   //  
   const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));  
   //  
200    // Variables for level2    // Variables for level2
201    //    //
202    TTree *S4tr = 0;    TTree *S4tr = 0;
203    UInt_t nevents = 0;    UInt_t nevents = 0;
204    //    //
205    // variables needed to reprocess data    // Variables needed to reprocess data
206      //
207      Long64_t maxsize = 10000000000LL;  
208      TTree::SetMaxTreeSize(maxsize);
209    //    //
210    TString S4version;    TString S4version;
211    ItoRunInfo *runinfo = 0;    ItoRunInfo *runinfo = 0;
# Line 258  int S4Core(UInt_t run, TFile *file, TSQL Line 267  int S4Core(UInt_t run, TFile *file, TSQL
267    TTree *tempS4 = 0;    TTree *tempS4 = 0;
268    stringstream tempname;    stringstream tempname;
269    stringstream S4folder;    stringstream S4folder;
270      Bool_t myfold = false;
271    tempname.str("");    tempname.str("");
272    tempname << outDir;    tempname << outDir;
273    tempname << "/" << processFolder.Data();    tempname << "/" << processFolder.Data();
274    S4folder.str("");    S4folder.str("");
275    S4folder << tempname.str().c_str();    S4folder << tempname.str().c_str();
   gSystem->MakeDirectory(S4folder.str().c_str());  
276    tempname << "/S4tree_run";    tempname << "/S4tree_run";
277    tempname << run << ".root";      tempname << run << ".root";  
278      UInt_t totnorun = 0;
279    //    //
280    // DB classes    // DB classes
281    //    //
# Line 322  int S4Core(UInt_t run, TFile *file, TSQL Line 332  int S4Core(UInt_t run, TFile *file, TSQL
332    //    //
333    // number of run to be processed    // number of run to be processed
334    //    //
335    numbofrun = runinfo->GetNoRun();    numbofrun = runinfo->GetNoRun();      
336      totnorun = runinfo->GetRunEntries();
337    //    //
338    // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run    // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run
339    //    //
# Line 343  int S4Core(UInt_t run, TFile *file, TSQL Line 354  int S4Core(UInt_t run, TFile *file, TSQL
354      //      //
355      // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?      // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
356      //      //
357        S4trclone->SetAutoSave(900000000000000LL);
358      reproc = true;      reproc = true;
359      //      //
360      // update versioning information      // update versioning information
361      //      //
362      if (verbose) printf("\n Preparing the pre-processing...\n");      if (verbose) printf("\n Preparing the pre-processing...\n");
363      //      //
364      if ( run == 0 ){      if ( run == 0 || totnorun == 1 ){
365        //        //
366        // we are reprocessing all the file        // we are reprocessing all the file
367        // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately        // if we are reprocessing everything we don't need to copy any old event and we can just work with the new tree and delete the old one immediately
# Line 368  int S4Core(UInt_t run, TFile *file, TSQL Line 380  int S4Core(UInt_t run, TFile *file, TSQL
380        //        //
381        // copying old tree to a new file        // copying old tree to a new file
382        //        //
383          gSystem->MakeDirectory(S4folder.str().c_str());
384          myfold = true;
385        tempfile = new TFile(tempname.str().c_str(),"RECREATE");        tempfile = new TFile(tempname.str().c_str(),"RECREATE");
386        tempS4 = S4trclone->CloneTree(-1,"fast");        tempS4 = S4trclone->CloneTree(-1,"fast");
387        tempS4->SetName("S4-old");        tempS4->SetName("S4-old");
# Line 387  int S4Core(UInt_t run, TFile *file, TSQL Line 401  int S4Core(UInt_t run, TFile *file, TSQL
401    //    //
402    file->cd();    file->cd();
403    S4tr = new TTree("S4-new","PAMELA Level2 S4 data");    S4tr = new TTree("S4-new","PAMELA Level2 S4 data");
404      S4tr->SetAutoSave(900000000000000LL);
405    S4tr->Branch("S4Level2","S4Level2",&s4);    S4tr->Branch("S4Level2","S4Level2",&s4);
406    //    //
407    if ( reproc && !reprocall ){    if ( reproc && !reprocall ){
# Line 395  int S4Core(UInt_t run, TFile *file, TSQL Line 410  int S4Core(UInt_t run, TFile *file, TSQL
410      //      //
411      tempfile = new TFile(tempname.str().c_str(),"READ");      tempfile = new TFile(tempname.str().c_str(),"READ");
412      S4trclone = (TTree*)tempfile->Get("S4-old");      S4trclone = (TTree*)tempfile->Get("S4-old");
413        S4trclone->SetAutoSave(900000000000000LL);
414      S4trclone->SetBranchAddress("S4Level2",&s4clone);      S4trclone->SetBranchAddress("S4Level2",&s4clone);
415      //            //      
416      if ( nobefrun > 0 ){      if ( nobefrun > 0 ){
# Line 405  int S4Core(UInt_t run, TFile *file, TSQL Line 421  int S4Core(UInt_t run, TFile *file, TSQL
421        }        }
422        for (UInt_t j = 0; j < nobefrun; j++){        for (UInt_t j = 0; j < nobefrun; j++){
423          //          //
424          S4trclone->GetEntry(j);            if ( S4trclone->GetEntry(j) <= 0 ) throw -36;    
425          //          //
426          // copy s4clone to mydec          // copy s4clone to mydec
427          //          //
428          s4->Clear();          //      s4->Clear();
429          //          //
430          memcpy(&s4,&s4clone,sizeof(s4clone));          memcpy(&s4,&s4clone,sizeof(s4clone));
431          //          //
# Line 463  int S4Core(UInt_t run, TFile *file, TSQL Line 479  int S4Core(UInt_t run, TFile *file, TSQL
479      //      //
480      // prepare the timesync for the db      // prepare the timesync for the db
481      //      //
482        TString host = glt->CGetHost();
483        TString user = glt->CGetUser();
484        TString psw = glt->CGetPsw();
485        TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());    
486      if ( !dbc->IsConnected() ) throw -504;      if ( !dbc->IsConnected() ) throw -504;
487        stringstream myquery;
488        myquery.str("");
489        myquery << "SET time_zone='+0:00'";
490        delete dbc->Query(myquery.str().c_str());
491      dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);      dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
492      //      //
493      // Search in the DB the path and name of the LEVEL0 file to be processed.      // Search in the DB the path and name of the LEVEL0 file to be processed.
494      //      //
495      if ( !dbc->IsConnected() ) throw -504;      //    if ( !dbc->IsConnected() ) throw -504;
496      glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);      glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
497      //      //
498      ftmpname.str("");      ftmpname.str("");
# Line 486  int S4Core(UInt_t run, TFile *file, TSQL Line 510  int S4Core(UInt_t run, TFile *file, TSQL
510        printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent));        printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent));
511      }      }
512      //      //
513        //    if ( !totevent ) goto closeandexit;
514        //
515      // Open Level0 file      // Open Level0 file
516      //      //
517        if ( l0File ) l0File->Close();
518      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
519      if ( !l0File ) {      if ( !l0File ) {
520        if ( debug ) printf(" S4 - ERROR: problems opening  Level0 file\n");        if ( debug ) printf(" S4 - ERROR: problems opening  Level0 file\n");
# Line 522  int S4Core(UInt_t run, TFile *file, TSQL Line 549  int S4Core(UInt_t run, TFile *file, TSQL
549      //      //
550      nevents = l0S4->GetEntries();      nevents = l0S4->GetEntries();
551      //      //
552      if ( nevents < 1 ) {      if ( nevents < 1 && totevent ) {
553        if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");        if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");
554        l0File->Close();        l0File->Close();
555        code = -11;        code = -11;
556        goto closeandexit;        goto closeandexit;
557      };      };
558      //      //
559      if ( runinfo->EV_TO > nevents-1 ) {      if ( runinfo->EV_TO > nevents-1 && totevent ) {
560        if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");        if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");
561        l0File->Close();        l0File->Close();
562        code = -12;        code = -12;
# Line 553  int S4Core(UInt_t run, TFile *file, TSQL Line 580  int S4Core(UInt_t run, TFile *file, TSQL
580      //      //
581      if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");      if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
582      //      //
583        if ( dbc ){
584          dbc->Close();
585          delete dbc;
586          dbc = 0;
587        };
588        //
589      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){      for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
590        //        //
591        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
592        //        //
593        l0head->GetEntry(re);        if ( l0head->GetEntry(re) <= 0 ) throw -36;
594        //        //
595        // absolute time of this event        // absolute time of this event
596        //        //
# Line 566  int S4Core(UInt_t run, TFile *file, TSQL Line 599  int S4Core(UInt_t run, TFile *file, TSQL
599        //        //
600        // paranoid check        // paranoid check
601        //        //
602        if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME)  ) {        if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1))  ) {
603          if (verbose) printf(" S4 - WARNING: event at time outside the run time window, skipping it\n");          if (verbose) printf(" S4 - WARNING: event at time outside the run time window, skipping it\n");
604          goto jumpev;          goto jumpev;
605        };        };
# Line 576  int S4Core(UInt_t run, TFile *file, TSQL Line 609  int S4Core(UInt_t run, TFile *file, TSQL
609        // start processing        // start processing
610        //        //
611        s4->Clear();        s4->Clear();
612        l0S4->GetEntry(re);        if ( l0S4->GetEntry(re) <= 0 ) throw -36;
613        if (l0s4e->unpackError == 0){        if (l0s4e->unpackError == 0){
614          s4->S4adc = l0s4e->S4_DATA;          s4->S4adc = l0s4e->S4_DATA;
615          //          //
# Line 621  int S4Core(UInt_t run, TFile *file, TSQL Line 654  int S4Core(UInt_t run, TFile *file, TSQL
654          //          //
655          // Get entry from old tree          // Get entry from old tree
656          //          //
657          S4trclone->GetEntry(j);            if ( S4trclone->GetEntry(j) <= 0 ) throw -36;    
658          //          //
659          // copy s4clone to s4          // copy s4clone to s4
660          //          //
661          //      s4 = new S4Level2();          //      s4 = new S4Level2();
662          s4->Clear();          //      s4->Clear();
663          memcpy(&s4,&s4clone,sizeof(s4clone));          memcpy(&s4,&s4clone,sizeof(s4clone));
664          //          //
665          // Fill entry in the new tree          // Fill entry in the new tree
# Line 641  int S4Core(UInt_t run, TFile *file, TSQL Line 674  int S4Core(UInt_t run, TFile *file, TSQL
674    //    //
675    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
676    if ( tempfile ) tempfile->Close();                if ( tempfile ) tempfile->Close();            
677    gSystem->Unlink(tempname.str().c_str());    if ( myfold) gSystem->Unlink(tempname.str().c_str());
678    //    //
679    if ( runinfo ) runinfo->Close();        //runinfo->Write();
680    if ( S4tr ) S4tr->SetName("S4");        if ( S4tr ) S4tr->SetName("S4");    
681    if ( file ){    if ( file ){
682      file->cd();      file->cd();
683      file->Write();      if ( S4tr ) S4tr->Write("S4", TObject::kOverwrite); // 10RED bug fixed
684        //file->Write();
685    };    };
686    //    //
687    gSystem->Unlink(S4folder.str().c_str());    if ( myfold ) gSystem->Unlink(S4folder.str().c_str());
688    //    //
689    // the end    // the end
690    //    //
691    if (verbose) printf("\n Exiting...\n");    if (verbose) printf("\n Exiting...\n");
   if ( S4tr ) S4tr->Delete();  
692    //    //
   if ( s4 ) delete s4;  
   if ( s4clone ) delete s4clone;  
693    if ( glroot ) delete glroot;    if ( glroot ) delete glroot;
694    if ( runinfo ) delete runinfo;    if ( runinfo ) runinfo->Close();    
695      if ( runinfo ) delete runinfo;
696    //    //
697    if(code < 0)  throw code;    if(code < 0)  throw code;
698    return(code);    return(code);

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.30

  ViewVC Help
Powered by ViewVC 1.1.23