/[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.2 by mocchiut, Fri Jun 30 09:22:00 2006 UTC revision 1.22 by mocchiut, Thu Sep 4 15:46:19 2008 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      tr->GetEntry(glS4calib->EV_ROOT);
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
206    //    //
207    // variables needed to reprocess data    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    //    //
279    // DB classes    // DB classes
280    //    //
281    GL_ROOT *glroot = new GL_ROOT();    GL_ROOT *glroot = new GL_ROOT();
282    GL_S4_CALIB *glS4calib = new GL_S4_CALIB();    GL_TIMESYNC *dbtime = 0;
283    //    //
284    // Let's start!    // Let's start!
285    //    //
# Line 175  int S4Core(ULong64_t run, TFile *file, T Line 287  int S4Core(ULong64_t run, TFile *file, T
287    // 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
288    // 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.
289    //    //
290    if ( run == 0ULL )  reproc = true;    if ( run == 0 )  reproc = true;
291    //    //
292    //    //
293    // Output file is "outputfile"    // Output file is "outputfile"
# Line 220  int S4Core(ULong64_t run, TFile *file, T Line 332  int S4Core(ULong64_t run, TFile *file, T
332    // number of run to be processed    // number of run to be processed
333    //    //
334    numbofrun = runinfo->GetNoRun();    numbofrun = runinfo->GetNoRun();
335      UInt_t totnorun = runinfo->GetRunEntries();
336    //    //
337    // 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
338    //    //
# Line 230  int S4Core(ULong64_t run, TFile *file, T Line 343  int S4Core(ULong64_t run, TFile *file, T
343      // tree does not exist, we are not reprocessing      // tree does not exist, we are not reprocessing
344      //      //
345      reproc = false;      reproc = false;
346      if ( run == 0ULL ){      if ( run == 0 ){
347        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");
348      }      }
349      if ( runinfo->IsReprocessing() && run != 0ULL ) {      if ( runinfo->IsReprocessing() && run != 0 ) {
350        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");
351      }      }
352    } else {    } else {
353      //      //
354      // 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?
355      //      //
356        S4trclone->SetAutoSave(900000000000000LL);
357      reproc = true;      reproc = true;
358      //      //
359      // update versioning information      // update versioning information
360      //      //
     //sgnl = runinfo->Update(run, "S4",S4version);  
361      if (verbose) printf("\n Preparing the pre-processing...\n");      if (verbose) printf("\n Preparing the pre-processing...\n");
362      //      //
363      if ( run == 0ULL ){      if ( run == 0 || totnorun == 1 ){
364        //        //
365        // we are reprocessing all the file        // we are reprocessing all the file
366        // 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 375  int S4Core(ULong64_t run, TFile *file, T
375        //        //
376        reprocall = false;        reprocall = false;
377        //        //
378        if (verbose) printf("\n S4 - WARNING: Reprocessing run number %llu \n",run);        if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run);
379        //        //
380        // copying old tree to a new file        // copying old tree to a new file
381        //        //
382          gSystem->MakeDirectory(S4folder.str().c_str());
383          myfold = true;
384        tempfile = new TFile(tempname.str().c_str(),"RECREATE");        tempfile = new TFile(tempname.str().c_str(),"RECREATE");
385        tempS4 = S4trclone->CloneTree(-1,"fast");        tempS4 = S4trclone->CloneTree(-1,"fast");
386        tempS4->SetName("S4-old");        tempS4->SetName("S4-old");
# Line 285  int S4Core(ULong64_t run, TFile *file, T Line 400  int S4Core(ULong64_t run, TFile *file, T
400    //    //
401    file->cd();    file->cd();
402    S4tr = new TTree("S4-new","PAMELA Level2 S4 data");    S4tr = new TTree("S4-new","PAMELA Level2 S4 data");
403      S4tr->SetAutoSave(900000000000000LL);
404    S4tr->Branch("S4Level2","S4Level2",&s4);    S4tr->Branch("S4Level2","S4Level2",&s4);
405    //    //
406    if ( reproc && !reprocall ){    if ( reproc && !reprocall ){
# Line 293  int S4Core(ULong64_t run, TFile *file, T Line 409  int S4Core(ULong64_t run, TFile *file, T
409      //      //
410      tempfile = new TFile(tempname.str().c_str(),"READ");      tempfile = new TFile(tempname.str().c_str(),"READ");
411      S4trclone = (TTree*)tempfile->Get("S4-old");      S4trclone = (TTree*)tempfile->Get("S4-old");
412        S4trclone->SetAutoSave(900000000000000LL);
413      S4trclone->SetBranchAddress("S4Level2",&s4clone);      S4trclone->SetBranchAddress("S4Level2",&s4clone);
414      //            //      
415      if ( nobefrun > 0 ){      if ( nobefrun > 0 ){
416        if (verbose){        if (verbose){
417          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");  
418          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);
419          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);
420        }        }
421        for (UInt_t j = 0; j < nobefrun; j++){        for (UInt_t j = 0; j < nobefrun; j++){
# Line 307  int S4Core(ULong64_t run, TFile *file, T Line 424  int S4Core(ULong64_t run, TFile *file, T
424          //          //
425          // copy s4clone to mydec          // copy s4clone to mydec
426          //          //
         //      s4 = new S4Level2();  
427          s4->Clear();          s4->Clear();
428            //
429          memcpy(&s4,&s4clone,sizeof(s4clone));          memcpy(&s4,&s4clone,sizeof(s4clone));
430          //          //
431          // Fill entry in the new tree          // Fill entry in the new tree
# Line 333  int S4Core(ULong64_t run, TFile *file, T Line 450  int S4Core(ULong64_t run, TFile *file, T
450      idRun = runlist->At(irun);      idRun = runlist->At(irun);
451      if (verbose){      if (verbose){
452        printf("\n\n\n ####################################################################### \n");        printf("\n\n\n ####################################################################### \n");
453        printf("                    PROCESSING RUN NUMBER %i \n",(int)idRun);        printf("                    PROCESSING RUN NUMBER %u \n",idRun);
454        printf(" ####################################################################### \n\n\n");        printf(" ####################################################################### \n\n\n");
455      }      }
456      //      //
457      runinfo->ID_REG_RUN = 0ULL;      runinfo->ID_ROOT_L0 = 0;
458      //      //
459      // store in the runinfo class the GL_RUN variables for our run      // store in the runinfo class the GL_RUN variables for our run
460      //      //
461      sgnl = 0;      sgnl = 0;
462      sgnl = runinfo->GetRunInfo(idRun);      sgnl = runinfo->GetRunInfo(idRun);
463      if ( sgnl ){      if ( sgnl ){
464        //printf(" S4 - ERROR: RunInfo exited with non-zero status\n");        if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
465        code = sgnl;        code = sgnl;
466        goto closeandexit;        goto closeandexit;
467      } else {      } else {
468        sgnl = 0;        sgnl = 0;
469      };      };
470      //      //
471      // 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
472      //      //
473      if ( runinfo->ID_REG_RUN == 0 ){      if ( runinfo->ID_ROOT_L0 == 0 ){
474        //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);
475        code = -5;        code = -5;
476        goto closeandexit;            goto closeandexit;    
477      };      };
478      //      //
479        // prepare the timesync for the db
480        //
481        TString host = glt->CGetHost();
482        TString user = glt->CGetUser();
483        TString psw = glt->CGetPsw();
484        TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());    
485        if ( !dbc->IsConnected() ) throw -504;
486        stringstream myquery;
487        myquery.str("");
488        myquery << "SET time_zone='+0:00'";
489        dbc->Query(myquery.str().c_str());
490        dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
491        //
492      // 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.
493      //      //
494      glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);      //    if ( !dbc->IsConnected() ) throw -504;
495        glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
496      //      //
497      ftmpname.str("");      ftmpname.str("");
498      ftmpname << glroot->PATH.Data() << "/";      ftmpname << glroot->PATH.Data() << "/";
# Line 370  int S4Core(ULong64_t run, TFile *file, T Line 501  int S4Core(ULong64_t run, TFile *file, T
501      //      //
502      // print out informations      // print out informations
503      //      //
504      totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;      totevent = runinfo->NEVENTS;
505      if (verbose){      if (verbose){
506        printf("\n LEVEL0 data file: %s \n",fname.Data());        printf("\n LEVEL0 data file: %s \n",fname.Data());
507        printf(" RUN HEADER absolute time is:  %llu \n",runinfo->RUNHEADER_TIME);        printf(" RUN HEADER absolute time is:  %u \n",runinfo->RUNHEADER_TIME);
508        printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);        printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
509        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));
510      }      }
511      //      //
512        //    if ( !totevent ) goto closeandexit;
513        //
514      // Open Level0 file      // Open Level0 file
515      //      //
516      l0File = new TFile(fname.Data());      l0File = new TFile(fname.Data());
517      if ( !l0File ) {      if ( !l0File ) {
518        //printf(" S4 - ERROR: problems opening  Level0 file\n");        if ( debug ) printf(" S4 - ERROR: problems opening  Level0 file\n");
519        code = -6;        code = -6;
520        goto closeandexit;        goto closeandexit;
521      };      };
522        //
523      l0tr = (TTree*)l0File->Get("Physics");      l0tr = (TTree*)l0File->Get("Physics");
524      if ( !l0tr ) {      if ( !l0tr ) {
525        //printf(" S4 - ERROR: no Physics tree in Level0 file\n");        if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n");
526        l0File->Close();        l0File->Close();
527        code = -7;        code = -7;
528        goto closeandexit;        goto closeandexit;
529      };      };
530      l0head = l0tr->GetBranch("Header");      l0head = l0tr->GetBranch("Header");
531      if ( !l0head ) {      if ( !l0head ) {
532        //printf(" S4 - ERROR: no Header branch in Level0 tree\n");        if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n");
533        l0File->Close();        l0File->Close();
534        code = -8;        code = -8;
535        goto closeandexit;            goto closeandexit;    
536      };      };
     l0registry = l0tr->GetBranch("Registry");  
     if ( !l0registry ) {  
       //printf(" S4 - ERROR: no Registry branch in Level0 tree\n");  
       l0File->Close();  
       code = -9;  
       goto closeandexit;      
     };  
537      l0S4 = l0tr->GetBranch("S4");      l0S4 = l0tr->GetBranch("S4");
538      if ( !l0S4 ) {      if ( !l0S4 ) {
539        //printf(" S4 - ERROR: no S4 branch in Level0 tree\n");        if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n");
540        l0File->Close();        l0File->Close();
541        code = -503;        code = -503;
542        goto closeandexit;        goto closeandexit;
543      };      };
   
544      //      //
     l0tr->SetBranchAddress("Registry", &l0reg);  
545      l0tr->SetBranchAddress("S4", &l0s4e);      l0tr->SetBranchAddress("S4", &l0s4e);
546      l0tr->SetBranchAddress("Header", &eh);      l0tr->SetBranchAddress("Header", &eh);
547      //      //
548      nevents = l0registry->GetEntries();      nevents = l0S4->GetEntries();
549      //      //
550      if ( nevents < 1 ) {      if ( nevents < 1 && totevent ) {
551        //printf(" S4 - ERROR: Level0 file is empty\n\n");        if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");
552        l0File->Close();        l0File->Close();
553        code = -11;        code = -11;
554        goto closeandexit;        goto closeandexit;
555      };      };
556      //      //
557      if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {      if ( runinfo->EV_TO > nevents-1 && totevent ) {
558        //printf(" S4 - ERROR: too few entries in the registry tree\n");        if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");
559        l0File->Close();        l0File->Close();
560        code = -12;        code = -12;
561        goto closeandexit;        goto closeandexit;
# Line 439  int S4Core(ULong64_t run, TFile *file, T Line 564  int S4Core(ULong64_t run, TFile *file, T
564      // 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)
565      // second query: which is the value of paramfit relative to the calibration?      // second query: which is the value of paramfit relative to the calibration?
566      //      //
567      glS4calib->Query_GL_S4_CALIB(runinfo->RUNHEADER_TIME, dbc);      TArrayD *params = 0;
568      ParamFit0 = glS4calib->PARAM_FIT0;      params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc);
569      ParamFit1 = glS4calib->PARAM_FIT1;      if ( !params ){
570          code = -13;
571          goto closeandexit;
572        };
573        //
574        ParamFit0 = params->At(0);
575        ParamFit1 = params->At(1);
576      //      //
577      // run over all the events of the run      // run over all the events of the run
578      //      //
579      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");
580      //      //
581      for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){      if ( dbc ){
582          dbc->Close();
583          //      delete dbc;
584        };
585        //
586        for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
587        //        //
588        if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);          if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);  
589        //        //
590        l0registry->GetEntry(re);        l0head->GetEntry(re);
591        //        //
592        // absolute time of this event        // absolute time of this event
593        //        //
594        atime = l0reg->absTime;        ph = eh->GetPscuHeader();
595        //        atime = dbtime->DBabsTime(ph->GetOrbitalTime());  
       // physics events is at entry number ei where  
       //  
       ei = l0reg->event;  
