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

  ViewVC Help
Powered by ViewVC 1.1.23