/[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.2 - (hide annotations) (download)
Wed Jun 21 08:15:05 2006 UTC (18 years, 5 months ago) by pam-se
Branch: MAIN
Changes since 1.1: +2 -0 lines
error in file AcCore.cpp corrected

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     acl2 = new AcLevel2();
307     memcpy(&acl2,&acl2clone,sizeof(acl2clone));
308     //
309     // Fill entry in the new tree
310     //
311     ac->Fill();
312     //
313     };
314     if ( verbose ) printf(" Finished successful copying!\n");
315     };
316     };
317     //
318     // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
319     //
320     runlist = runinfo->GetRunList();
321     //
322     // Loop over the run to be processed
323     //
324     for (UInt_t irun=0; irun < numbofrun; irun++){
325     //
326     // retrieve the first run ID to be processed using the RunInfo list
327     //
328     idRun = runlist->At(irun);
329     if ( verbose ){
330     printf("\n\n\n ####################################################################### \n");
331     printf(" PROCESSING RUN NUMBER %i \n",(int)idRun);
332     printf(" ####################################################################### \n\n\n");
333     };
334     //
335     runinfo->ID_REG_RUN = 0ULL;
336     //
337     // store in the runinfo class the GL_RUN variables for our run
338     //
339     sgnl = 0;
340     sgnl = runinfo->GetRunInfo(idRun);
341     if ( sgnl ){
342     if ( verbose ) printf(" ANTICOUNTER - ERROR: RunInfo exited with non-zero status\n");
343     code = sgnl;
344     goto closeandexit;
345     } else {
346     sgnl = 0;
347     };
348     //
349     // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN
350     //
351     if ( runinfo->ID_REG_RUN == 0 ){
352     if ( verbose ) printf("\n ANTICOUNTER - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun);
353     code = -5;
354     goto closeandexit;
355     };
356     //
357     // Search in the DB the path and name of the LEVEL0 file to be processed.
358     //
359     glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc);
360     //
361     ftmpname.str("");
362     ftmpname << glroot->PATH.Data() << "/";
363     ftmpname << glroot->NAME.Data();
364     fname = ftmpname.str().c_str();
365     //
366     // print out informations
367     //
368     totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1;
369     if ( verbose ) {
370     printf("\n LEVEL0 data file: %s \n",fname.Data());
371     printf(" RUN HEADER absolute time is: %llu \n",runinfo->RUNHEADER_TIME);
372     printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME);
373     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);
374     };
375     //
376     // Open Level0 file
377     //
378     l0File = new TFile(fname.Data());
379     if ( !l0File ) {
380     if ( verbose ) printf(" ANTICOUNTER - ERROR: problems opening Level0 file\n");
381     code = -6;
382     goto closeandexit;
383     };
384     l0tr = (TTree*)l0File->Get("Physics");
385     if ( !l0tr ) {
386     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Physics tree in Level0 file\n");
387     l0File->Close();
388     code = -7;
389     goto closeandexit;
390     };
391     l0head = l0tr->GetBranch("Header");
392     if ( !l0head ) {
393     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Header branch in Level0 tree\n");
394     l0File->Close();
395     code = -8;
396     goto closeandexit;
397     };
398     l0registry = l0tr->GetBranch("Registry");
399     if ( !l0registry ) {
400     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Registry branch in Level0 tree\n");
401     l0File->Close();
402     code = -9;
403     goto closeandexit;
404     };
405     l0ac = l0tr->GetBranch("Anticounter");
406     if ( !l0ac ) {
407     if ( verbose ) printf(" ANTICOUNTER - ERROR: no Anticounter branch in Level0 tree\n");
408     l0File->Close();
409     code = -704;
410     goto closeandexit;
411     };
412     //
413     //l0tr->SetBranchAddress("Trigger", &trig);
414     l0tr->SetBranchAddress("Anticounter", &acc);
415     l0tr->SetBranchAddress("Registry", &l0reg);
416     //
417     nevents = l0registry->GetEntries();
418     //
419     if ( nevents < 1 ) {
420     if ( verbose ) printf(" ANTICOUNTER - ERROR: Level0 file is empty\n\n");
421     l0File->Close();
422     code = -11;
423     goto closeandexit;
424     };
425     //
426     if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) {
427     if ( verbose ) printf(" ANTICOUNTER - ERROR: too few entries in the registry tree\n");
428     l0File->Close();
429     code = -12;
430     goto closeandexit;
431     };
432     //
433     // run over all the events of the run
434     //
435     if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
436     //
437     for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){
438     //
439     if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
440     //
441     l0registry->GetEntry(re);
442     //
443     // absolute time of this event
444     //
445     atime = l0reg->absTime;
446     //
447     // physics events is at entry number ei where
448     //
449     ei = l0reg->event;
450     //
451     // paranoid check
452     //
453     if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME ) {
454     if ( verbose ) printf(" ANTICOUNTER - WARNING: event at time outside the run time window, skipping it\n");
455     goto jumpev;
456     };
457     //
458     procev++;
459     //
460     // start processing
461     //
462     acl2 = new AcLevel2();
463     //
464 pam-se 1.2 l0ac->GetEntry(ei);
465     //
466 mocchiut 1.1 /***********************************************/
467     // Here starts Anticounter specific code (Silvio)
468     //
469     UShort_t CRCcheck[2];
470     UShort_t Status[2];
471     UShort_t Hitmap[2];
472     UShort_t Hitstatus[2];
473     UShort_t Trigger[2];
474     UShort_t Trigg_old[2];
475     UShort_t Counter[2][16];
476     UShort_t Counter_old[2][16];
477    
478     UShort_t Shift[2][16];
479     Int_t vec[2][16][16];
480    
481     Int_t tmp;
482     //
483     for(Int_t gg = 0; gg < 2; gg++)
484     {
485     Hitstatus[gg] = 0;
486     }
487     //
488     //fetch data
489     //
490     for(Int_t kk = 0; kk < 2; kk++)
491     {
492     if(re>0)
493     {
494     Trigg_old[kk] = Trigger[kk];
495     for(Int_t jj = 0; jj < 16; jj++)
496     Counter_old[kk][jj] = Counter[kk][jj];
497     }
498    
499     for(Int_t jj = 0; jj < 16; jj++)
500     {
501     Counter[kk][jj] = acc->counters[kk][jj];
502     Shift[kk][jj] = acc->shift[kk][jj];
503     }
504     Status[kk] = acc->status[kk];
505     Hitmap[kk] = acc->hitmap[kk];
506     Trigger[kk] = acc->trigg[kk];
507     CRCcheck[kk] = acc->CRCcheck[kk];
508     }
509    
510     /***********************************************/
511    
512    
513     //process data
514     /***********************************************/
515     //shiftregisters
516     for(Int_t b = 0; b < 2; b++){ //card
517     for(Int_t k = 0; k < 16; k++){ //shift register
518     Int_t cnt=1;
519     for(Int_t l = 0; l < 16; l++){ //bin
520     tmp = ((Shift[b][k] & cnt) > 0 ? 1 : 0);
521     vec[b][k][l]=tmp;
522     cnt=cnt<<1;
523     }
524     //cnt=1;
525     }
526     }
527    
528     //fill Level1 file
529     /************************************************/
530     for(Int_t s = 0; s < 2; s++) {
531     acl2->hitmap[s] = Hitmap[s];
532     acl2->trigger[s] = Trigger[s];
533     Int_t cnt=1;
534     for(Int_t k = 0; k < 16; k++) {
535     for(Int_t bin = 5; bin < 9; bin++) { //acceptance window
536     if(vec[s][bin][k]==1)
537     Hitstatus[s] = Hitstatus[s] | cnt;
538     }
539     cnt=cnt<<1;
540     }
541     acl2->hitstatus[s] = Hitstatus[s];
542    
543     //Status
544     /****************************************/
545     if(s==0){
546     if(Trigger[0] != (Trigg_old[0]+1))
547     acl2->status[0] = acl2->status[0] | 0x1;
548     if(Status[0] & 0x001F < 0x001F)
549     acl2->status[0] = acl2->status[0] | 0x2;
550     if(CRCcheck[0] == 0)
551     acl2->status[0] = acl2->status[0] | 0x4;
552     for(Int_t gg = 0; gg < 16; gg++){
553     if((Counter[0][gg] == Counter_old[0][gg]) && ei>0){
554     acl2->status[0] = acl2->status[0] | 0x8;
555     }
556     }
557     }
558     else {
559     if(Trigger[1] != (Trigg_old[1]+2))
560     acl2->status[1] = acl2->status[1] | 0x1;
561     if(Status[1] & 0x001F < 0x001F)
562     acl2->status[1] = acl2->status[1] | 0x2;
563     if(CRCcheck[1] == 0)
564     acl2->status[1] = acl2->status[1] | 0x4;
565     for(Int_t gg = 0; gg < 16; gg++){
566     if((Counter[1][gg] == Counter_old[1][gg]) && ei>0)
567     acl2->status[1] = acl2->status[1] | 0x8;
568     }
569     }
570     }
571     //acl2->OBT = ph->GetOrbitalTime();
572     //acl2->pkt_num = re+1;
573     //acl2->pro_num = ph->GetCounter();
574     //
575     // End Anticounter specific code
576     //
577     /************************************************/
578    
579     //
580     // Fill the rootple
581     //
582     ac->Fill();
583     //
584     //
585     jumpev:
586     debug = false;
587     //
588     };
589     //
590     // Here you may want to clear some variables before processing another run
591     //
592     ei = 0;
593     }; // process all the runs
594     //
595     if ( verbose ) printf("\n Finished processing data \n");
596     //
597     closeandexit:
598     //
599     // 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.
600     //
601     if ( !reprocall && reproc && code >= 0 ){
602     if ( totfileentries > noaftrun ){
603     if ( verbose ){
604     printf("\n Post-processing: copying events from the old tree after the processed run\n");
605     printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
606     printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
607     };
608     for (UInt_t j = noaftrun; j < totfileentries; j++ ){
609     //
610     // Get entry from old tree
611     //
612     acclone->GetEntry(j);
613     //
614     // copy acl2clone to acl2
615     //
616     acl2 = new AcLevel2();
617     memcpy(&acl2,&acl2clone,sizeof(acl2clone));
618     //
619     // Fill entry in the new tree
620     //
621     ac->Fill();
622     };
623     if ( verbose ) printf(" Finished successful copying!\n");
624     };
625     };
626     //
627     // Close files, delete old tree(s), write and close level2 file
628     //
629     if ( l0File ) l0File->Close();
630     if ( tempfile ) tempfile->Close();
631     gSystem->Unlink(tempname.str().c_str());
632     //
633     if ( code < 0 && verbose ) printf("\n ANTICOUNTER - ERROR: an error occurred, try to save anyway...\n");
634     if ( verbose ) printf("\n Writing and closing rootple\n");
635     if ( runinfo ) runinfo->Close();
636     if ( ac ) ac->SetName("Anticounter");
637     if ( file ){
638     file->cd();
639     file->Write();
640     };
641     //
642     gSystem->Unlink(acfolder.str().c_str());
643     //
644     // the end
645     //
646     if ( verbose ) printf("\n Exiting...\n");
647     if( ac ) ac->Delete();
648     if(code < 0) throw code;
649     return(code);
650     }
651    
652    
653    

  ViewVC Help
Powered by ViewVC 1.1.23