/[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.1 by mocchiut, Fri May 19 13:15:26 2006 UTC revision 1.27 by mocchiut, Tue May 15 14:31:27 2012 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 9  Line 12 
12  //  //
13  // ROOT headers  // ROOT headers
14  //  //
15    #include <TGraph.h>
16    #include <TF1.h>
17  #include <TTree.h>  #include <TTree.h>
18  #include <TClassEdit.h>  #include <TClassEdit.h>
19  #include <TObject.h>  #include <TObject.h>
20  #include <TList.h>  #include <TList.h>
21  #include <TArrayL.h>  #include <TArrayI.h>
22    #include <TArrayD.h>
23  #include <TSystem.h>  #include <TSystem.h>
24  #include <TSystemDirectory.h>  #include <TSystemDirectory.h>
25  #include <TString.h>  #include <TString.h>
# Line 26  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>
 #include <RegistryEvent.h>  
43  #include <PscuHeader.h>  #include <PscuHeader.h>
44  #include <PscuEvent.h>  #include <PscuEvent.h>
45  #include <EventHeader.h>  #include <EventHeader.h>
 #include <RegistryEvent.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>
54    //
55  using namespace std;  using namespace std;
56    //
57    /*
58     * Fit function Received from Valeria Malvezzi 06/02/2006
59     */
60    Double_t fitf(Double_t *x, Double_t *par){  
61      Double_t fitval =(par[0]*x[0])+par[1];
62      return fitval;
63    }
64    
65    /*
66     * Fit the S4 calibration with a straight line - Received from Valeria Malvezzi 06/02/2006
67     */
68    TArrayD *S4_paramfit(UInt_t atime, TSQLServer *dbc){      
69      //  
70      TArrayD *parametri = new TArrayD(2);
71      //
72      GL_S4_CALIB *glS4calib = new GL_S4_CALIB();
73      //
74      //  if ( !dbc->IsConnected() ) throw -504;
75      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();
84      //  if ( !dbc->IsConnected() ) throw -504;
85      glroot->Query_GL_ROOT(glS4calib->ID_ROOT_L0,dbc);
86      //
87      stringstream ftmpname;
88      ftmpname.str("");
89      ftmpname << glroot->PATH.Data() << "/";
90      ftmpname << glroot->NAME.Data();
91      //
92      TFile *file = 0;
93      file = new TFile(ftmpname.str().c_str());
94      //
95      if ( !file ) return(NULL);
96      //
97      TTree *tr = 0;
98      tr = (TTree*)file->Get("CalibS4");
99      if ( !tr ){
100        file->Close();
101        return(NULL);
102      };
103      //
104      pamela::CalibS4Event *S4CalibEvent = 0;
105      tr->SetBranchAddress("CalibS4", &S4CalibEvent);
106      if ( tr->GetEntries() < glS4calib->EV_ROOT ) return(NULL);
107      //
108      if ( tr->GetEntry(glS4calib->EV_ROOT) <= 0 ) throw -36;
109      //
110      // Variables initialization
111      //
112      Double_t mip[3]={1, 30, 300};
113      Double_t adc[3] = {0.,0.,0.};
114      //
115      // Fit calibrations and find parameters to calibrate data
116      //
117      pamela::S4::S4Event  *s4Record = 0;
118      //
119      for (Int_t j = 0; j < 4; j++){
120        for (Int_t i = 0; i < 128; i++){
121          s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i));
122          switch (j) {
123          case 0 :{
124            adc[0]=adc[0]+((s4Record->S4_DATA)-32);
125            break;
126          };
127          case 1 :{
128            adc[1]=adc[1]+((s4Record->S4_DATA)-32);
129            break;
130          };
131          case 3 :{
132            adc[2]=adc[2]+((s4Record->S4_DATA)-32);
133            break;
134          };
135          };
136        };
137      };
138      //
139      adc[0]=adc[0]/128;
140      adc[1]=adc[1]/128;
141      adc[2]=adc[2]/128;  
142      //  if ( IsDebug() ) printf(" adc1 = %g adc2 = %g adc3 = %g\n",adc[0],adc[1],adc[2]);
143      TGraph *fitpar = new TGraph (3, adc, mip);
144      TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // function definition with 2 parameters
145      //  
146      func->SetParameters(0.3,1.); // parameters initialization to 1
147      func->SetParNames("m","q"); //parameter's name
148      fitpar->Fit(func,"qr"); //function fit
149      parametri->AddAt(func->GetParameter(0),0);
150      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;
152      //
153      delete glroot;
154      delete glS4calib;
155      delete fitpar;
156      delete func;
157      file->Close();
158      //
159      return parametri;
160    };
161    
162  //  //
163  // CORE ROUTINE  // CORE ROUTINE
164  //  //
165  //  //
166    int S4Core(UInt_t run, TFile *file,GL_TABLES *glt, Int_t S4argc, char *S4argv[]){
 int S4Core(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t S4argc, char *S4argv[]){  
   Int_t i = 0;  
   //  
   TString processFolder = "S4Folder";  
167    //    //
168    // Set these to true to have a very verbose output.    // Set these to true to have a verbose output.
       Bool_t debug = false;  
169    //    //
170        Bool_t verbose = false;    Bool_t debug = false;
171      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 78  int S4Core(ULong64_t run, TFile *file, T Line 189  int S4Core(ULong64_t run, TFile *file, T
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 85  int S4Core(ULong64_t run, TFile *file, T Line 197  int S4Core(ULong64_t run, TFile *file, T
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    Long64_t nevents = 0LL;    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;
212    TArrayL *runlist = 0;    TArrayI *runlist = 0;
213    TTree *S4trclone = 0;    TTree *S4trclone = 0;
214    Bool_t reproc = false;    Bool_t reproc = false;
215    Bool_t reprocall = false;    Bool_t reprocall = false;
# Line 105  int S4Core(ULong64_t run, TFile *file, T Line 218  int S4Core(ULong64_t run, TFile *file, T
218    UInt_t numbofrun = 0;    UInt_t numbofrun = 0;
219    stringstream ftmpname;    stringstream ftmpname;
220    TString fname;    TString fname;
221    Long64_t totfileentries = 0ULL;    UInt_t totfileentries = 0;
222    Long64_t idRun = 0LL;    UInt_t idRun = 0;
223    Double_t  ParamFit0 =  0ULL;    Double_t  ParamFit0 =  0.;
224    Double_t  ParamFit1 =  0ULL;    Double_t  ParamFit1 =  0.;
225    //    //
226    // variables needed to handle error signals    // variables needed to handle error signals
227    //    //
# Line 125  int S4Core(ULong64_t run, TFile *file, T Line 238  int S4Core(ULong64_t run, TFile *file, T
238    TFile *l0File = 0;    TFile *l0File = 0;
239    TTree *l0tr = 0;    TTree *l0tr = 0;
240    TBranch *l0head = 0;    TBranch *l0head = 0;
   TBranch *l0registry = 0;  
241    TBranch *l0S4 =0;    TBranch *l0S4 =0;
   pamela::RegistryEvent *l0reg=0;  
242    pamela::S4::S4Event  *l0s4e = 0;    pamela::S4::S4Event  *l0s4e = 0;
243    pamela::EventHeader *eh = 0;    pamela::EventHeader *eh = 0;
244      pamela::PscuHeader *ph = 0;
245    //    //
246    // Define other basic variables    // Define other basic variables
247    //    //
# Line 138  int S4Core(ULong64_t run, TFile *file, T Line 250  int S4Core(ULong64_t run, TFile *file, T
250    stringstream file3;    stringstream file3;
251    stringstream qy;    stringstream qy;
252    Int_t totevent = 0;    Int_t totevent = 0;
253    ULong64_t atime = 0ULL;    UInt_t atime = 0;
254    Int_t ei = 0;    //  Int_t ei = 0;
255    Int_t re = 0;    UInt_t re = 0;
256    //    //
257    // Working filename    // Working filename
258    //    //
# Line 155  int S4Core(ULong64_t run, TFile *file, T Line 267  int S4Core(ULong64_t run, TFile *file, T
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    //    //
282    GL_ROOT *glroot = new GL_ROOT();    GL_ROOT *glroot = new GL_ROOT();
283    GL_S4_CALIB *glS4calib = new GL_S4_CALIB();    GL_TIMESYNC *dbtime = 0;
284    //    //
285    // Let's start!    // Let's start!
286    //    //
# Line 175  int S4Core(ULong64_t run, TFile *file, T Line 288  int S4Core(ULong64_t run, TFile *file, T
288    // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file    // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file
289    // if it exists we are reprocessing data and we must delete that entries, if not we must create it.    // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
290    //    //
291    if ( run == 0ULL )  reproc = true;    if ( run == 0 )  reproc = true;
292    //    //
293    //    //
294    // Output file is "outputfile"    // Output file is "outputfile"
# Line 219  int S4Core(ULong64_t run, TFile *file, T Line 332  int S4Core(ULong64_t run, TFile *file, T
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 230  int S4Core(ULong64_t run, TFile *file, T Line 344  int S4Core(ULong64_t run, TFile *file, T
344      // tree does not exist, we are not reprocessing      // tree does not exist, we are not reprocessing
345      //      //
346      reproc = false;      reproc = false;
347      if ( run == 0ULL ){      if ( run == 0 ){
348        if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n");        if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n");
349      }      }
350      if ( runinfo->IsReprocessing() && run != 0ULL ) {      if ( runinfo->IsReprocessing() && run != 0 ) {
351        if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n");        if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n");
352      }      }
353    } else {    } else {
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      //      //
     //sgnl = runinfo->Update(run, "S4",S4version);  
362      if (verbose) printf("\n Preparing the pre-processing...\n");      if (verbose) printf("\n Preparing the pre-processing...\n");
363      //      //
364      if ( run == 0ULL ){      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 262  int S4Core(ULong64_t run, TFile *file, T Line 376  int S4Core(ULong64_t run, TFile *file, T
376        //        //
377        reprocall = false;        reprocall = false;
378        //        //
379        if (verbose) printf("\n S4 - WARNING: Reprocessing run number %llu \n",run);        if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run);
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 285  int S4Core(ULong64_t run, TFile *file, T Line 401  int S4Core(ULong64_t run, TFile *file, T
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 293  int S4Core(ULong64_t run, TFile *file, T Line 410  int S4Core(ULong64_t run, TFile *file, T
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 ){
417        if (verbose){        if (verbose){
418          printf("\n Pre-processing: copying events from the old tree before the processed run\n");            printf("\n Pre-processing: copying events from the old tree before the processed run\n");  
419          printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run);          printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
420          printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);          printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
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 = new S4Level2();          s4->Clear();
429            //
430          memcpy(&s4,&s4clone,sizeof(s4clone));          memcpy(&s4,&s4clone,sizeof(s4clone));
431          //          //
432          // Fill entry in the new tree          // Fill entry in the new tree
# Line 332  int S4Core(ULong64_t run, TFile *file, T Line 451  int S4Core(ULong64_t run, TFile *file, T
451      idRun = runlist->At(irun);      idRun = runlist->At(irun);
452      if (verbose){      if (verbose){
453        printf("\n\n\n ####################################################################### \n");        printf("\n\n\n ####################################################################### \n");
454        printf("                    PROCESSING RUN NUMBER %i \n",(int)idRun);        printf("                    PROCESSING RUN NUMBER %u \n",idRun);
455        printf(" ####################################################################### \n\n\n");        printf(" ####################################################################### \n\n\n");
456      }      }
457      //      //
458      runinfo->ID_REG_RUN = 0ULL;      runinfo->ID_ROOT_L0 = 0;
459      //      //
460      // store in the runinfo class the GL_RUN variables for our run      // store in the runinfo class the GL_RUN variables for our run
461      //      //
462      sgnl = 0;      sgnl = 0;
463      sgnl = runinfo->GetRunInfo(idRun);      sgnl = runinfo->GetRunInfo(idRun);
464      if ( sgnl ){      if ( sgnl ){
465        //printf(" S4 - ERROR: RunInfo exited with non-zero status\n");        if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
466        code = sgnl;        code = sgnl;
467        goto closeandexit;        goto closeandexit;
468      } else {      } else {
469        sgnl = 0;        sgnl = 0;
470      };      };
471      //      //
472      // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN      // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
473      //      //
474      if ( runinfo->ID_REG_RUN == 0 ){      if ( runinfo->ID_ROOT_L0 == 0 ){
475        //printf("\n S4 - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);        if ( debug ) printf("\n S4 - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
476        code = -5;        code = -5;
477        goto closeandexit;            goto closeandexit;    
478      };      };
479      //      //
480        // 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;
487        stringstream myquery;
488        myquery.str("");
489        myquery << "SET time_zone='+0:00'";
490        dbc->Query(myquery.str().c_str());
491        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      glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);      //    if ( !dbc->IsConnected() ) throw -504;
496        glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
497      //      //
498      ftmpname.str("");      ftmpname.str("");
499      ftmpname << glroot->PATH.Data() << "/";      ftmpname << glroot->PATH.Data() << "/";
# Line 369  int S4Core(ULong64_t run, TFile *file, T Line 502  int S4Core(ULong64_t run, TFile *file, T
502      //      //
503      // print out informations      // print out informations
504      //      //
505      totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;      totevent = runinfo->NEVENTS;
506      if (verbose){      if (verbose){
507        printf("\n LEVEL0 data file: %s \n",fname.Data());        printf("\n LEVEL0 data file: %s \n",fname.Data());
508        printf(" RUN HEADER absolute time is:  %llu \n",runinfo->RUNHEADER_TIME);        printf(" RUN HEADER absolute time is:  %u \n",runinfo->RUNHEADER_TIME);
509        printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);        printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
510        printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,runinfo->EV_REG_PHYS_FROM,runinfo->EV_REG_PHYS_TO);        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      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
518      if ( !l0File ) {      if ( !l0File ) {
519        //printf(" S4 - ERROR: problems opening  Level0 file\n");        if ( debug ) printf(" S4 - ERROR: problems opening  Level0 file\n");
520        code = -6;        code = -6;
521        goto closeandexit;        goto closeandexit;
522      };      };
523        //
524      l0tr = (TTree*)l0File->Get("Physics");      l0tr = (TTree*)l0File->Get("Physics");
525      if ( !l0tr ) {      if ( !l0tr ) {
526        //printf(" S4 - ERROR: no Physics tree in Level0 file\n");        if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n");
527        l0File->Close();        l0File->Close();
528        code = -7;        code = -7;
529        goto closeandexit;        goto closeandexit;
530      };      };
531      l0head = l0tr->GetBranch("Header");      l0head = l0tr->GetBranch("Header");
532      if ( !l0head ) {      if ( !l0head ) {
533        //printf(" S4 - ERROR: no Header branch in Level0 tree\n");        if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n");
534        l0File->Close();        l0File->Close();
535        code = -8;        code = -8;
536        goto closeandexit;            goto closeandexit;    
537      };      };
     l0registry = l0tr->GetBranch("Registry");  
     if ( !l0registry ) {  
       //printf(" S4 - ERROR: no Registry branch in Level0 tree\n");  
       l0File->Close();  
       code = -9;  
       goto closeandexit;      
     };  
538      l0S4 = l0tr->GetBranch("S4");      l0S4 = l0tr->GetBranch("S4");
539      if ( !l0S4 ) {      if ( !l0S4 ) {
540        //printf(" S4 - ERROR: no S4 branch in Level0 tree\n");        if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n");
541        l0File->Close();        l0File->Close();
542        code = -503;        code = -503;
543        goto closeandexit;        goto closeandexit;
544      };      };
   
545      //      //
     l0tr->SetBranchAddress("Registry", &l0reg);  
546      l0tr->SetBranchAddress("S4", &l0s4e);      l0tr->SetBranchAddress("S4", &l0s4e);
547      l0tr->SetBranchAddress("Header", &eh);      l0tr->SetBranchAddress("Header", &eh);
548      //      //
549      nevents = l0registry->GetEntries();      nevents = l0S4->GetEntries();
550      //      //
551      if ( nevents < 1 ) {      if ( nevents < 1 && totevent ) {
552        //printf(" S4 - ERROR: Level0 file is empty\n\n");        if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");
553        l0File->Close();        l0File->Close();
554        code = -11;        code = -11;
555        goto closeandexit;        goto closeandexit;
556      };      };
557      //      //
558      if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {      if ( runinfo->EV_TO > nevents-1 && totevent ) {
559        //printf(" S4 - ERROR: too few entries in the registry tree\n");        if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");
560        l0File->Close();        l0File->Close();
561        code = -12;        code = -12;
562        goto closeandexit;        goto closeandexit;
# Line 438  int S4Core(ULong64_t run, TFile *file, T Line 565  int S4Core(ULong64_t run, TFile *file, T
565      // Check if we have to load parameter files (or calibration associated to runs and not to events)      // Check if we have to load parameter files (or calibration associated to runs and not to events)
566      // second query: which is the value of paramfit relative to the calibration?      // second query: which is the value of paramfit relative to the calibration?
567      //      //
568      glS4calib->Query_GL_S4_CALIB(runinfo->RUNHEADER_TIME, dbc);      TArrayD *params = 0;
569      ParamFit0 = glS4calib->PARAM_FIT0;      params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc);
570      ParamFit1 = glS4calib->PARAM_FIT1;      if ( !params ){
571          code = -13;
572          goto closeandexit;
573        };
574        //
575        ParamFit0 = params->At(0);
576        ParamFit1 = params->At(1);
577      //      //
578      // run over all the events of the run      // run over all the events of the run
579      //      //
580      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");
581      //      //
582      for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){      if ( dbc ){
583          dbc->Close();
584          delete dbc;
585          dbc = 0;
586        };
587        //
588        for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
589        //        //
590        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
591        //        //
592        l0registry->GetEntry(re);        if ( l0head->GetEntry(re) <= 0 ) throw -36;
593        //        //
594        // absolute time of this event        // absolute time of this event
595        //        //
596        atime = l0reg->absTime;        ph = eh->GetPscuHeader();
597        //        atime = dbtime->DBabsTime(ph->GetOrbitalTime());  
       // physics events is at entry number ei where  
       //  
       ei = l0reg->event;  
598        //        //
599        // paranoid check        // paranoid check
600        //        //
601        if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME)  ) {        if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1))  ) {
602          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");
603          goto jumpev;          goto jumpev;
604        };        };
# Line 471  int S4Core(ULong64_t run, TFile *file, T Line 607  int S4Core(ULong64_t run, TFile *file, T
607        //        //
608        // start processing        // start processing
609        //        //
610        s4 = new S4Level2();        s4->Clear();
611        l0S4->GetEntry(ei);        if ( l0S4->GetEntry(re) <= 0 ) throw -36;
612        if (l0s4e->unpackError == 1)  continue;              if (l0s4e->unpackError == 0){
613        s4->S4adc = l0s4e->S4_DATA;          s4->S4adc = l0s4e->S4_DATA;
614        if ((l0s4e->S4_DATA) > 31 ){          //
615          s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;          if ((l0s4e->S4_DATA) > 31 ){
616        }else{            s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;
617          s4->S4calibrated = 0;          }else{
618        }            s4->S4calibrated = 0;
619            }
620          };
621          //
622          s4->unpackError = l0s4e->unpackError;
623          //
624        S4tr->Fill();        S4tr->Fill();
625        //            //    
626        //        //
# Line 490  int S4Core(ULong64_t run, TFile *file, T Line 631  int S4Core(ULong64_t run, TFile *file, T
631      //      //
632      // Here you may want to clear some variables before processing another run        // Here you may want to clear some variables before processing another run  
633      //      //
634      ei = 0;      delete dbtime;
635        if ( params ) delete params;
636        //
637    }; // process all the runs    }; // process all the runs
638    //    //
639    if (verbose) printf("\n Finished processing data \n");    if (verbose) printf("\n Finished processing data \n");
# Line 510  int S4Core(ULong64_t run, TFile *file, T Line 653  int S4Core(ULong64_t run, TFile *file, T
653          //          //
654          // Get entry from old tree          // Get entry from old tree
655          //          //
656          S4trclone->GetEntry(j);            if ( S4trclone->GetEntry(j) <= 0 ) throw -36;    
657          //          //
658          // copy s4clone to s4          // copy s4clone to s4
659          //          //
660          s4 = new S4Level2();          //      s4 = new S4Level2();
661            s4->Clear();
662          memcpy(&s4,&s4clone,sizeof(s4clone));          memcpy(&s4,&s4clone,sizeof(s4clone));
663          //          //
664          // Fill entry in the new tree          // Fill entry in the new tree
# Line 529  int S4Core(ULong64_t run, TFile *file, T Line 673  int S4Core(ULong64_t run, TFile *file, T
673    //    //
674    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
675    if ( tempfile ) tempfile->Close();                if ( tempfile ) tempfile->Close();            
676    gSystem->Unlink(tempname.str().c_str());    if ( myfold) gSystem->Unlink(tempname.str().c_str());
677      //
678    //if ( code < 0 ) printf("\n S4 - ERROR: an error occurred, try to save anyway...\n");    //runinfo->Write();
   //printf("\n Writing and closing rootple\n");  
   if ( runinfo ) runinfo->Close();      
679    if ( S4tr ) S4tr->SetName("S4");        if ( S4tr ) S4tr->SetName("S4");    
680    if ( file ){    if ( file ){
681      file->cd();      file->cd();
682      file->Write();      S4tr->Write("S4", TObject::kOverwrite);
683        //file->Write();
684    };    };
685    //    //
686    gSystem->Unlink(S4folder.str().c_str());    if ( myfold ) gSystem->Unlink(S4folder.str().c_str());
687    //    //
688    // the end    // the end
689    //    //
690    if (verbose) printf("\n Exiting...\n");    if (verbose) printf("\n Exiting...\n");
691    if(S4tr)S4tr->Delete();    //
692      if ( glroot ) delete glroot;
693      if ( runinfo ) runinfo->Close();    
694      if ( runinfo ) delete runinfo;
695      //
696    if(code < 0)  throw code;    if(code < 0)  throw code;
697    return(code);    return(code);
698  }  }
699    
700    
   

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.27

  ViewVC Help
Powered by ViewVC 1.1.23