/[PAMELA software]/DarthVader/CalorimeterLevel2/src/CaloCore.cpp
ViewVC logotype

Contents of /DarthVader/CalorimeterLevel2/src/CaloCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (show annotations) (download)
Fri Apr 27 09:53:14 2007 UTC (17 years, 7 months ago) by mocchiut
Branch: MAIN
CVS Tags: v3r04, v3r05, v3r03
Changes since 1.26: +2 -1 lines
Cosmetic changes

1 //
2 // Given a calibration and a data file this program create an ntuple with LEVEL2 calorimeter variables - Emiliano Mocchiutti
3 //
4 // CaloCore.cxx version 3.05 (2006-05-30)
5 //
6 // The only input needed is the path to the directory created by YODA for the data file you want to analyze.
7 //
8 // Changelog:
9 //
10 // 3.08 (2006-11-13): Added high energy nuclei capability and "process all events" capability.
11 //
12 // 3.04 - 3.05 (2006-05-30): Qlast and nlast are now calculated using 4 (not 8) strips aournd the shower axis. Small bug fixed.
13 //
14 // 3.03 - 3.04 (2006-05-23): Forgot to put impx and impy in the PAMELA reference system, fixed.
15 //
16 // 3.02 - 3.03 (2006-05-18): updated to be called in DarthVader. Output dimension are now in cm and in the PAMELA reference system.
17 //
18 // 3.01 - 3.02 (2006-04-21): when copying entries get size of caclone and not of ca... (shouldn't matter). Fixed increasing file dimension bug when reprocessing.
19 // Added variable planemax[2], plane of maximum energy release (x and y) in final output. Use ItoRunInfo instead of RunInfo.
20 //
21 // 3.00 - 3.01 (2006-04-14): fixed small bug in tagging the track used to determine track-related variables, put in caloprocessing the opening of parameters files, fixed
22 // small bug in fortran routines
23 //
24 // 2.01 - 3.00 (2006-04-14): almost everything has changed. Now it can process one, all or some runs, introduced the final CaloLevel2 class+methods and the
25 // working class "CaloProcessing", linked to the preliminary tracker flight software v0r00, reads YODA unique output files,
26 // reduced the number of installed libraries, F77 programs splitted depending on the function contained, introduced the readout and
27 // processing of self-trigger events, if the tracker provides more than one track all calorimeter track-related variables are saved
28 // as many times as the number of tracks (via the TClonesArray object in the level2 rootple) and many other small changes.
29 //
30 // 2.00 - 2.01 (2006-01-26): bug: wrong calculation of baselines in some cases, fixed.
31 //
32 // 1.00 - 2.00 (2006-01-11): use TSQL ROOT classes instead of directly calling MySQL.
33 //
34 // 0.00 - 1.00 (2005-09-14): seems working.
35 //
36 // 0.00 (2005-09-09): clone of CaloLEVEL2.c .
37 //
38 // C/C++ headers
39 //
40 #include <fstream>
41 #include <string.h>
42 //
43 // ROOT headers
44 //
45 #include <TTree.h>
46 #include <TClassEdit.h>
47 #include <TObject.h>
48 #include <TList.h>
49 #include <TArrayI.h>
50 #include <TSystem.h>
51 #include <TSystemDirectory.h>
52 #include <TString.h>
53 #include <TFile.h>
54 #include <TClass.h>
55 #include <TCanvas.h>
56 #include <TH1.h>
57 #include <TH1F.h>
58 #include <TH2D.h>
59 #include <TLatex.h>
60 #include <TPad.h>
61 #include <TSQLServer.h>
62 #include <TSQLRow.h>
63 #include <TSQLResult.h>
64 #include <TClonesArray.h>
65 #include <TStreamerInfo.h>
66 //
67 // This program headers
68 //
69 #include <RunInfo.h>
70 //
71 // YODA headers
72 //
73 #include <PamelaRun.h>
74 #include <physics/trigger/TriggerEvent.h>
75 //
76 // This program headers
77 //
78 #include <CaloCore.h>
79 #include <CaloLevel1.h>
80 #include <CaloLevel2.h>
81 #include <CaloLevel0.h>
82 #include <CaloVerl2.h>
83 //
84 // Tracker classes headers and definitions
85 //
86 #include <TrkLevel2.h>
87 //
88 using namespace std;
89 //
90 // CORE ROUTINE
91 //
92 int CaloCore(UInt_t run, TFile *file, TSQLServer *dbc, Int_t calargc, char *calargv[]){
93 //
94 // Set these to true to have a very verbose output.
95 //
96 Bool_t verbose = false;
97 Bool_t debug = false;
98 //
99 Bool_t crosst = true;
100 //
101 Bool_t trackanyway = true;
102 //
103 Float_t rigdefault = 50.;
104 //
105 Bool_t hZn = true;
106 //
107 Bool_t withtrk = true;
108 //
109 Bool_t st = true;
110 //
111 Bool_t getl1 = true;
112 //
113 Bool_t mechal = false;
114 //
115 // Output directory is the working directoy.
116 //
117 const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
118 //
119 Int_t ri = 0;
120 TString processFolder = Form("calorimeterFolder_%u",run);
121 if ( calargc > 0 ){
122 ri = 0;
123 while ( ri < calargc ){
124 if ( !strcmp(calargv[ri],"-processFolder") ) {
125 if ( calargc < ri+1 ){
126 throw -3;
127 };
128 processFolder = (TString)calargv[ri+1];
129 ri++;
130 };
131 if ( !strcmp(calargv[ri],"-v") || !strcmp(calargv[ri],"--verbose") ) {
132 verbose = true;
133 };
134 if ( !strcmp(calargv[ri],"-g") || !strcmp(calargv[ri],"--debug") ) {
135 verbose = true;
136 debug = true;
137 };
138 if ( !strcmp(calargv[ri],"--alltracks") ) {
139 trackanyway = true;
140 };
141 if ( !strcmp(calargv[ri],"--use-default-alig") ) {
142 mechal = true;
143 };
144 if ( !strcmp(calargv[ri],"--no-crosstalk") ) {
145 crosst = false;
146 };
147 if ( !strcmp(calargv[ri],"--no-tracker") ) {
148 withtrk = false;
149 };
150 if ( !strcmp(calargv[ri],"--with-tracker") ) {
151 withtrk = true;
152 };
153 if ( !strcmp(calargv[ri],"--defrig") ) {
154 if ( calargc < ri+1 ){
155 throw -3;
156 };
157 rigdefault = atof(calargv[ri+1]);
158 ri++;
159 };
160 if ( !strcmp(calargv[ri],"--no-alltracks") ) {
161 trackanyway = false;
162 };
163 if ( !strcmp(calargv[ri],"--highZnuclei") ) {
164 hZn = true;
165 };
166 if ( !strcmp(calargv[ri],"--no-highZnuclei") ) {
167 hZn = false;
168 };
169 if ( !strcmp(calargv[ri],"--selftrigger") ) {
170 st = true;
171 };
172 if ( !strcmp(calargv[ri],"--no-selftrigger") ) {
173 st = false;
174 };
175 if ( !strcmp(calargv[ri],"--no-level1") ) {
176 getl1 = false;
177 };
178 if ( !strcmp(calargv[ri],"--help") ) {
179 printf("\n\n CALORIMETER HELP CALLED\n\n");
180 printf(" CaloCore options: \n");
181 printf(" -v | --verbose be verbose\n");
182 printf(" -g | --debug be really verbose\n");
183 printf(" --defrig rig rig is the default rigidity in GV to be used to\n");
184 printf(" obtain calorimeter variables in the routines\n");
185 printf(" \"alltracks\" and \"higZnuclei\" [default = 50]\n");
186 printf(" --alltracks fill the track related variables even in the case\n");
187 printf(" of no tracks from tracker and no selftrigger event\n");
188 printf(" when we have a calorimeter fit for both views [default]\n");
189 printf(" --no-alltracks fill the track related variables only in the case\n");
190 printf(" of a good track from tracker or selftrigger\n");
191 printf(" --highZnuclei call the routine to analyze high Z nuclei\n");
192 printf(" selftrigger events [default]\n");
193 printf(" --no-highZnuclei do not call the routine to analyze high Z nuclei\n");
194 printf(" selftrigger events\n");
195 printf(" --no-tracker do not use tracker level2\n");
196 printf(" --with-tracker use tracker level2 [default]\n");
197 printf(" --no-crosstalk do not apply crosstalk corrections\n");
198 printf(" --selftrigger process selftrigger events [default]\n");
199 printf(" --no-selftrigger skip selftrigger events\n");
200 printf(" --no-level1 do not save Level1 TBranch\n");
201 printf(" --use-default-alig use default mechanical alignement (defined in CaloLevel1.h)\n");
202 throw -114;
203 };
204 ri++;
205 };
206 };
207 //
208 if ( verbose ){
209 printf("\n");
210 if ( getl1 ) printf(" Saving calorimeter level1 data \n");
211 if ( st ) printf(" Calling selftrigger subroutine \n");
212 if ( hZn ) printf(" Calling high energy nuclei subroutine \n");
213 if ( trackanyway ) printf(" Filling track related variables for all the possible tracks \n");
214 if ( hZn || trackanyway ) printf(" => default assumed rigidity %f \n",rigdefault);
215 if ( withtrk ) printf(" Using tracker level2 data \n");
216 if ( mechal ) printf(" Using default mechanical alignement (defined in CaloLevel1.h)\n");
217 printf("\n");
218 };
219 //
220 // Working filename
221 //
222 TString outputfile;
223 stringstream name;
224 name.str("");
225 name << outdir << "/";
226 //
227 // Variables.
228 //
229 TTree *tracker = 0;
230 TTree *calo = 0;
231 TTree *caloclone = 0;
232 Bool_t reproc = false;
233 Bool_t reprocall = false;
234 UInt_t nevents = 0;
235 UInt_t nobefrun = 0;
236 UInt_t noaftrun = 0;
237 UInt_t numbofrun = 0;
238 UInt_t totnorun = 0;
239 //
240 Int_t code = 0;
241 Int_t sgnl;
242 //
243 // calorimeter level2 classes
244 //
245 CaloLevel1 *c1 = 0;
246 CaloLevel1 *c1clone = 0;
247 if ( getl1 ){
248 c1 = new CaloLevel1();
249 c1clone = new CaloLevel1();
250 };
251 //
252 // calorimeter level2 classes
253 //
254 CaloLevel2 *ca = new CaloLevel2();
255 CaloLevel2 *caclone = new CaloLevel2();
256 //
257 TrkLevel2 *trk = new TrkLevel2();
258 Int_t nevtrkl2 = 0;
259 //
260 UInt_t procev = 0;
261 //
262 // define variables where to store the absolute run header and run trailer times (unsigned long long integers, when set to a number use to store the correct number).
263 //
264 UInt_t runheadtime = 0;
265 UInt_t runtrailtime = 0;
266 UInt_t evfrom = 0;
267 UInt_t evto = 0;
268 UInt_t totfileentries = 0;
269 UInt_t idRun = 0;
270 Int_t id_reg_run=-1;
271 stringstream ftmpname;
272 TString fname;
273 //
274 // define variables for opening and reading level0 file
275 //
276 TFile *l0File = 0;
277 TTree *l0tr = 0;
278 TTree *softinfo = 0;
279 TBranch *l0head = 0;
280 TBranch *l0calo = 0;
281 TBranch *l0trig = 0;
282 pamela::EventHeader *eh = 0;
283 pamela::PscuHeader *ph = 0;
284 pamela::trigger::TriggerEvent *trig = 0;
285 Int_t yodaver = 0;
286 //
287 // Define some basic variables
288 //
289 CaloLevel0 *event = new CaloLevel0(); // NOTICE: very important to call here the constructor!
290 event->SetCrossTalk(crosst);
291 stringstream file2;
292 stringstream file3;
293 stringstream qy;
294 // Bool_t imtrack = false;
295 Bool_t filled = false;
296 //
297 UInt_t caloevents = 0;
298 stringstream calfile;
299 stringstream aligfile;
300 //
301 Int_t i = -1;
302 Int_t itr = -1;
303 Int_t badevent = 0;
304 Int_t totevent = 0;
305 //
306 UInt_t atime = 0;
307 //
308 Int_t S3 = 0;
309 Int_t S2 = 0;
310 Int_t S12 = 0;
311 Int_t S11 = 0;
312 UInt_t re = 0;
313 UInt_t jumped = 0;
314 //
315 TString caloversion;
316 ItoRunInfo *runinfo = 0;
317 TArrayI *runlist = 0;
318 //
319 Float_t tmptrigty = -1.;
320 Int_t ntrkentry = 0;
321 GL_PARAM *q4 = new GL_PARAM();
322 UInt_t tttrkpar1 = 0;
323 Bool_t trkpar1 = true;
324 GL_ROOT *glroot = new GL_ROOT();
325 GL_TIMESYNC *dbtime = 0;
326 //
327 Long64_t maxsize = 10000000000LL;
328 TTree::SetMaxTreeSize(maxsize);
329 //
330 //
331 TFile *tempfile = 0;
332 TTree *tempcalo = 0;
333 stringstream tempname;
334 stringstream calofolder;
335 tempname.str("");
336 tempname << outdir;
337 tempname << "/" << processFolder.Data();
338 calofolder.str("");
339 calofolder << tempname.str().c_str();
340 gSystem->MakeDirectory(calofolder.str().c_str());
341 tempname << "/calotree_run";
342 tempname << run << ".root";
343 //
344 // As a first thing we must check what we have to do: if run = -1 we must process all events in the file has been passed
345 // if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file
346 // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
347 //
348 if ( run == 0 ) reproc = true;
349 //
350 //
351 //
352 if ( !file->IsOpen() ){
353 if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n");
354 throw -101;
355 };
356 //
357 if ( withtrk ){
358 //
359 // Does it contain the Tracker tree?
360 //
361 tracker = (TTree*)file->Get("Tracker");
362 if ( !tracker ) {
363 if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n");
364 code = -102;
365 goto closeandexit;
366 };
367 tracker->SetBranchAddress("TrkLevel2",&trk);
368 nevtrkl2 = tracker->GetEntries();
369 };
370 //
371 // Call runinfo
372 //
373 sgnl = 0;
374 runinfo = new ItoRunInfo(file);
375 //
376 // update versioning information and retrieve informations about the run to be processed
377 //
378 caloversion = CaloInfo(false);
379 sgnl = runinfo->Update(run,"CALO",caloversion);
380 //
381 if ( sgnl ){
382 if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
383 code = sgnl;
384 goto closeandexit;
385 } else {
386 sgnl = 0;
387 };
388 //
389 // number of events in the file BEFORE the first event of our run
390 //
391 nobefrun = runinfo->GetFirstEntry();
392 //
393 // total number of events in the file
394 //
395 totfileentries = runinfo->GetFileEntries();
396 //
397 // first file entry AFTER the last event of our run
398 //
399 noaftrun = runinfo->GetLastEntry() + 1;
400 //
401 // number of run to be processed
402 //
403 numbofrun = runinfo->GetNoRun();
404 //
405 // number of runs in the file
406 //
407 totnorun = runinfo->GetRunEntries();
408 //
409 // Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it.
410 //
411 caloclone = (TTree*)file->Get("Calorimeter");
412 //
413 if ( !caloclone ){
414 reproc = false;
415 if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n");
416 if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" CALORIMETER - WARNING: it seems you are not reprocessing data but calorimeter\n versioning information already exists in RunInfo.\n");
417 //
418 } else {
419 //
420 caloclone->SetAutoSave(900000000000000LL);
421 reproc = true;
422 //
423 if ( verbose ) printf("\n Preparing the pre-processing...\n");
424 //
425 if ( run == 0 ){
426 //
427 // if we are reprocessing everything we don't need to copy any old event and we can just create a new branch in the clone tree and jump steps 4/5/7.
428 //
429 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n");
430 reprocall = true;
431 //
432 } else {
433 //
434 // we are reprocessing a single run
435 //
436 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run);
437 reprocall = false;
438 //
439 //
440 //
441 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
442 tempcalo = caloclone->CloneTree(-1,"fast");
443 tempcalo->SetName("Calorimeter-old");
444 tempfile->Write();
445 tempfile->Close();
446 };
447 //
448 // delete old tree
449 //
450 caloclone->Delete("all");
451 //
452 if ( verbose ) printf("\n ...done!\n");
453 //
454 };
455 //
456 // create calorimeter tree calo
457 //
458 file->cd();
459 calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data");
460 calo->SetAutoSave(900000000000000LL);
461 ca->Set();
462 calo->Branch("CaloLevel2","CaloLevel2",&ca);
463 if ( getl1 ) calo->Branch("CaloLevel1","CaloLevel1",&c1);
464 //
465 if ( reproc && !reprocall ){
466 //
467 //
468 //
469 tempfile = new TFile(tempname.str().c_str(),"READ");
470 caloclone = (TTree*)tempfile->Get("Calorimeter-old");
471 caloclone->SetAutoSave(900000000000000LL);
472 caloclone->SetBranchAddress("CaloLevel2",&caclone);
473 TBranch *havel1 = caloclone->GetBranch("CaloLevel1");
474 if ( !havel1 && getl1 ) throw -117;
475 if ( havel1 && !getl1 ) throw -118;
476 if ( getl1 ) caloclone->SetBranchAddress("CaloLevel1",&c1clone);
477 //
478 if ( nobefrun > 0 ){
479 if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
480 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
481 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
482 for (UInt_t j = 0; j < nobefrun; j++){
483 //
484 caloclone->GetEntry(j);
485 //
486 // copy caclone to ca
487 //
488 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
489 memcpy(&ca,&caclone,sizeof(caclone));
490 //
491 // Fill entry in the new tree
492 //
493 calo->Fill();
494 //
495 if ( getl1 ) c1->Clear();
496 ca->Clear();
497 //
498 };
499 if ( verbose ) printf(" Finished successful copying!\n");
500 };
501 };
502 //
503 // Get the list of run to be processed
504 //
505 runlist = runinfo->GetRunList();
506 //
507 // Loop over the run to be processed
508 //
509 for (UInt_t irun=0; irun < numbofrun; irun++){
510 //
511 badevent = 0;
512 //
513 idRun = runlist->At(irun);
514 if ( verbose ) printf("\n\n\n ####################################################################### \n");
515 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
516 if ( verbose ) printf(" ####################################################################### \n\n\n");
517 //
518 sgnl = runinfo->GetRunInfo(idRun);
519 if ( sgnl ){
520 if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
521 code = sgnl;
522 goto closeandexit;
523 } else {
524 sgnl = 0;
525 };
526 id_reg_run = runinfo->ID_ROOT_L0;
527 runheadtime = runinfo->RUNHEADER_TIME;
528 runtrailtime = runinfo->RUNTRAILER_TIME;
529 evfrom = runinfo->EV_FROM;
530 evto = runinfo->EV_TO;
531 //
532 if ( id_reg_run == -1 ){
533 if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
534 code = -5;
535 goto closeandexit;
536 };
537 //
538 // prepare the timesync for the db
539 //
540 if ( !dbc->IsConnected() ) throw -116;
541 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
542 //
543 // Search in the DB the path and name of the LEVEL0 file to be processed.
544 //
545 if ( !dbc->IsConnected() ) throw -116;
546 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
547 //
548 ftmpname.str("");
549 ftmpname << glroot->PATH.Data() << "/";
550 ftmpname << glroot->NAME.Data();
551 fname = ftmpname.str().c_str();
552 //
553 // print out informations
554 //
555 totevent = runinfo->NEVENTS;
556 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
557 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime);
558 if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime);
559 if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent);
560 //
561 // Open Level0 file
562 //
563 l0File = new TFile(fname.Data());
564 if ( !l0File ) {
565 if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n");
566 code = -6;
567 goto closeandexit;
568 };
569 l0tr = (TTree*)l0File->Get("Physics");
570 if ( !l0tr ) {
571 if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n");
572 l0File->Close();
573 code = -7;
574 goto closeandexit;
575 };
576 l0head = l0tr->GetBranch("Header");
577 if ( !l0head ) {
578 if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n");
579 l0File->Close();
580 code = -8;
581 goto closeandexit;
582 };
583 l0calo = l0tr->GetBranch("Calorimeter");
584 if ( !l0calo ) {
585 if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n");
586 l0File->Close();
587 code = -103;
588 goto closeandexit;
589 };
590 l0trig = l0tr->GetBranch("Trigger");
591 if ( !l0trig ) {
592 if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n");
593 l0File->Close();
594 code = -104;
595 goto closeandexit;
596 };
597 //
598 l0tr->SetBranchAddress("Trigger", &trig);
599 l0tr->SetBranchAddress("Header", &eh);
600 //
601 softinfo = (TTree*)l0File->Get("SoftInfo");
602 if ( softinfo ){
603 softinfo->SetBranchAddress("SoftInfo",&yodaver);
604 softinfo->GetEntry(0);
605 };
606 if ( debug ) printf(" LEVEL0 FILE GENERATED WITH YODA VERSION %i \n",yodaver);
607 //
608 // Construct the event object, look for the calibration which include the first header
609 //
610 sgnl = 0;
611 if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n");
612 if ( !dbc->IsConnected() ) throw -116;
613 event->ProcessingInit(dbc,runheadtime,sgnl,l0tr,debug,verbose);
614 if ( verbose ) printf("\n");
615 if ( sgnl == 100 ) {
616 code = sgnl;
617 if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n");
618 sgnl = 0;
619 };
620 if ( sgnl ){
621 l0File->Close();
622 code = sgnl;
623 goto closeandexit;
624 };
625 //
626 qy.str("");
627 //
628 nevents = l0head->GetEntries();
629 caloevents = l0calo->GetEntries();
630 //
631 if ( nevents < 1 ) {
632 if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n");
633 l0File->Close();
634 code = -11;
635 goto closeandexit;
636 };
637 //
638 if ( evto > nevents-1 ) {
639 if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n");
640 l0File->Close();
641 code = -12;
642 goto closeandexit;
643 };
644 //
645 // Check if we have to load parameter files
646 //
647 sgnl = 0;
648 if ( !dbc->IsConnected() ) throw -116;
649 sgnl = event->ChkParam(dbc,runheadtime,mechal); // calorimeter parameter files
650 if ( sgnl < 0 ){
651 code = sgnl;
652 l0File->Close();
653 goto closeandexit;
654 };
655 //
656 if ( withtrk ){
657 if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){
658 trkpar1 = false;
659 if ( !dbc->IsConnected() ) throw -116;
660 Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc);
661 if ( glpar < 0 ){
662 code = glpar;
663 goto closeandexit;
664 };
665 tttrkpar1 = q4->TO_TIME;
666 // ----------------------------
667 // Read the magnetic field
668 // ----------------------------
669 if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
670 trk->LoadField(q4->PATH+q4->NAME);
671 if ( verbose ) printf("\n");
672 };
673 };
674 //
675 // run over all the events of the run
676 //
677 if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
678 //
679 //
680 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
681 //for ( re = 46438+200241; re < 46441+200241; re++){
682 // printf(" i = %i \n",re-200241);
683 //
684 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
685 //
686 l0head->GetEntry(re);
687 //
688 // absolute time of this event
689 //
690 ph = eh->GetPscuHeader();
691 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
692 //
693 //
694 //
695 if ( re > caloevents-1 ){
696 if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i);
697 l0File->Close();
698 code = -112;
699 goto closeandexit;
700 };
701 //
702 if ( atime > runtrailtime || atime < runheadtime ) {
703 if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n");
704 jumped++;
705 goto jumpev;
706 };
707 //
708 // retrieve tracker informations
709 //
710 if ( !reprocall ){
711 itr = nobefrun + (re - evfrom - jumped);
712 //itr = re-(46438+200241);
713 } else {
714 itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
715 };
716 //
717 if ( withtrk ){
718 if ( itr > nevtrkl2 ){
719 if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
720 l0File->Close();
721 code = -113;
722 goto closeandexit;
723 };
724 //
725 trk->Clear();
726 //
727 tracker->GetEntry(itr);
728 //
729 };
730 //
731 procev++;
732 //
733 // start processing
734 //
735 if ( getl1 ) c1->Clear();
736 ca->Clear();
737 //
738 // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
739 //
740 l0trig->GetEntry(re);
741 S3 = 0;
742 S2 = 0;
743 S12 = 0;
744 S11 = 0;
745 S3 = trig->patterntrig[2];
746 S2 = trig->patterntrig[3];
747 S12 = trig->patterntrig[4];
748 S11 = trig->patterntrig[5];
749 if ( trig->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
750 if ( trig->patterntrig[0] ) tmptrigty = 2.;
751 if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
752 if ( !(trig->patterntrig[1] & (1<<0)) && !trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
753 event->clevel2->trigty = tmptrigty;
754 //
755 // check if the calibration we are using is still good, if not load another calibration
756 //
757 sgnl = 0;
758 sgnl = event->ChkCalib(dbc,atime);
759 if ( sgnl < 0 ){
760 code = sgnl;
761 goto closeandexit;
762 };
763 if ( sgnl == 100 ){
764 code = sgnl;
765 if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n");
766 badevent++;
767 sgnl = 0;
768 };
769 //
770 // do we have at least one track from the tracker? this check has been disabled
771 //
772 event->clevel1->good2 = 1;
773 //
774 //
775 // Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine
776 //
777 event->Calibrate(re);
778 //
779 // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables.
780 //
781 //
782 // Calculate variables common to all tracks (qtot, nstrip, etc.)
783 //
784 event->GetCommonVar();
785 //
786 // Fill common variables
787 //
788 event->FillCommonVar(c1,ca);
789 //
790 // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
791 //
792 ntrkentry = 0;
793 //
794 filled = false;
795 //
796 // Run over tracks (tracker or calorimeter )
797 //
798 if ( withtrk ){
799 for(Int_t nt=0; nt < trk->ntrk(); nt++){
800 //
801 event->clevel1->good2 = 1;
802 //
803 TrkTrack *ptt = trk->GetStoredTrack(nt);
804 //
805 event->clevel1->trkchi2 = 0;
806 //
807 // Copy the alpha vector in the input structure
808 //
809 for (Int_t e = 0; e < 5 ; e++){
810 event->clevel1->al_p[e][0] = ptt->al[e];
811 };
812 //
813 // Get tracker related variables for this track
814 //
815 event->GetTrkVar();
816 //
817 // Save tracker track sequence number
818 //
819 event->trkseqno = nt;
820 //
821 // Copy values in the class ca from the structure clevel2
822 //
823 event->FillTrkVar(ca,ntrkentry);
824 ntrkentry++;
825 filled = true;
826 //
827 }; // loop on all the tracks
828 };
829 //
830 // if no tracks found but there is the possibility to have a good track we should try to calculate anyway the track related variables using the calorimeter
831 // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
832 // here we make an event selection so it must be done very carefully...
833 //
834 // conditions are: 0) no track from the tracker 1) we have a track fit both in x and y 2) no problems with calo for this event 3) no selftrigger event
835 //
836 if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
837 if ( debug ) printf(" Event with a track not fitted by the tracker at entry %i \n",itr);
838 //
839 // Disable "track mode" in the fortran routine
840 //
841 event->clevel1->good2 = 0;
842 event->clevel1->riginput = rigdefault;
843 if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
844 //
845 // We have a selftrigger event to analyze.
846 //
847 for (Int_t e = 0; e < 5 ; e++){
848 event->clevel1->al_p[e][0] = 0.;
849 event->clevel1->al_p[e][1] = 0.;
850 };
851 event->clevel1->trkchi2 = 0;
852 //
853 event->GetTrkVar();
854 //
855 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
856 //
857 if ( event->clevel1->good2 == 0 ) {
858 //
859 // In selftrigger mode the trkentry variable is set to -1
860 //
861 event->trkseqno = -3;
862 //
863 // Copy values in the class ca from the structure clevel2
864 //
865 event->FillTrkVar(ca,ntrkentry);
866 ntrkentry++;
867 filled = true;
868 //
869 } else {
870 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
871 };
872 //
873 };
874 //
875 // Call high energy nuclei routine
876 //
877 if ( hZn && event->clevel2->trigty >= 2. ){
878 if ( debug ) printf(" Calling selftrigger high energy nuclei routine for entry %i \n",itr);
879 //
880 // Disable "track mode" in the fortran routine
881 //
882 event->clevel1->good2 = 0;
883 //
884 // Set high energy nuclei flag to one
885 //
886 event->clevel1->hzn = 1;
887 event->clevel1->riginput = rigdefault;
888 //
889 // We have a selftrigger event to analyze.
890 //
891 for (Int_t e = 0; e < 5 ; e++){
892 event->clevel1->al_p[e][0] = 0.;
893 event->clevel1->al_p[e][1] = 0.;
894 };
895 event->clevel1->trkchi2 = 0;
896 //
897 event->GetTrkVar();
898 //
899 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
900 //
901 if ( event->clevel1->good2 == 0 ) {
902 //
903 // In selftrigger mode the trkentry variable is set to -1
904 //
905 event->trkseqno = -2;
906 //
907 // Copy values in the class ca from the structure clevel2
908 //
909 event->FillTrkVar(ca,ntrkentry);
910 ntrkentry++;
911 filled = true;
912 //
913 } else {
914 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
915 };
916 //
917 };
918 //
919 // self trigger event
920 //
921 if ( st && event->clevel2->trigty >= 2. ){
922 if ( verbose ) printf(" Selftrigger event at entry %i \n",itr);
923 //
924 // Disable "track mode" in the fortran routine
925 //
926 event->clevel1->good2 = 0;
927 //
928 // disable high enery nuclei flag;
929 //
930 event->clevel1->hzn = 0;
931 //
932 // We have a selftrigger event to analyze.
933 //
934 for (Int_t e = 0; e < 5 ; e++){
935 event->clevel1->al_p[e][0] = 0.;
936 event->clevel1->al_p[e][1] = 0.;
937 };
938 event->clevel1->trkchi2 = 0;
939 //
940 event->GetTrkVar();
941 //
942 // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
943 //
944 if ( event->clevel1->good2 == 0 ) {
945 //
946 // In selftrigger mode the trkentry variable is set to -1
947 //
948 event->trkseqno = -1;
949 //
950 // Copy values in the class ca from the structure clevel2
951 //
952 event->FillTrkVar(ca,ntrkentry);
953 ntrkentry++;
954 filled = true;
955 //
956 } else {
957 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
958 };
959 };
960 //
961 if ( !filled ) badevent++;
962 //
963 // Clear structures used to communicate with fortran
964 //
965 event->ClearStructs();
966 //
967 // Fill the rootple
968 //
969 calo->Fill();
970 //
971 // delete ca;
972 //
973 jumpev:
974 if ( !debug ) debug = false;
975 //
976 };
977 //
978 if ( verbose ) printf("\n SUMMARY:\n");
979 if ( verbose ) printf(" Total number of events: %i \n",totevent);
980 if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent);
981 if ( verbose ) printf(" Events without tracks: %i \n",badevent);
982 //
983 if ( badevent == totevent ){
984 if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun);
985 code = 101;
986 };
987 //
988 delete dbtime;
989 //
990 // Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software).
991 //
992 event->RunClose();
993 if ( l0File ) l0File->Close();
994 //
995 }; // process all the runs
996 //
997 if ( verbose ) printf("\n Finished processing data \n");
998 //
999 closeandexit:
1000 //
1001 if ( !reprocall && reproc && code >= 0 ){
1002 if ( totfileentries > noaftrun ){
1003 if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1004 if ( verbose ) printf(" Copying %u events in the file which are after the end of the run %u \n",(totfileentries-noaftrun),run);
1005 if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries);
1006 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1007 //
1008 // Get entry from old tree
1009 //
1010 caloclone->GetEntry(j);
1011 //
1012 // copy caclone to ca
1013 //
1014 if ( getl1 ) c1->Clear();
1015 ca->Clear();
1016 //
1017 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
1018 memcpy(&ca,&caclone,sizeof(caclone));
1019 //
1020 // Fill entry in the new tree
1021 //
1022 calo->Fill();
1023 //
1024 };
1025 if ( verbose ) printf(" Finished successful copying!\n");
1026 };
1027 };
1028 //
1029 // Case of no errors: close files, delete old tree(s), write and close level2 file
1030 //
1031 if ( l0File ) l0File->Close();
1032 if ( tempfile ) tempfile->Close();
1033 gSystem->Unlink(tempname.str().c_str());
1034 if ( tracker ) tracker->Delete();
1035 //
1036 if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n");
1037 if ( verbose ) printf("\n Writing and closing rootple\n");
1038 if ( runinfo ) runinfo->Close();
1039 if ( calo ) calo->SetName("Calorimeter");
1040 if ( file ){
1041 file->cd();
1042 file->Write();
1043 };
1044 if ( calo ) calo->Delete();
1045 //
1046 gSystem->Unlink(calofolder.str().c_str());
1047 //
1048 if ( ca ) delete ca;
1049 if ( caclone ) delete caclone;
1050 if ( c1 ) delete c1;
1051 if ( c1clone ) delete c1clone;
1052 if ( trk ) delete trk;
1053 if ( q4 ) delete q4;
1054 if ( glroot ) delete glroot;
1055 if ( runinfo ) delete runinfo;
1056 //
1057 // the end
1058 //
1059 if ( verbose ) printf("\n Exiting...\n");
1060 if ( code < 0 ) throw code;
1061 return(code);
1062 }
1063
1064
1065

  ViewVC Help
Powered by ViewVC 1.1.23