/[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.6 - (show annotations) (download)
Thu Nov 9 17:05:43 2006 UTC (18 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.5: +1 -0 lines
Added unpackError in all classes, ToF .rz bug fixed, install_GL_PARAM time screw bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23