/[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.1 - (hide annotations) (download)
Sat Jun 17 12:14:34 2006 UTC (18 years, 5 months ago) by mocchiut
Branch: MAIN
Branch point for: TriggerLevel2
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.23