/[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.45 - (show annotations) (download)
Fri Nov 28 09:05:18 2008 UTC (16 years, 3 months ago) by mocchiut
Branch: MAIN
CVS Tags: v6r01, v6r00
Changes since 1.44: +1 -1 lines
Event loss due to timesync bug fixed + ToF dE/dx II order correction implemented

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, GL_TABLES *glt, 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 Bool_t ctground = false;
101 Bool_t usetable = true;
102 Bool_t noselfct = false;
103 //
104 Bool_t trackanyway = true;
105 //
106 Float_t rigdefault = 50.;
107 //
108 Bool_t hZn = true;
109 //
110 Bool_t withtrk = true;
111 //
112 Bool_t st = true;
113 //
114 Bool_t getl1 = true;
115 //
116 Bool_t mechal = false;
117 //
118 Bool_t checksimu = true;
119 //
120 // Output directory is the working directoy.
121 //
122 const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
123 //
124 Int_t ri = 0;
125 TString processFolder = Form("calorimeterFolder_%u",run);
126 if ( calargc > 0 ){
127 ri = 0;
128 while ( ri < calargc ){
129 if ( !strcmp(calargv[ri],"-processFolder") ) {
130 if ( calargc < ri+1 ){
131 throw -3;
132 };
133 processFolder = (TString)calargv[ri+1];
134 ri++;
135 };
136 if ( !strcmp(calargv[ri],"-v") || !strcmp(calargv[ri],"--verbose") ) {
137 verbose = true;
138 };
139 if ( !strcmp(calargv[ri],"-g") || !strcmp(calargv[ri],"--debug") ) {
140 verbose = true;
141 debug = true;
142 };
143 if ( !strcmp(calargv[ri],"--alltracks") ) {
144 trackanyway = true;
145 };
146 if ( !strcmp(calargv[ri],"--use-default-alig") ) {
147 mechal = true;
148 };
149 if ( !strcmp(calargv[ri],"--no-crosstalk") ) {
150 crosst = false;
151 };
152 if ( !strcmp(calargv[ri],"--flight-crosstalk") ) {
153 ctground = false;
154 };
155 if ( !strcmp(calargv[ri],"--ct-use-pulse") ) {
156 usetable = false;
157 };
158 if ( !strcmp(calargv[ri],"--ct-use-table") ) {
159 usetable = true;
160 };
161 if ( !strcmp(calargv[ri],"--ground-crosstalk") ) {
162 ctground = true;
163 };
164 if ( !strcmp(calargv[ri],"--no-self-crosstalk") ) {
165 noselfct = true;
166 };
167 if ( !strcmp(calargv[ri],"--no-tracker") ) {
168 withtrk = false;
169 };
170 if ( !strcmp(calargv[ri],"--with-tracker") ) {
171 withtrk = true;
172 };
173 if ( !strcmp(calargv[ri],"--defrig") ) {
174 if ( calargc < ri+1 ){
175 throw -3;
176 };
177 rigdefault = atof(calargv[ri+1]);
178 ri++;
179 };
180 if ( !strcmp(calargv[ri],"--no-alltracks") ) {
181 trackanyway = false;
182 };
183 if ( !strcmp(calargv[ri],"--highZnuclei") ) {
184 hZn = true;
185 };
186 if ( !strcmp(calargv[ri],"--no-highZnuclei") ) {
187 hZn = false;
188 };
189 if ( !strcmp(calargv[ri],"--selftrigger") ) {
190 st = true;
191 };
192 if ( !strcmp(calargv[ri],"--no-selftrigger") ) {
193 st = false;
194 };
195 if ( !strcmp(calargv[ri],"--no-level1") ) {
196 getl1 = false;
197 };
198 if ( !strcmp(calargv[ri],"--level1") ) {
199 getl1 = true;
200 };
201 if ( !strcmp(calargv[ri],"--ignore-h20") ) {
202 checksimu = false;
203 };
204 if ( !strcmp(calargv[ri],"--help") ) {
205 printf("\n\n CALORIMETER HELP CALLED\n\n");
206 printf(" CaloCore options: \n");
207 printf(" -v | --verbose be verbose\n");
208 printf(" -g | --debug be really verbose\n");
209 printf(" --defrig rig rig is the default rigidity in GV to be used to\n");
210 printf(" obtain calorimeter variables in the routines\n");
211 printf(" \"alltracks\" and \"higZnuclei\" [default = 50]\n");
212 printf(" --alltracks fill the track related variables even in the case\n");
213 printf(" of no tracks from tracker and no selftrigger event\n");
214 printf(" when we have a calorimeter fit for both views [default]\n");
215 printf(" --no-alltracks fill the track related variables only in the case\n");
216 printf(" of a good track from tracker or selftrigger\n");
217 printf(" --highZnuclei call the routine to analyze high Z nuclei\n");
218 printf(" selftrigger events [default]\n");
219 printf(" --no-highZnuclei do not call the routine to analyze high Z nuclei\n");
220 printf(" selftrigger events\n");
221 printf(" --no-tracker do not use tracker level2\n");
222 printf(" --with-tracker use tracker level2 [default]\n");
223 printf(" --no-crosstalk do not apply crosstalk corrections\n");
224 printf(" --ground-crosstalk apply ground crosstalk corrections\n");
225 printf(" --flight-crosstalk apply flight crosstalk corrections [default]\n");
226 printf(" --no-self-crosstalk do not apply preamplifiers crosstalk corrections to the strip itself\n");
227 printf(" --ct--use-pulse flight crosstalk corrections from pulse calibrations \n");
228 printf(" --ct--use-table flight crosstalk corrections from calibration table [default] \n");
229 printf(" --selftrigger process selftrigger events [default]\n");
230 printf(" --no-selftrigger skip selftrigger events\n");
231 printf(" --no-level1 do not save Level1 TBranch\n");
232 printf(" --level1 save Level1 TBranch [default] \n");
233 printf(" --ignore-h20 do not check if it is a file from simulations\n");
234 printf(" --use-default-alig use default mechanical alignement (defined in CaloLevel1.h)\n");
235 throw -114;
236 };
237 ri++;
238 };
239 };
240 //
241 if ( verbose ){
242 printf("\n");
243 if ( getl1 ) printf(" Saving calorimeter level1 data \n");
244 if ( st ) printf(" Calling selftrigger subroutine \n");
245 if ( hZn ) printf(" Calling high energy nuclei subroutine \n");
246 if ( trackanyway ) printf(" Filling track related variables for all the possible tracks \n");
247 if ( hZn || trackanyway ) printf(" => default assumed rigidity %f \n",rigdefault);
248 if ( withtrk ) printf(" Using tracker level2 data \n");
249 if ( mechal ) printf(" Using default mechanical alignement (defined in CaloLevel1.h)\n");
250 if ( crosst ){
251 printf(" Applying cross-talk corrections \n");
252 if ( ctground ){
253 printf(" => Using ground cross-talk coefficients \n");
254 } else {
255 printf(" => Using flight cross-talk coefficients... \n");
256 };
257 if ( usetable ){
258 printf(" ... coming from tables \n");
259 } else {
260 printf(" ... coming from pulse calibrations \n");
261 };
262 } else {
263 printf(" Do not applying cross-talk corrections \n");
264 };
265 if ( !checksimu ) printf(" Check on h20 TTree in level2 file skipped \n");
266 printf("\n");
267 };
268 //
269 // Working filename
270 //
271 TString outputfile;
272 stringstream name;
273 name.str("");
274 name << outdir << "/";
275 //
276 // Variables.
277 //
278 TTree *tracker = 0;
279 TTree *calo = 0;
280 TTree *caloclone = 0;
281 Bool_t reproc = false;
282 Bool_t reprocall = false;
283 UInt_t nevents = 0;
284 UInt_t nobefrun = 0;
285 UInt_t noaftrun = 0;
286 UInt_t numbofrun = 0;
287 UInt_t totnorun = 0;
288 //
289 Int_t code = 0;
290 Int_t sgnl;
291 //
292 // calorimeter level2 classes
293 //
294 CaloLevel1 *c1 = 0;
295 CaloLevel1 *c1clone = 0;
296 if ( getl1 ){
297 c1 = new CaloLevel1();
298 c1clone = new CaloLevel1();
299 };
300 //
301 // calorimeter level2 classes
302 //
303 CaloLevel2 *ca = new CaloLevel2();
304 CaloLevel2 *caclone = new CaloLevel2();
305 //
306 TrkLevel2 *trk = new TrkLevel2();
307 Int_t nevtrkl2 = 0;
308 //
309 UInt_t procev = 0;
310 //
311 // 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).
312 //
313 UInt_t runheadtime = 0;
314 UInt_t runtrailtime = 0;
315 UInt_t evfrom = 0;
316 UInt_t evto = 0;
317 UInt_t totfileentries = 0;
318 UInt_t idRun = 0;
319 Int_t id_reg_run=-1;
320 stringstream ftmpname;
321 TString fname;
322 //
323 // define variables for opening and reading level0 file
324 //
325 TFile *l0File = 0;
326 TTree *l0tr = 0;
327 TTree *softinfo = 0;
328 TBranch *l0head = 0;
329 TBranch *l0calo = 0;
330 TBranch *l0trig = 0;
331 pamela::EventHeader *eh = 0;
332 pamela::PscuHeader *ph = 0;
333 pamela::trigger::TriggerEvent *trig = 0;
334 Int_t yodaver = 0;
335 //
336 // Define some basic variables
337 //
338 CaloLevel0 *event = new CaloLevel0(); // NOTICE: very important to call here the constructor!
339 event->SetCrossTalk(crosst);
340 // event->SetCrossTalkType(ctground);
341 Int_t ict = -1;
342 if ( ctground ) ict = 0;
343 if ( !ctground ){
344 ict = 1;
345 };
346 if ( !ctground && noselfct ){
347 ict = 2;
348 };
349 event->SetCrossTalkType(ict);
350 stringstream file2;
351 stringstream file3;
352 stringstream qy;
353 // Bool_t imtrack = false;
354 Bool_t filled = false;
355 //
356 UInt_t caloevents = 0;
357 stringstream calfile;
358 stringstream aligfile;
359 //
360 Int_t i = -1;
361 Int_t itr = -1;
362 Int_t badevent = 0;
363 Int_t totevent = 0;
364 //
365 UInt_t atime = 0;
366 //
367 Int_t S3 = 0;
368 Int_t S2 = 0;
369 Int_t S12 = 0;
370 Int_t S11 = 0;
371 UInt_t re = 0;
372 UInt_t jumped = 0;
373 //
374 TString caloversion;
375 ItoRunInfo *runinfo = 0;
376 TArrayI *runlist = 0;
377 //
378 Float_t tmptrigty = -1.;
379 Int_t ntrkentry = 0;
380 GL_PARAM *q4 = new GL_PARAM();
381 UInt_t tttrkpar1 = 0;
382 Bool_t trkpar1 = true;
383 GL_ROOT *glroot = new GL_ROOT();
384 GL_TIMESYNC *dbtime = 0;
385 //
386 Long64_t maxsize = 10000000000LL;
387 TTree::SetMaxTreeSize(maxsize);
388 //
389 //
390 TFile *tempfile = 0;
391 TTree *tempcalo = 0;
392 Bool_t myfold = false;
393 stringstream tempname;
394 stringstream calofolder;
395 tempname.str("");
396 tempname << outdir;
397 tempname << "/" << processFolder.Data();
398 calofolder.str("");
399 calofolder << tempname.str().c_str();
400 tempname << "/calotree_run";
401 tempname << run << ".root";
402 //
403 // 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
404 // if run != -1 we must process only that run but first we have to check if the branch calorimeter already exist in the file
405 // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
406 //
407 if ( run == 0 ) reproc = true;
408 //
409 //
410 //
411 if ( !file->IsOpen() ){
412 if ( verbose ) printf(" CALORIMETER - ERROR: cannot open file for writing\n");
413 throw -101;
414 };
415 //
416 if ( withtrk ){
417 //
418 // Does it contain the Tracker tree?
419 //
420 tracker = (TTree*)file->Get("Tracker");
421 if ( !tracker ) {
422 if ( verbose ) printf(" CALORIMETER - ERROR: no tracker tree\n");
423 code = -102;
424 goto closeandexit;
425 };
426 tracker->SetBranchAddress("TrkLevel2",&trk);
427 nevtrkl2 = tracker->GetEntries();
428 };
429 //
430 // Is it a file from simulations?
431 //
432 if ( checksimu ){
433 TTree *h20 = (TTree*)file->Get("h20");
434 if ( h20 ){
435 //
436 // yes!
437 //
438 crosst = false;
439 ctground = true;
440 mechal = true;
441 if ( verbose ) printf("\n\n SIMULATION!!! Setting --use-default-alig and --no-crosstalk flags!\n\n");
442 h20->Delete();
443 };
444 };
445 //
446 // Call runinfo
447 //
448 sgnl = 0;
449 runinfo = new ItoRunInfo(file);
450 //
451 // update versioning information and retrieve informations about the run to be processed
452 //
453 caloversion = CaloInfo(false);
454 sgnl = runinfo->Update(run,"CALO",caloversion);
455 //
456 if ( sgnl ){
457 if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
458 code = sgnl;
459 goto closeandexit;
460 } else {
461 sgnl = 0;
462 };
463 //
464 // number of events in the file BEFORE the first event of our run
465 //
466 nobefrun = runinfo->GetFirstEntry();
467 //
468 // total number of events in the file
469 //
470 totfileentries = runinfo->GetFileEntries();
471 //
472 // first file entry AFTER the last event of our run
473 //
474 noaftrun = runinfo->GetLastEntry() + 1;
475 //
476 // number of run to be processed
477 //
478 numbofrun = runinfo->GetNoRun();
479 //
480 // number of runs in the file
481 //
482 totnorun = runinfo->GetRunEntries();
483 //
484 // Does it contain already a Calorimeter branch? if so we are reprocessing data, if not we must create it.
485 //
486 caloclone = (TTree*)file->Get("Calorimeter");
487 //
488 if ( !caloclone ){
489 reproc = false;
490 if ( run == 0 && verbose ) printf(" CALORIMETER - WARNING: you are reprocessing data but calorimeter tree does not exist!\n");
491 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");
492 //
493 } else {
494 //
495 caloclone->SetAutoSave(900000000000000LL);
496 reproc = true;
497 //
498 if ( verbose ) printf("\n Preparing the pre-processing...\n");
499 //
500 if ( run == 0 || totnorun == 1 ){
501 //
502 // 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.
503 //
504 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing all runs in the file\n");
505 reprocall = true;
506 //
507 } else {
508 //
509 // we are reprocessing a single run
510 //
511 if ( verbose ) printf("\n CALORIMETER - WARNING: Reprocessing run number %u \n",run);
512 reprocall = false;
513 //
514 //
515 //
516 gSystem->MakeDirectory(calofolder.str().c_str());
517 myfold = true;
518 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
519 tempcalo = caloclone->CloneTree(-1,"fast");
520 tempcalo->SetName("Calorimeter-old");
521 tempfile->Write();
522 tempfile->Close();
523 };
524 //
525 // delete old tree
526 //
527 caloclone->Delete("all");
528 //
529 if ( verbose ) printf("\n ...done!\n");
530 //
531 };
532 //
533 // create calorimeter tree calo
534 //
535 file->cd();
536 calo = new TTree("Calorimeter-new","PAMELA Level2 calorimeter data");
537 calo->SetAutoSave(900000000000000LL);
538 ca->Set();
539 calo->Branch("CaloLevel2","CaloLevel2",&ca);
540 if ( getl1 ) calo->Branch("CaloLevel1","CaloLevel1",&c1);
541 //
542 if ( reproc && !reprocall ){
543 //
544 //
545 //
546 tempfile = new TFile(tempname.str().c_str(),"READ");
547 caloclone = (TTree*)tempfile->Get("Calorimeter-old");
548 caloclone->SetAutoSave(900000000000000LL);
549 caloclone->SetBranchAddress("CaloLevel2",&caclone);
550 TBranch *havel1 = caloclone->GetBranch("CaloLevel1");
551 if ( !havel1 && getl1 ) throw -117;
552 if ( havel1 && !getl1 ) throw -118;
553 if ( getl1 ) caloclone->SetBranchAddress("CaloLevel1",&c1clone);
554 //
555 if ( nobefrun > 0 ){
556 if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
557 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
558 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
559 for (UInt_t j = 0; j < nobefrun; j++){
560 //
561 caloclone->GetEntry(j);
562 //
563 // copy caclone to ca
564 //
565 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
566 memcpy(&ca,&caclone,sizeof(caclone));
567 //
568 // Fill entry in the new tree
569 //
570 calo->Fill();
571 //
572 if ( getl1 ) c1->Clear();
573 ca->Clear();
574 //
575 };
576 if ( verbose ) printf(" Finished successful copying!\n");
577 };
578 };
579 //
580 // Get the list of run to be processed
581 //
582 runlist = runinfo->GetRunList();
583 //
584 // Loop over the run to be processed
585 //
586 for (UInt_t irun=0; irun < numbofrun; irun++){
587 //
588 badevent = 0;
589 //
590 idRun = runlist->At(irun);
591 if ( verbose ) printf("\n\n\n ####################################################################### \n");
592 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
593 if ( verbose ) printf(" ####################################################################### \n\n\n");
594 //
595 sgnl = runinfo->GetRunInfo(idRun);
596 if ( sgnl ){
597 if ( verbose ) printf(" CALORIMETER - ERROR: RunInfo exited with non-zero status\n");
598 code = sgnl;
599 goto closeandexit;
600 } else {
601 sgnl = 0;
602 };
603 id_reg_run = runinfo->ID_ROOT_L0;
604 runheadtime = runinfo->RUNHEADER_TIME;
605 runtrailtime = runinfo->RUNTRAILER_TIME;
606 evfrom = runinfo->EV_FROM;
607 evto = runinfo->EV_TO;
608 //
609 if ( id_reg_run == -1 ){
610 if ( verbose ) printf("\n CALORIMETER - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
611 code = -5;
612 goto closeandexit;
613 };
614 //
615 // prepare the timesync for the db
616 //
617 TString host = glt->CGetHost();
618 TString user = glt->CGetUser();
619 TString psw = glt->CGetPsw();
620 TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
621 if ( !dbc->IsConnected() ) throw -116;
622 stringstream myquery;
623 myquery.str("");
624 myquery << "SET time_zone='+0:00'";
625 dbc->Query(myquery.str().c_str());
626 //
627 // if ( !dbc->IsConnected() ) throw -116;
628 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
629 //
630 // Search in the DB the path and name of the LEVEL0 file to be processed.
631 //
632 // if ( !dbc->IsConnected() ) throw -116;
633 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
634 //
635 ftmpname.str("");
636 ftmpname << glroot->PATH.Data() << "/";
637 ftmpname << glroot->NAME.Data();
638 fname = ftmpname.str().c_str();
639 //
640 // print out informations
641 //
642 totevent = runinfo->NEVENTS;
643 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
644 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runheadtime);
645 if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runtrailtime);
646 if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i (reg entries)\n\n",totevent,idRun,evfrom,evfrom+totevent);
647 //
648 // if ( !totevent ) goto closeandexit;
649 //
650 // Open Level0 file
651 //
652 l0File = new TFile(fname.Data());
653 if ( !l0File ) {
654 if ( verbose ) printf(" CALORIMETER - ERROR: problems opening Level0 file\n");
655 code = -6;
656 goto closeandexit;
657 };
658 l0tr = (TTree*)l0File->Get("Physics");
659 if ( !l0tr ) {
660 if ( verbose ) printf(" CALORIMETER - ERROR: no Physics tree in Level0 file\n");
661 l0File->Close();
662 code = -7;
663 goto closeandexit;
664 };
665 l0head = l0tr->GetBranch("Header");
666 if ( !l0head ) {
667 if ( verbose ) printf(" CALORIMETER - ERROR: no Header branch in Level0 tree\n");
668 l0File->Close();
669 code = -8;
670 goto closeandexit;
671 };
672 l0calo = l0tr->GetBranch("Calorimeter");
673 if ( !l0calo ) {
674 if ( verbose ) printf(" CALORIMETER - ERROR: no Calorimeter branch in Level0 tree\n");
675 l0File->Close();
676 code = -103;
677 goto closeandexit;
678 };
679 l0trig = l0tr->GetBranch("Trigger");
680 if ( !l0trig ) {
681 if ( verbose ) printf(" CALORIMETER - ERROR: no Trigger branch in Level0 tree\n");
682 l0File->Close();
683 code = -104;
684 goto closeandexit;
685 };
686 //
687 l0tr->SetBranchAddress("Trigger", &trig);
688 l0tr->SetBranchAddress("Header", &eh);
689 //
690 softinfo = (TTree*)l0File->Get("SoftInfo");
691 if ( softinfo ){
692 softinfo->SetBranchAddress("SoftInfo",&yodaver);
693 softinfo->GetEntry(0);
694 };
695 if ( debug ) printf(" LEVEL0 FILE GENERATED WITH YODA VERSION %i \n",yodaver);
696 //
697 if ( withtrk ){
698 if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runheadtime ) ){
699 trkpar1 = false;
700 Int_t glpar = q4->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc);
701 if ( glpar < 0 ){
702 code = glpar;
703 goto closeandexit;
704 };
705 tttrkpar1 = q4->TO_TIME;
706 // ----------------------------
707 // Read the magnetic field
708 // ----------------------------
709 if ( verbose ) printf(" Reading magnetic field maps at %s\n",(q4->PATH+q4->NAME).Data());
710 trk->LoadField(q4->PATH+q4->NAME);
711 if ( verbose ) printf("\n");
712 };
713 };
714 //
715 // Close DB connection
716 //
717 if ( dbc ){
718 dbc->Close();
719 delete dbc;
720 };
721 //
722 //
723 // Construct the event object, look for the calibration which include the first header
724 //
725 sgnl = 0;
726 if ( verbose ) printf(" Check for calorimeter calibrations and initialize event object \n");
727 // if ( !dbc->IsConnected() ) throw -116;
728 event->ProcessingInit(glt,runheadtime,sgnl,l0tr,debug,verbose);
729 if ( verbose ) printf("\n");
730 if ( sgnl == 100 ) {
731 code = sgnl;
732 if ( verbose ) printf(" CALORIMETER - WARNING: run header not included in any calibration interval\n");
733 sgnl = 0;
734 };
735 if ( sgnl ){
736 l0File->Close();
737 code = sgnl;
738 goto closeandexit;
739 };
740 //
741 qy.str("");
742 //
743 nevents = l0head->GetEntries();
744 caloevents = l0calo->GetEntries();
745 //
746 if ( nevents < 1 && totevent ) {
747 if ( verbose ) printf(" CALORIMETER - ERROR: Level0 file is empty\n\n");
748 l0File->Close();
749 code = -11;
750 goto closeandexit;
751 };
752 //
753 if ( evto > nevents-1 && totevent ) {
754 if ( verbose ) printf(" CALORIMETER - ERROR: too few entries in the registry tree\n");
755 l0File->Close();
756 code = -12;
757 goto closeandexit;
758 };
759 //
760 // Check if we have to load parameter files
761 //
762 sgnl = 0;
763 sgnl = event->ChkParam(glt,runheadtime,mechal); // calorimeter parameter files
764 if ( sgnl < 0 ){
765 code = sgnl;
766 l0File->Close();
767 goto closeandexit;
768 };
769 //
770 // Calculate the cross talk corrections needed for this run using calibration informations
771 //
772 if ( crosst && !ctground ){
773 sgnl = 0;
774 sgnl = event->CalcCrossTalkCorr(glt,runheadtime,usetable);
775 if ( sgnl < 0 ){
776 code = sgnl;
777 l0File->Close();
778 goto closeandexit;
779 };
780 };
781 //
782 // run over all the events of the run
783 //
784 if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
785 //
786 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
787 // for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+5000); re++){
788 //for ( re = 92012+3400; re < 92012+3700; re++){
789 // printf(" i = %i \n",re-200241);
790 //
791 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
792 //
793 if ( debug ) printf("\n\n\n EVENT NUMBER %i \n",procev);
794 //
795 l0head->GetEntry(re);
796 //
797 // absolute time of this event
798 //
799 ph = eh->GetPscuHeader();
800 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
801 //
802 //
803 //
804 if ( re > caloevents-1 ){
805 if ( verbose ) printf(" CALORIMETER - ERROR: no physics events with entry = %i in Level0 file\n",i);
806 l0File->Close();
807 code = -112;
808 goto closeandexit;
809 };
810 //
811 if ( atime > (runtrailtime+1) || atime < (runheadtime-1) ) {
812 if ( verbose ) printf(" CALORIMETER - WARNING: event at time outside the run time window, skipping it\n");
813 jumped++;
814 goto jumpev;
815 };
816 //
817 // retrieve tracker informations
818 //
819 if ( !reprocall ){
820 itr = nobefrun + (re - evfrom - jumped);
821 //itr = re-(46438+200241);
822 } else {
823 itr = runinfo->GetFirstEntry() + (re - evfrom - jumped);
824 };
825 //
826 if ( withtrk ){
827 if ( itr > nevtrkl2 ){
828 if ( verbose ) printf(" CALORIMETER - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
829 l0File->Close();
830 code = -113;
831 goto closeandexit;
832 };
833 //
834 trk->Clear();
835 //
836 tracker->GetEntry(itr);
837 //
838 };
839 //
840 procev++;
841 //
842 // start processing
843 //
844 if ( getl1 ) c1->Clear();
845 ca->Clear();
846 //
847 // determine who generate the trigger for this event (TOF, S4/PULSER, CALO)
848 //
849 l0trig->GetEntry(re);
850 S3 = 0;
851 S2 = 0;
852 S12 = 0;
853 S11 = 0;
854 S3 = trig->patterntrig[2];
855 S2 = trig->patterntrig[3];
856 S12 = trig->patterntrig[4];
857 S11 = trig->patterntrig[5];
858 if ( trig->patterntrig[1] & (1<<0) ) tmptrigty = 1.;
859 if ( trig->patterntrig[0] ) tmptrigty = 2.;
860 if ( S3 || S2 || S12 || S11 ) tmptrigty = 0.;
861 if ( !(trig->patterntrig[1] & (1<<0)) && !trig->patterntrig[0] && !S3 && !S2 && !S12 && !S11 ) tmptrigty = 1.;
862 event->clevel2->trigty = tmptrigty;
863 //
864 // check if the calibration we are using is still good, if not load another calibration
865 //
866 sgnl = 0;
867 sgnl = event->ChkCalib(glt,atime);
868 if ( sgnl < 0 ){
869 code = sgnl;
870 goto closeandexit;
871 };
872 if ( sgnl == 100 ){
873 code = sgnl;
874 if ( verbose ) printf(" CALORIMETER - WARNING: data not associated to any calibration interval\n");
875 badevent++;
876 sgnl = 0;
877 };
878 //
879 // do we have at least one track from the tracker? this check has been disabled
880 //
881 event->clevel1->good2 = 1;
882 //
883 // use the whole calorimeter, 22 W planes
884 //
885 event->clevel1->npla = 22;
886 //
887 // Use the standard silicon planes shifting
888 //
889 event->clevel1->reverse = 0;
890 //
891 //
892 // Calibrate calorimeter event "re" and store output in the two structures that will be passed to fortran routine
893 //
894 event->Calibrate(re);
895 //
896 // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract topological variables.
897 //
898 //
899 // Calculate variables common to all tracks (qtot, nstrip, etc.)
900 //
901 event->GetCommonVar();
902 //
903 // Fill common variables
904 //
905 event->FillCommonVar(c1,ca);
906 //
907 // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker)
908 //
909 ntrkentry = 0;
910 //
911 filled = false;
912 //
913 // Run over tracks (tracker or calorimeter )
914 //
915 if ( withtrk ){
916 for(Int_t nt=0; nt < trk->ntrk(); nt++){
917 //
918 event->clevel1->good2 = 1;
919 //
920 TrkTrack *ptt = trk->GetStoredTrack(nt);
921 //
922 event->clevel1->trkchi2 = 0;
923 //
924 // Copy the alpha vector in the input structure
925 //
926 for (Int_t e = 0; e < 5 ; e++){
927 event->clevel1->al_p[e][0] = ptt->al[e];
928 };
929 //
930 // Get tracker related variables for this track
931 //
932 event->GetTrkVar();
933 //
934 // Save tracker track sequence number
935 //
936 event->trkseqno = nt;
937 //
938 // Copy values in the class ca from the structure clevel2
939 //
940 event->FillTrkVar(ca,ntrkentry);
941 ntrkentry++;
942 filled = true;
943 //
944 }; // loop on all the tracks
945 };
946 //
947 // 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
948 // fit of the track (to be used for example when TRK is off due to any reason like IPM3/5 off).
949 // here we make an event selection so it must be done very carefully...
950 //
951 // 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
952 //
953 if ( trackanyway && !filled && event->clevel2->npcfit[0] >= 2 && event->clevel2->npcfit[1] >= 2 && event->clevel2->good != 0 && event->clevel2->trigty < 2. ){
954 if ( debug ) printf(" Event with a track not fitted by the tracker at entry %i \n",itr);
955 //
956 // Disable "track mode" in the fortran routine
957 //
958 event->clevel1->good2 = 0;
959 event->clevel1->riginput = rigdefault;
960 if ( debug ) printf(" Using as default rigidity: %f \n",event->clevel1->riginput);
961 //
962 // We have a selftrigger event to analyze.
963 //
964 for (Int_t e = 0; e < 5 ; e++){
965 event->clevel1->al_p[e][0] = 0.;
966 event->clevel1->al_p[e][1] = 0.;
967 };
968 event->clevel1->trkchi2 = 0;
969 //
970 event->GetTrkVar();
971 //
972 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
973 //
974 if ( event->clevel1->good2 == 0 ) {
975 //
976 // In selftrigger mode the trkentry variable is set to -1
977 //
978 event->trkseqno = -3;
979 //
980 // Copy values in the class ca from the structure clevel2
981 //
982 event->FillTrkVar(ca,ntrkentry);
983 ntrkentry++;
984 filled = true;
985 //
986 } else {
987 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
988 };
989 //
990 };
991 //
992 // Call high energy nuclei routine
993 //
994 if ( hZn && event->clevel2->trigty >= 2. ){
995 if ( debug ) printf(" Calling selftrigger high energy nuclei routine for entry %i \n",itr);
996 //
997 // Disable "track mode" in the fortran routine
998 //
999 event->clevel1->good2 = 0;
1000 //
1001 // Set high energy nuclei flag to one
1002 //
1003 event->clevel1->hzn = 1;
1004 event->clevel1->riginput = rigdefault;
1005 //
1006 // We have a selftrigger event to analyze.
1007 //
1008 for (Int_t e = 0; e < 5 ; e++){
1009 event->clevel1->al_p[e][0] = 0.;
1010 event->clevel1->al_p[e][1] = 0.;
1011 };
1012 event->clevel1->trkchi2 = 0;
1013 //
1014 event->GetTrkVar();
1015 //
1016 // if we had no problem (clevel1->good2 = 0, NOTICE zero, not one in this mode!), fill and go on
1017 //
1018 if ( event->clevel1->good2 == 0 ) {
1019 //
1020 // In selftrigger mode the trkentry variable is set to -1
1021 //
1022 event->trkseqno = -2;
1023 //
1024 // Copy values in the class ca from the structure clevel2
1025 //
1026 event->FillTrkVar(ca,ntrkentry);
1027 ntrkentry++;
1028 filled = true;
1029 //
1030 } else {
1031 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
1032 };
1033 //
1034 };
1035 //
1036 // self trigger event
1037 //
1038 if ( st && event->clevel2->trigty >= 2. ){
1039 if ( verbose ) printf(" Selftrigger event at entry %i \n",itr);
1040 //
1041 // Disable "track mode" in the fortran routine
1042 //
1043 event->clevel1->good2 = 0;
1044 //
1045 // disable high enery nuclei flag;
1046 //
1047 event->clevel1->hzn = 0;
1048 //
1049 // We have a selftrigger event to analyze.
1050 //
1051 for (Int_t e = 0; e < 5 ; e++){
1052 event->clevel1->al_p[e][0] = 0.;
1053 event->clevel1->al_p[e][1] = 0.;
1054 };
1055 event->clevel1->trkchi2 = 0;
1056 //
1057 event->GetTrkVar();
1058 //
1059 // if we had no problem (clevel2->good = 0, NOTICE zero, not one in selftrigger mode!), fill and go on
1060 //
1061 if ( event->clevel1->good2 == 0 ) {
1062 //
1063 // In selftrigger mode the trkentry variable is set to -1
1064 //
1065 event->trkseqno = -1;
1066 //
1067 // Copy values in the class ca from the structure clevel2
1068 //
1069 event->FillTrkVar(ca,ntrkentry);
1070 ntrkentry++;
1071 filled = true;
1072 //
1073 } else {
1074 if ( verbose ) printf(" Selftrigger: problems with event at entry %i \n",itr);
1075 };
1076 };
1077 //
1078 if ( !filled ) badevent++;
1079 //
1080 // Clear structures used to communicate with fortran
1081 //
1082 event->ClearStructs();
1083 //
1084 // Fill the rootple
1085 //
1086 calo->Fill();
1087 //
1088 // delete ca;
1089 //
1090 jumpev:
1091 if ( !debug ) debug = false;
1092 //
1093 };
1094 //
1095 if ( verbose ) printf("\n SUMMARY:\n");
1096 if ( verbose ) printf(" Total number of events: %i \n",totevent);
1097 if ( verbose ) printf(" Events with at least one track: %i \n",totevent-badevent);
1098 if ( verbose ) printf(" Events without tracks: %i \n",badevent);
1099 //
1100 if ( badevent == totevent ){
1101 if ( verbose ) printf("\n CALORIMETER - WARNING no tracks or good events in run %u \n",idRun);
1102 code = 101;
1103 };
1104 //
1105 delete dbtime;
1106 //
1107 // Clear variables before processing another run (needed to have always the same result when reprocessing data with the same software).
1108 //
1109 event->RunClose();
1110 if ( l0File ) l0File->Close();
1111 //
1112 }; // process all the runs
1113 //
1114 if ( verbose ) printf("\n Finished processing data \n");
1115 //
1116 closeandexit:
1117 //
1118 if ( !reprocall && reproc && code >= 0 ){
1119 if ( totfileentries > noaftrun ){
1120 if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1121 if ( verbose ) printf(" Copying %u events in the file which are after the end of the run %u \n",(totfileentries-noaftrun),run);
1122 if ( verbose ) printf(" Start copying at event number %u end copying at event number %u \n",noaftrun,totfileentries);
1123 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1124 //
1125 // Get entry from old tree
1126 //
1127 caloclone->GetEntry(j);
1128 //
1129 // copy caclone to ca
1130 //
1131 if ( getl1 ) c1->Clear();
1132 ca->Clear();
1133 //
1134 if ( getl1 ) memcpy(&c1,&c1clone,sizeof(c1clone));
1135 memcpy(&ca,&caclone,sizeof(caclone));
1136 //
1137 // Fill entry in the new tree
1138 //
1139 calo->Fill();
1140 //
1141 };
1142 if ( verbose ) printf(" Finished successful copying!\n");
1143 };
1144 };
1145 //
1146 // Case of no errors: close files, delete old tree(s), write and close level2 file
1147 //
1148 if ( l0File ) l0File->Close();
1149 if ( tempfile ) tempfile->Close();
1150 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1151 if ( tracker ) tracker->Delete();
1152 //
1153 if ( code < 0 ) printf("\n CALORIMETER - ERROR: an error occurred, try to save anyway...\n");
1154 if ( verbose ) printf("\n Writing and closing rootple\n");
1155 if ( runinfo ) runinfo->Close();
1156 if ( calo ) calo->SetName("Calorimeter");
1157 if ( file ){
1158 file->cd();
1159 file->Write();
1160 };
1161 if ( calo ) calo->Delete();
1162 //
1163 if ( myfold ) gSystem->Unlink(calofolder.str().c_str());
1164 //
1165 if ( ca ) delete ca;
1166 if ( caclone ) delete caclone;
1167 if ( c1 ) delete c1;
1168 if ( c1clone ) delete c1clone;
1169 if ( trk ) delete trk;
1170 if ( q4 ) delete q4;
1171 if ( glroot ) delete glroot;
1172 if ( runinfo ) delete runinfo;
1173 //
1174 // the end
1175 //
1176 if ( verbose ) printf("\n Exiting...\n");
1177 if ( code < 0 ) throw code;
1178 return(code);
1179 }
1180
1181
1182

  ViewVC Help
Powered by ViewVC 1.1.23