/[PAMELA software]/DarthVader/TriggerLevel2/src/TrigCore.cpp
ViewVC logotype

Annotation of /DarthVader/TriggerLevel2/src/TrigCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Thu Nov 9 17:05:50 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
Changes since 1.4: +2 -0 lines
Added unpackError in all classes, ToF .rz bug fixed, install_GL_PARAM time screw bug fixed

1 mocchiut 1.1 //
2     // C/C++ headers
3     //
4     #include <fstream>
5     #include <string.h>
6     //
7     // ROOT headers
8     //
9     #include <TTree.h>
10     #include <TClassEdit.h>
11     #include <TObject.h>
12     #include <TList.h>
13     #include <TArrayL.h>
14     #include <TSystem.h>
15     #include <TSystemDirectory.h>
16     #include <TString.h>
17     #include <TFile.h>
18     #include <TClass.h>
19     #include <TCanvas.h>
20     #include <TH1.h>
21     #include <TH1F.h>
22     #include <TH2D.h>
23     #include <TLatex.h>
24     #include <TPad.h>
25     #include <TSQLServer.h>
26     #include <TSQLRow.h>
27     #include <TSQLResult.h>
28     #include <TClonesArray.h>
29     //
30     // YODA headers
31     //
32     #include <PamelaRun.h>
33     #include <physics/trigger/TriggerEvent.h>
34     //
35     // RunInfo header
36     //
37     #include <RunInfo.h>
38     //
39     // This program headers
40     //
41     #include <TrigCore.h>
42     #include <TrigLevel2.h>
43     #include <TrigVerl2.h>
44     //
45     using namespace std;
46     //
47     //
48     // CORE ROUTINE
49     //
50     //
51 mocchiut 1.4 int TrigCore(UInt_t run, TFile *file, TSQLServer *dbc, Int_t Trigargc, char *Trigargv[]){
52 mocchiut 1.1 //
53     TString processFolder = "TrigFolder";
54     //
55     // Set these to true to have a very verbose output.
56     //
57     Bool_t verbose = false;
58     Bool_t debug = false;
59     //
60     if ( Trigargc > 0 ){
61     Int_t i = 0;
62     while ( i < Trigargc ){
63     if ( !strcmp(Trigargv[i],"-processFolder") ) {
64     if ( Trigargc < i+1 ){
65     throw -3;
66     };
67     processFolder = (TString)Trigargv[i+1];
68     i++;
69     };
70     if ( !strcmp(Trigargv[i],"-v") || !strcmp(Trigargv[i],"--verbose") ) {
71     verbose = true;
72     };
73     if ( !strcmp(Trigargv[i],"-g") || !strcmp(Trigargv[i],"--debug") ) {
74     debug = true;
75     };
76     i++;
77     };
78     };
79     //
80     //
81     // Output directory is the working directoy.
82     //
83     const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
84     //
85     // Variables for level2
86     //
87     TTree *trigt = 0;
88 mocchiut 1.4 UInt_t nevents = 0;
89 mocchiut 1.1 //
90     // variables needed to reprocess data
91     //
92     TString trigversion;
93     ItoRunInfo *runinfo = 0;
94 mocchiut 1.4 TArrayI *runlist = 0;
95 mocchiut 1.1 TTree *trigtclone = 0;
96     Bool_t reproc = false;
97     Bool_t reprocall = false;
98     UInt_t nobefrun = 0;
99     UInt_t noaftrun = 0;
100     UInt_t numbofrun = 0;
101     stringstream ftmpname;
102     TString fname;
103 mocchiut 1.4 UInt_t totfileentries = 0;
104     UInt_t idRun = 0;
105 mocchiut 1.1 //
106     // variables needed to handle error signals
107     //
108     Int_t code = 0;
109     Int_t sgnl;
110     //
111     // trigger level2 classes
112     //
113     TrigLevel2 *trig = new TrigLevel2();
114     TrigLevel2 *trigclone = new TrigLevel2();
115     //
116     // define variables for opening and reading level0 file
117     //
118     TFile *l0File = 0;
119     TTree *l0tr = 0;
120     TBranch *l0head = 0;
121     TBranch *l0trig = 0;
122 mocchiut 1.4 pamela::EventHeader *eh = 0;
123     pamela::PscuHeader *ph = 0;
124 mocchiut 1.1 pamela::trigger::TriggerEvent *triggerEvent = 0;
125     //
126     // Define other basic variables
127     //
128     UInt_t procev = 0;
129     stringstream file2;
130     stringstream file3;
131     stringstream qy;
132     Int_t totevent = 0;
133 mocchiut 1.4 UInt_t atime = 0;
134     UInt_t re = 0;
135 mocchiut 1.1 //
136     // Working filename
137     //
138     TString outputfile;
139     stringstream name;
140     name.str("");
141     name << outdir << "/";
142     //
143     // temporary file and folder
144     //
145     TFile *tempfile = 0;
146     TTree *temptrig = 0;
147     stringstream tempname;
148     stringstream Trigfolder;
149     tempname.str("");
150     tempname << outdir;
151     tempname << "/" << processFolder.Data();
152     Trigfolder.str("");
153     Trigfolder << tempname.str().c_str();
154     gSystem->MakeDirectory(Trigfolder.str().c_str());
155     tempname << "/trigtree_run";
156     tempname << run << ".root";
157     //
158     // DB classes
159     //
160     GL_ROOT *glroot = new GL_ROOT();
161 mocchiut 1.4 GL_TIMESYNC *dbtime = 0;
162 mocchiut 1.1 //
163     // Let's start!
164     //
165     //
166     // As a first thing we must check what we have to do: if run = 0 we must process all events in the file has been passed
167     // if run != 0 we must process only that run but first we have to check if the tree Trigger already exist in the file
168     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
169     //
170 mocchiut 1.4 if ( run == 0 ) reproc = true;
171 mocchiut 1.1 //
172     //
173     // Output file is "outputfile"
174     //
175     if ( !file->IsOpen() ){
176     if ( verbose ) printf(" Trigger - ERROR: cannot open file for writing\n");
177     throw -401;
178     };
179     //
180     // Retrieve GL_RUN variables from the level2 file
181     //
182     trigversion = TrigInfo(false); // we should decide how to handle versioning system
183     //
184     // create an interface to RunInfo called "runinfo"
185     //
186     runinfo = new ItoRunInfo(file);
187     //
188     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
189     //
190     sgnl = 0;
191     sgnl = runinfo->Update(run,"TRIG",trigversion);
192     if ( sgnl ){
193     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
194     code = sgnl;
195     goto closeandexit;
196     } else {
197     sgnl = 0;
198     };
199     //
200     // number of events in the file BEFORE the first event of our run
201     //
202     nobefrun = runinfo->GetFirstEntry();
203     //
204     // total number of events in the file
205     //
206     totfileentries = runinfo->GetFileEntries();
207     //
208     // first file entry AFTER the last event of our run
209     //
210     noaftrun = runinfo->GetLastEntry() + 1;
211     //
212     // number of run to be processed
213     //
214     numbofrun = runinfo->GetNoRun();
215     //
216     // Try to access the Trigger tree in the file, if it exists we are reprocessing data if not we are processing a new run
217     //
218     trigtclone = (TTree*)file->Get("Trigger");
219     //
220     if ( !trigtclone ){
221     //
222     // tree does not exist, we are not reprocessing
223     //
224     reproc = false;
225 mocchiut 1.4 if ( run == 0 && verbose ) printf(" Trigger - WARNING: you are reprocessing data but Trigger tree does not exist!\n");
226     if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" Trigger - WARNING: it seems you are not reprocessing data but Trigger\n versioning information already exists in RunInfo.\n");
227 mocchiut 1.1
228     } else {
229     //
230     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
231     //
232     reproc = true;
233     //
234     // update versioning information
235     //
236     if ( verbose ) printf("\n Preparing the pre-processing...\n");
237     //
238 mocchiut 1.4 if ( run == 0 ){
239 mocchiut 1.1 //
240     // we are reprocessing all the file
241     // 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
242     //
243     reprocall = true;
244     //
245     if ( verbose ) printf("\n Trigger - WARNING: Reprocessing all runs\n");
246     //
247     } else {
248     //
249     // we are reprocessing a single run, we must copy to the new tree the events in the file which preceed the first event of the run
250     //
251     reprocall = false;
252     //
253 mocchiut 1.4 if ( verbose ) printf("\n Trigger - WARNING: Reprocessing run number %u \n",run);
254 mocchiut 1.1 //
255     // copying old tree to a new file
256     //
257     tempfile = new TFile(tempname.str().c_str(),"RECREATE");
258     temptrig = trigtclone->CloneTree(-1,"fast");
259     temptrig->SetName("Trigger-old");
260     tempfile->Write();
261     tempfile->Close();
262     }
263     //
264     // Delete the old tree from old file and memory
265     //
266     trigtclone->Delete("all");
267     //
268     if ( verbose ) printf(" ...done!\n");
269     //
270     };
271     //
272     // create Trigger tree trigt
273     //
274     file->cd();
275     trigt = new TTree("Trigger-new","PAMELA Level2 Trigger data");
276     trigt->Branch("TrigLevel2","TrigLevel2",&trig);
277     //
278     if ( reproc && !reprocall ){
279     //
280     // open new file and retrieve alo tree informations
281     //
282     tempfile = new TFile(tempname.str().c_str(),"READ");
283     trigtclone = (TTree*)tempfile->Get("Trigger-old");
284     trigtclone->SetBranchAddress("TrigLevel2",&trigclone);
285     //
286     if ( nobefrun > 0 ){
287     if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
288 mocchiut 1.4 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
289 mocchiut 1.1 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
290     for (UInt_t j = 0; j < nobefrun; j++){
291     //
292     trigtclone->GetEntry(j);
293     //
294     // copy trigclone to trig
295     //
296 mocchiut 1.2 // trig = new TrigLevel2();
297     trig->Clear();
298 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
299     //
300     // Fill entry in the new tree
301     //
302     trigt->Fill();
303     //
304     };
305     if ( verbose ) printf(" Finished successful copying!\n");
306     };
307     };
308     //
309     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
310     //
311     runlist = runinfo->GetRunList();
312     //
313     // Loop over the run to be processed
314     //
315     for (UInt_t irun=0; irun < numbofrun; irun++){
316     //
317     // retrieve the first run ID to be processed using the RunInfo list
318     //
319     idRun = runlist->At(irun);
320     if ( verbose ) printf("\n\n\n ####################################################################### \n");
321 mocchiut 1.4 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
322 mocchiut 1.1 if ( verbose ) printf(" ####################################################################### \n\n\n");
323     //
324 mocchiut 1.4 runinfo->ID_ROOT_L0 = 0;
325 mocchiut 1.1 //
326     // store in the runinfo class the GL_RUN variables for our run
327     //
328     sgnl = 0;
329     sgnl = runinfo->GetRunInfo(idRun);
330     if ( sgnl ){
331     if ( verbose ) printf(" Trigger - ERROR: RunInfo exited with non-zero status\n");
332     code = sgnl;
333     goto closeandexit;
334     } else {
335     sgnl = 0;
336     };
337     //
338 mocchiut 1.4 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
339 mocchiut 1.1 //
340 mocchiut 1.4 if ( runinfo->ID_ROOT_L0 == 0 ){
341     if ( verbose ) printf("\n Trigger - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
342 mocchiut 1.1 code = -5;
343     goto closeandexit;
344     };
345     //
346 mocchiut 1.4 // prepare the timesync for the db
347     //
348     dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
349     //
350 mocchiut 1.1 // Search in the DB the path and name of the LEVEL0 file to be processed.
351     //
352 mocchiut 1.4 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
353 mocchiut 1.1 //
354     ftmpname.str("");
355     ftmpname << glroot->PATH.Data() << "/";
356     ftmpname << glroot->NAME.Data();
357     fname = ftmpname.str().c_str();
358     //
359     // print out informations
360     //
361 mocchiut 1.4 totevent = runinfo->NEVENTS;
362 mocchiut 1.1 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
363 mocchiut 1.4 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
364     if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
365     if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM,runinfo->EV_FROM+totevent);
366 mocchiut 1.1 //
367     // Open Level0 file
368     //
369     l0File = new TFile(fname.Data());
370     if ( !l0File ) {
371     if ( verbose ) printf(" Trigger - ERROR: problems opening Level0 file\n");
372     code = -6;
373     goto closeandexit;
374     };
375     l0tr = (TTree*)l0File->Get("Physics");
376     if ( !l0tr ) {
377     if ( verbose ) printf(" Trigger - ERROR: no Physics tree in Level0 file\n");
378     l0File->Close();
379     code = -7;
380     goto closeandexit;
381     };
382     l0head = l0tr->GetBranch("Header");
383     if ( !l0head ) {
384     if ( verbose ) printf(" Trigger - ERROR: no Header branch in Level0 tree\n");
385     l0File->Close();
386     code = -8;
387     goto closeandexit;
388     };
389     l0trig = l0tr->GetBranch("Trigger");
390     if ( !l0trig ) {
391     if ( verbose ) printf(" Trigger - ERROR: no Trigger branch in Level0 tree\n");
392     l0File->Close();
393     code = -402;
394     goto closeandexit;
395     };
396     //
397     l0tr->SetBranchAddress("Trigger", &triggerEvent);
398 mocchiut 1.4 l0tr->SetBranchAddress("Header", &eh);
399 mocchiut 1.1 //
400 mocchiut 1.4 nevents = l0trig->GetEntries();
401 mocchiut 1.1 //
402     if ( nevents < 1 ) {
403     if ( verbose ) printf(" Trigger - ERROR: Level0 file is empty\n\n");
404     l0File->Close();
405     code = -11;
406     goto closeandexit;
407     };
408     //
409 mocchiut 1.4 if ( runinfo->EV_TO > nevents-1 ) {
410     if ( verbose ) printf(" Trigger - ERROR: too few entries in the tree\n");
411 mocchiut 1.1 l0File->Close();
412     code = -12;
413     goto closeandexit;
414     };
415     //
416     // run over all the events of the run
417     //
418     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
419     //
420 mocchiut 1.4 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
421 mocchiut 1.1 //
422     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
423     //
424 mocchiut 1.4 l0head->GetEntry(re);
425 mocchiut 1.1 //
426     // absolute time of this event
427     //
428 mocchiut 1.4 ph = eh->GetPscuHeader();
429     atime = dbtime->DBabsTime(ph->GetOrbitalTime());
430 mocchiut 1.1 //
431     // paranoid check
432     //
433     if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME ) {
434     if ( verbose ) printf(" Trigger - WARNING: event at time outside the run time window, skipping it\n");
435     goto jumpev;
436     };
437     ///
438 mocchiut 1.4 l0trig->GetEntry(re);
439 mocchiut 1.1 ///
440     //
441     procev++;
442     //
443     // start processing
444     //
445 mocchiut 1.2 trig->Clear();
446     // trig = new TrigLevel2();
447 mocchiut 1.1 //
448     // now we must copy from the output structure to the level2 class:
449     //
450     trig->evcount = triggerEvent->evcount;
451     for (Int_t kk=0; kk<3;kk++){
452     trig->pmtpl[kk] = triggerEvent->pmtpl[kk];
453     trig->patternbusy[kk] = triggerEvent->patternbusy[kk];
454     }
455    
456     for (Int_t kk=0; kk<6;kk++){
457     trig->trigrate[kk] = triggerEvent->trigrate[kk];
458     trig->patterntrig[kk] = triggerEvent->patterntrig[kk];
459     }
460    
461     for (Int_t kk=0; kk<2;kk++){
462     trig->dltime[kk] = triggerEvent->dltime[kk];
463     trig->s4calcount[kk] = triggerEvent->s4calcount[kk];
464     }
465    
466     for (Int_t kk=0; kk<24;kk++){
467     trig->pmtcount1[kk] = triggerEvent->pmtcount1[kk];
468     trig->pmtcount2[kk] = triggerEvent->pmtcount2[kk];
469     }
470    
471     trig->trigconf = triggerEvent->trigconf;
472 mocchiut 1.5
473     trig->unpackError = triggerEvent->unpackError;
474 mocchiut 1.1
475     trigt->Fill();
476     //
477     //
478     jumpev:
479     debug = false;
480     //
481     };
482 mocchiut 1.4 //
483     // Here you may want to clear some variables before processing another run
484     //
485     delete dbtime;
486 mocchiut 1.1 }; // process all the runs
487     //
488     if ( verbose ) printf("\n Finished processing data \n");
489     //
490     closeandexit:
491     //
492     // we have finished processing the run(s). If we processed a single run now we must copy all the events after our run from the old tree to the new one and delete the old tree.
493     //
494     if ( !reprocall && reproc && code >= 0 ){
495     if ( totfileentries > noaftrun ){
496     if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
497     if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
498     if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
499     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
500     //
501     // Get entry from old tree
502     //
503     trigtclone->GetEntry(j);
504     //
505     // copy trigclone to trig
506     //
507 mocchiut 1.2 trig->Clear();
508 mocchiut 1.4 //
509 mocchiut 1.1 memcpy(&trig,&trigclone,sizeof(trigclone));
510     //
511     // Fill entry in the new tree
512     //
513     trigt->Fill();
514     };
515     if ( verbose ) printf(" Finished successful copying!\n");
516     };
517     };
518     //
519     // Close files, delete old tree(s), write and close level2 file
520     //
521     if ( l0File ) l0File->Close();
522     if ( tempfile ) tempfile->Close();
523     gSystem->Unlink(tempname.str().c_str());
524     //
525     if ( code < 0 && verbose ) printf("\n Trigger - ERROR: an error occurred, try to save anyway...\n");
526     if ( verbose ) printf("\n Writing and closing rootple\n");
527     if ( runinfo ) runinfo->Close();
528     if ( trigt ) trigt->SetName("Trigger");
529     if ( file ){
530     file->cd();
531     file->Write();
532     // file->Write("Trigger");
533     };
534     //
535     gSystem->Unlink(Trigfolder.str().c_str());
536     //
537     // the end
538     //
539     if ( verbose ) printf("\n Exiting...\n");
540     if ( trigt ) trigt->Delete();
541 mocchiut 1.3 //
542     if ( trig ) delete trig;
543     if ( trigclone ) delete trigclone;
544     if ( glroot ) delete glroot;
545     if ( runinfo ) delete runinfo;
546     //
547 mocchiut 1.1 if ( code < 0 ) throw code;
548     return(code);
549     }
550    
551    
552    

  ViewVC Help
Powered by ViewVC 1.1.23