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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download)
Tue May 22 08:15:04 2007 UTC (17 years, 9 months ago) by mocchiut
Branch: MAIN
CVS Tags: v3r06
Changes since 1.13: +2 -1 lines
Reprocessing tool tuning

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

  ViewVC Help
Powered by ViewVC 1.1.23