596        //        //
597        // paranoid check        // paranoid check
598        //        //
# Line 472  int S4Core(ULong64_t run, TFile *file, T Line 605  int S4Core(ULong64_t run, TFile *file, T
605        //        //
606        // start processing        // start processing
607        //        //
       //      s4 = new S4Level2();  
608        s4->Clear();        s4->Clear();
609        l0S4->GetEntry(ei);        l0S4->GetEntry(re);
610        if (l0s4e->unpackError == 1)  continue;              if (l0s4e->unpackError == 0){
611        s4->S4adc = l0s4e->S4_DATA;          s4->S4adc = l0s4e->S4_DATA;
612        if ((l0s4e->S4_DATA) > 31 ){          //
613          s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;          if ((l0s4e->S4_DATA) > 31 ){
614        }else{            s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;
615          s4->S4calibrated = 0;          }else{
616        }            s4->S4calibrated = 0;
617            }
618          };
619          //
620          s4->unpackError = l0s4e->unpackError;
621          //
622        S4tr->Fill();        S4tr->Fill();
623        //            //    
624        //        //
# Line 492  int S4Core(ULong64_t run, TFile *file, T Line 629  int S4Core(ULong64_t run, TFile *file, T
629      //      //
630      // Here you may want to clear some variables before processing another run        // Here you may want to clear some variables before processing another run  
631      //      //
632      ei = 0;      delete dbtime;
633        if ( params ) delete params;
634        //
635    }; // process all the runs    }; // process all the runs
636    //    //
637    if (verbose) printf("\n Finished processing data \n");    if (verbose) printf("\n Finished processing data \n");
# Line 532  int S4Core(ULong64_t run, TFile *file, T Line 671  int S4Core(ULong64_t run, TFile *file, T
671    //    //
672    if ( l0File ) l0File->Close();    if ( l0File ) l0File->Close();
673    if ( tempfile ) tempfile->Close();                if ( tempfile ) tempfile->Close();            
674    gSystem->Unlink(tempname.str().c_str());    if ( myfold) gSystem->Unlink(tempname.str().c_str());
675      //
   //if ( code < 0 ) printf("\n S4 - ERROR: an error occurred, try to save anyway...\n");  
   //printf("\n Writing and closing rootple\n");  
676    if ( runinfo ) runinfo->Close();        if ( runinfo ) runinfo->Close();    
677    if ( S4tr ) S4tr->SetName("S4");        if ( S4tr ) S4tr->SetName("S4");    
678    if ( file ){    if ( file ){
# Line 543  int S4Core(ULong64_t run, TFile *file, T Line 680  int S4Core(ULong64_t run, TFile *file, T
680      file->Write();      file->Write();
681    };    };
682    //    //
683    gSystem->Unlink(S4folder.str().c_str());    if ( myfold ) gSystem->Unlink(S4folder.str().c_str());
684    //    //
685    // the end    // the end
686    //    //
687    if (verbose) printf("\n Exiting...\n");    if (verbose) printf("\n Exiting...\n");
688    if(S4tr)S4tr->Delete();    if ( S4tr ) S4tr->Delete();
689      //
690      if ( s4 ) delete s4;
691      if ( s4clone ) delete s4clone;
692      if ( glroot ) delete glroot;
693      if ( runinfo ) delete runinfo;
694      //
695    if(code < 0)  throw code;    if(code < 0)  throw code;
696    return(code);    return(code);
697  }  }
698    
699    
   

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.22

  ViewVC Help
Powered by ViewVC 1.1.23