/[PAMELA software]/DarthVader/AnticounterLevel2/src/AcCore.cpp
ViewVC logotype

Annotation of /DarthVader/AnticounterLevel2/src/AcCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Fri Jun 30 09:21:50 2006 UTC (18 years, 5 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r01beta, v1r01
Changes since 1.2: +6 -3 lines
Memory leak 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 <RegistryEvent.h>
34     #include <physics/trigger/TriggerEvent.h>
35     #include <physics/anticounter/AnticounterEvent.h>
36     //
37     // RunInfo header
38     //
39     #include <RunInfo.h>
40     //
41     // This program headers
42     //
43     #include <AcCore.h>
44     #include <AcLevel2.h>
45     #include <AcVerl2.h>
46     //
47     using namespace std;
48     //
49     // CORE ROUTINE
50     //
51     //
52     int AcCore(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t Acargc, char *Acargv[]){
53     Int_t i = 0;
54     Bool_t verbose = false;
55     Bool_t debug = false;
56     //
57     TString processFolder = "AcFolder";
58     if ( Acargc > 0 ){
59     i = 0;
60     while ( i < Acargc ){
61     if ( !strcmp(Acargv[i],"-processFolder") ) {
62     if ( Acargc < i+1 ){
63     throw -3;
64     };
65     processFolder = (TString)Acargv[i+1];
66     i++;
67     };
68     if ( !strcmp(Acargv[i],"-v") || !strcmp(Acargv[i],"--verbose") ) {
69     verbose = true;
70     };
71     if ( !strcmp(Acargv[i],"-g") || !strcmp(Acargv[i],"--debug") ) {
72     debug = true;
73     };
74     i++;
75     };
76     };
77     //
78     // Set these to true to have a very verbose output.
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 *ac = 0;
88     Long64_t nevents = 0LL;
89     //
90     // variables needed to reprocess data
91     //
92     TString acversion;
93     ItoRunInfo *runinfo = 0;
94     TArrayL *runlist = 0;
95     TTree *acclone = 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     Long64_t totfileentries = 0ULL;
104     Long64_t idRun = 0LL;
105     //
106     // variables needed to handle error signals
107     //
108     Int_t code = 0;
109     Int_t sgnl;
110     //
111     // anticounter level2 classes
112     //
113     AcLevel2 *acl2 = new AcLevel2();
114     AcLevel2 *acl2clone = new AcLevel2();
115     // note: the string "mydect" is now "ac", "mydetector" is "anticounter" (mydectversion -> acversion, mydectclone -> acclone);
116     // the string "mydec" (without t) -> acl2 (acl2, acl2clone)
117     //
118     // define variables for opening and reading level0 file
119     //
120     TFile *l0File = 0;
121     TTree *l0tr = 0;
122     TBranch *l0head = 0;
123     TBranch *l0registry = 0;
124     TBranch *l0ac = 0;
125     pamela::RegistryEvent *l0reg=0;
126     pamela::anticounter::AnticounterEvent *acc = 0;
127     // pamela::trigger::TriggerEvent *trig = 0;
128     //AcLevel2 *acl2 = new AcLevel2();
129     //
130     // Define other basic variables
131     //
132     UInt_t procev = 0;
133     stringstream file2;
134     stringstream file3;
135     stringstream qy;
136     // Int_t itr = -1;
137     Int_t totevent = 0;
138     ULong64_t atime = 0ULL;
139     Int_t ei = 0;
140     Int_t re = 0;
141     //
142     // Working filename
143     //
144     TString outputfile;
145     stringstream name;
146     name.str("");
147     name << outdir << "/";
148     //
149     // temporary file and folder
150     //
151     TFile *tempfile = 0;
152     TTree *tempac = 0;
153     stringstream tempname;
154     stringstream acfolder;
155     tempname.str("");
156     tempname << outdir;
157     tempname << "/" << processFolder.Data();
158     acfolder.str("");
159     acfolder << tempname.str().c_str();
160     gSystem->MakeDirectory(acfolder.str().c_str());
161     tempname << "/ac2tree_run";
162     tempname << run << ".root";
163     //
164     // DB classes
165     //
166     GL_ROOT *glroot = new GL_ROOT();
167     //GL_PARAM *glparam = new GL_PARAM();
168     //
169     // Let's start!
170     //
171     //
172     // 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
173     // if run != 0 we must process only that run but first we have to check if the tree Anticounter already exist in the file
174     // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
175     //
176     if ( run == 0ULL ) reproc = true;
177     //
178     // Output file is "outputfile"
179     //
180     outputfile = name.str().c_str();
181     if ( verbose ) printf("\n Output filename is: \n %s \n\n",outputfile.Data());
182     //
183     //
184     if ( !file->IsOpen() ){
185     if ( verbose ) printf(" ANTICOUNTER - ERROR: cannot open file for writing\n");
186     throw -701;
187     };
188     //
189     // Retrieve GL_RUN variables from the level2 file
190     //
191     acversion = AcInfo(false); // we should decide how to handle versioning system
192     //
193     // create an interface to RunInfo called "runinfo"
194     //
195     runinfo = new ItoRunInfo(file);
196     //
197     // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
198     //
199     sgnl = 0;
200     sgnl = runinfo->Update(run,"AC",acversion);
201     if ( sgnl ){
202     if ( verbose ) printf(" ANTICOUNTER - ERROR: RunInfo exited with non-zero status\n");
203     code = sgnl;
204     goto closeandexit;
205     } else {
206     sgnl = 0;
207     };
208     //
209     // number of events in the file BEFORE the first event of our run
210     //
211     nobefrun = runinfo->GetFirstEntry();
212     //
213     // total number of events in the file
214     //
215     totfileentries = runinfo->GetFileEntries();
216     //
217     // first file entry AFTER the last event of our run
218     //
219     noaftrun = runinfo->GetLastEntry() + 1;
220     //
221     // number of run to be processed
222     //
223     numbofrun = runinfo->GetNoRun();
224     //
225     // Try to access the Anticounter tree in the file, if it exists we are reprocessing data if not we are processing a new run
226     //
227     acclone = (TTree*)file->Get("Anticounter");
228     //
229     if ( !acclone ){
230     //
231     // tree does not exist, we are not reprocessing
232     //
233     reproc = false;
234     if ( run == 0ULL && verbose ) printf(" ANTICOUNTER - WARNING: you are reprocessing data but AC tree does not exist!\n");
235     if ( verbose && runinfo->IsReprocessing() && run != 0ULL ) printf(" ANTICOUNTER - WARNING: it seems you are not reprocessing data but Anticounter\n versioning information already exists in RunInfo.\n");
236    
237     } else {
238     //
239     // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
240     //
241     reproc = true;
242     //
243     //
244     if ( verbose ) printf("\n Preparing the pre-processing...\n");
245     //
246     if ( run == 0ULL ){
247     //
248     // we are reprocessing all the file
249     // 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
250     //
251     reprocall = true;
252     //
253     if ( verbose ) printf("\n ANTICOUNTER - WARNING: Reprocessing all runs\n");
254     //
255     } else {
256     //
257     // 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
258     //
259     reprocall = false;
260     //
261     if ( verbose ) printf("\n ANTICOUNTER - WARNING: Reprocessing run number %llu \n",run);
262     //
263     // copying old tree to a new file
264     //
265     tempfile = new TFile(tempname.str().c_str(),"RECREATE");
266     tempac = acclone->CloneTree(-1,"fast");
267     tempac->SetName("Anticounter-old");
268     tempfile->Write();
269     tempfile->Close();
270     }
271     //
272     // Delete the old tree from old file and memory
273     //
274     acclone->Delete("all");
275     //
276     if ( verbose ) printf(" ...done!\n");
277     //
278     };
279     //
280     // create anticounter tree ac
281     //
282     file->cd();
283     ac = new TTree("Anticounter-new","PAMELA Level2 Anticounter data");
284     ac->Branch("AcLevel2","AcLevel2",&acl2);
285     //
286     if ( reproc && !reprocall ){
287     //
288     // open new file and retrieve alo tree informations
289     //
290     tempfile = new TFile(tempname.str().c_str(),"READ");
291     acclone = (TTree*)tempfile->Get("Anticounter-old");
292     acclone->SetBranchAddress("AcLevel2",&acl2clone);
293     //
294     if ( nobefrun > 0 ){
295     if ( verbose ) {
296     printf("\n Pre-processing: copying events from the old tree before the processed run\n");
297     printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run);
298     printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
299     };
300     for (UInt_t j = 0; j < nobefrun; j++){
301     //
302     acclone->GetEntry(j);
303     //
304     // copy acl2clone to acl2
305     //
306 mocchiut 1.3 acl2->Clear();
307     // acl2 = new AcLevel2();
308 mocchiut 1.1 memcpy(&acl2,&acl2clone,sizeof(acl2clone));
309     //
310     // Fill entry in the new tree
311     //
312     ac->Fill();
313     //
314     };
315     if ( verbose ) printf(" Finished successful copying!\n");
316     };
317     };
318     //
319     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
320     //
321     runlist = runinfo->GetRunList();
322     //
323     // Loop over the run to be processed
324     //
325     for (UInt_t irun=0; irun < numbofrun; irun++){
326     //
327     // retrieve the first run ID to be processed using the RunInfo list
328     //
329     idRun = runlist->At(irun);
330     if ( verbose ){
331     printf("\n\n\n ####################################################################### \n");
332     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
333     printf(" ####################################################################### \n\n\n");
334     };
335     //
336     runinfo->ID_REG_RUN = 0ULL;
337     //
338     // store in the runinfo class the GL_RUN variables for our run
339     //
340     sgnl = 0;
341     sgnl = runinfo->GetRunInfo(idRun);
342     if ( sgnl ){
343     if ( verbose ) printf(" ANTICOUNTER - ERROR: RunInfo exited with non-zero status\n");
344     code = sgnl;
345     goto closeandexit;
346     } else {
347     sgnl = 0;
348     };
349     //
350     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
351     //
352     if ( runinfo->ID_REG_RUN == 0 ){
353     if ( verbose ) printf("\n ANTICOUNTER - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);
354     code = -5;
355     goto closeandexit;
356     };
357     //
358     // Search in the DB the path and name of the LEVEL0 file to be processed.
359     //
360     glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);
361     //
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     totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;
370     if ( verbose ) {
371     printf("\n LEVEL0 data file: %s \n",fname.Data());
372     printf(" RUN HEADER absolute time is: %llu \n",runinfo->RUNHEADER_TIME);
373     printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);
374     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);
375     };
376     //
377     // Open Level0 file
378     //
379     l0File = new TFile(fname.Data());
380     if ( !l0File ) {
381     if ( verbose ) printf(" ANTICOUNTER - ERROR: problems opening Level0 file\n");
382     code = -6;
383     goto closeandexit;
384     };
385     l0tr = (TTree*)l0File->Get("Physics");
386     if ( !l0tr ) {
387     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Physics tree in Level0 file\n");
388     l0File->Close();
389     code = -7;
390     goto closeandexit;
391     };
392     l0head = l0tr->GetBranch("Header");
393     if ( !l0head ) {
394     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Header branch in Level0 tree\n");
395     l0File->Close();
396     code = -8;
397     goto closeandexit;
398     };
399     l0registry = l0tr->GetBranch("Registry");
400     if ( !l0registry ) {
401     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Registry branch in Level0 tree\n");
402     l0File->Close();
403     code = -9;
404     goto closeandexit;
405     };
406     l0ac = l0tr->GetBranch("Anticounter");
407     if ( !l0ac ) {
408     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Anticounter branch in Level0 tree\n");
409     l0File->Close();
410     code = -704;
411     goto closeandexit;
412     };
413     //
414     //l0tr->SetBranchAddress("Trigger", &trig);
415     l0tr->SetBranchAddress("Anticounter", &acc);
416     l0tr->SetBranchAddress("Registry", &l0reg);
417     //
418     nevents = l0registry->GetEntries();
419     //
420     if ( nevents < 1 ) {
421     if ( verbose ) printf(" ANTICOUNTER - ERROR: Level0 file is empty\n\n");
422     l0File->Close();
423     code = -11;
424     goto closeandexit;
425     };
426     //
427     if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {
428     if ( verbose ) printf(" ANTICOUNTER - ERROR: too few entries in the registry tree\n");
429     l0File->Close();
430     code = -12;
431     goto closeandexit;
432     };
433     //
434     // run over all the events of the run
435     //
436     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
437     //
438     for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){
439     //
440     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
441     //
442     l0registry->GetEntry(re);
443     //
444     // absolute time of this event
445     //
446     atime = l0reg->absTime;
447     //
448     // physics events is at entry number ei where
449     //
450     ei = l0reg->event;
451     //
452     // paranoid check
453     //
454     if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME ) {
455     if ( verbose ) printf(" ANTICOUNTER - WARNING: event at time outside the run time window, skipping it\n");
456     goto jumpev;
457     };
458     //
459     procev++;
460     //
461     // start processing
462     //
463 mocchiut 1.3 // acl2 = new AcLevel2();
464     acl2->Clear();
465 mocchiut 1.1 //
466 pam-se 1.2 l0ac->GetEntry(ei);
467     //
468 mocchiut 1.1 /***********************************************/
469     // Here starts Anticounter specific code (Silvio)
470     //
471     UShort_t CRCcheck[2];
472     UShort_t Status[2];
473     UShort_t Hitmap[2];
474     UShort_t Hitstatus[2];
475     UShort_t Trigger[2];
476     UShort_t Trigg_old[2];
477     UShort_t Counter[2][16];
478     UShort_t Counter_old[2][16];
479    
480     UShort_t Shift[2][16];
481     Int_t vec[2][16][16];
482    
483     Int_t tmp;
484     //
485     for(Int_t gg = 0; gg < 2; gg++)
486     {
487     Hitstatus[gg] = 0;
488     }
489     //
490     //fetch data
491     //
492     for(Int_t kk = 0; kk < 2; kk++)
493     {
494     if(re>0)
495     {
496     Trigg_old[kk] = Trigger[kk];
497     for(Int_t jj = 0; jj < 16; jj++)
498     Counter_old[kk][jj] = Counter[kk][jj];
499     }
500    
501     for(Int_t jj = 0; jj < 16; jj++)
502     {
503     Counter[kk][jj] = acc->counters[kk][jj];
504     Shift[kk][jj] = acc->shift[kk][jj];
505     }
506     Status[kk] = acc->status[kk];
507     Hitmap[kk] = acc->hitmap[kk];
508     Trigger[kk] = acc->trigg[kk];
509     CRCcheck[kk] = acc->CRCcheck[kk];
510     }
511    
512     /***********************************************/
513    
514    
515     //process data
516     /***********************************************/
517     //shiftregisters
518     for(Int_t b = 0; b < 2; b++){ //card
519     for(Int_t k = 0; k < 16; k++){ //shift register
520     Int_t cnt=1;
521     for(Int_t l = 0; l < 16; l++){ //bin
522     tmp = ((Shift[b][k] & cnt) > 0 ? 1 : 0);
523     vec[b][k][l]=tmp;
524     cnt=cnt<<1;
525     }
526     //cnt=1;
527     }
528     }
529    
530     //fill Level1 file
531     /************************************************/
532     for(Int_t s = 0; s < 2; s++) {
533     acl2->hitmap[s] = Hitmap[s];
534     acl2->trigger[s] = Trigger[s];
535     Int_t cnt=1;
536     for(Int_t k = 0; k < 16; k++) {
537     for(Int_t bin = 5; bin < 9; bin++) { //acceptance window
538     if(vec[s][bin][k]==1)
539     Hitstatus[s] = Hitstatus[s] | cnt;
540     }
541     cnt=cnt<<1;
542     }
543     acl2->hitstatus[s] = Hitstatus[s];
544    
545     //Status
546     /****************************************/
547     if(s==0){
548     if(Trigger[0] != (Trigg_old[0]+1))
549     acl2->status[0] = acl2->status[0] | 0x1;
550     if(Status[0] & 0x001F < 0x001F)
551     acl2->status[0] = acl2->status[0] | 0x2;
552     if(CRCcheck[0] == 0)
553     acl2->status[0] = acl2->status[0] | 0x4;
554     for(Int_t gg = 0; gg < 16; gg++){
555     if((Counter[0][gg] == Counter_old[0][gg]) && ei>0){
556     acl2->status[0] = acl2->status[0] | 0x8;
557     }
558     }
559     }
560     else {
561     if(Trigger[1] != (Trigg_old[1]+2))
562     acl2->status[1] = acl2->status[1] | 0x1;
563     if(Status[1] & 0x001F < 0x001F)
564     acl2->status[1] = acl2->status[1] | 0x2;
565     if(CRCcheck[1] == 0)
566     acl2->status[1] = acl2->status[1] | 0x4;
567     for(Int_t gg = 0; gg < 16; gg++){
568     if((Counter[1][gg] == Counter_old[1][gg]) && ei>0)
569     acl2->status[1] = acl2->status[1] | 0x8;
570     }
571     }
572     }
573     //acl2->OBT = ph->GetOrbitalTime();
574     //acl2->pkt_num = re+1;
575     //acl2->pro_num = ph->GetCounter();
576     //
577     // End Anticounter specific code
578     //
579     /************************************************/
580    
581     //
582     // Fill the rootple
583     //
584     ac->Fill();
585     //
586     //
587     jumpev:
588     debug = false;
589     //
590     };
591     //
592     // Here you may want to clear some variables before processing another run
593     //
594     ei = 0;
595     }; // process all the runs
596     //
597     if ( verbose ) printf("\n Finished processing data \n");
598     //
599     closeandexit:
600     //
601     // 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.
602     //
603     if ( !reprocall && reproc && code >= 0 ){
604     if ( totfileentries > noaftrun ){
605     if ( verbose ){
606     printf("\n Post-processing: copying events from the old tree after the processed run\n");
607     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
608     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
609     };
610     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
611     //
612     // Get entry from old tree
613     //
614     acclone->GetEntry(j);
615     //
616     // copy acl2clone to acl2
617     //
618 mocchiut 1.3 // acl2 = new AcLevel2();
619     acl2->Clear();
620 mocchiut 1.1 memcpy(&acl2,&acl2clone,sizeof(acl2clone));
621     //
622     // Fill entry in the new tree
623     //
624     ac->Fill();
625     };
626     if ( verbose ) printf(" Finished successful copying!\n");
627     };
628     };
629     //
630     // Close files, delete old tree(s), write and close level2 file
631     //
632     if ( l0File ) l0File->Close();
633     if ( tempfile ) tempfile->Close();
634     gSystem->Unlink(tempname.str().c_str());
635     //
636     if ( code < 0 && verbose ) printf("\n ANTICOUNTER - ERROR: an error occurred, try to save anyway...\n");
637     if ( verbose ) printf("\n Writing and closing rootple\n");
638     if ( runinfo ) runinfo->Close();
639     if ( ac ) ac->SetName("Anticounter");
640     if ( file ){
641     file->cd();
642     file->Write();
643     };
644     //
645     gSystem->Unlink(acfolder.str().c_str());
646     //
647     // the end
648     //
649     if ( verbose ) printf("\n Exiting...\n");
650     if( ac ) ac->Delete();
651     if(code < 0) throw code;
652     return(code);
653     }
654    
655    
656    

  ViewVC Help
Powered by ViewVC 1.1.23