/[PAMELA software]/DarthVader/ToFLevel2/src/ToFCore.cpp
ViewVC logotype

Contents of /DarthVader/ToFLevel2/src/ToFCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (show annotations) (download)
Wed Dec 2 22:41:02 2009 UTC (15 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.38: +2 -2 lines
Possible bug in dE/dx II order correction fixed

1 //
2 // C/C++ headers
3 //
4 #include <fstream>
5 #include <string.h>
6 #include <iomanip>
7 //
8 // ROOT headers
9 //
10 #include <TTree.h>
11 #include <TClassEdit.h>
12 #include <TObject.h>
13 #include <TList.h>
14 #include <TArrayI.h>
15 #include <TSystem.h>
16 #include <TSystemDirectory.h>
17 #include <TString.h>
18 #include <TFile.h>
19 #include <TClass.h>
20 #include <TCanvas.h>
21 #include <TH1.h>
22 #include <TH1F.h>
23 #include <TH2D.h>
24 #include <TLatex.h>
25 #include <TPad.h>
26 #include <TSQLServer.h>
27 #include <TSQLRow.h>
28 #include <TSQLResult.h>
29 #include <TClonesArray.h>
30 //
31 // RunInfo header
32 //
33 #include <RunInfo.h>
34 //
35 // YODA headers
36 //
37 #include <PamelaRun.h>
38 #include <physics/trigger/TriggerEvent.h>
39 #include <physics/tof/TofEvent.h>
40 //
41 // This program headers
42 //
43 #include <ToFCore.h>
44 #include <ToFLevel2.h>
45 #include <ToFVerl2.h>
46 //
47 //
48 // Declaration of the core fortran routines
49 //
50 #define tofl2com tofl2com_
51 extern "C" int tofl2com();
52 #define toftrk toftrk_
53 extern "C" int toftrk();
54 #define rdtofcal rdtofcal_
55 extern "C" int rdtofcal(char [], int *);
56
57 //
58 // Tracker classes headers and definitions
59 //
60 #include <TrkLevel2.h>
61 //
62 using namespace std;
63 //
64 //
65 // CORE ROUTINE
66 //
67 //
68 int ToFCore(UInt_t run, TFile *file, GL_TABLES *glt, Int_t ToFargc, char *ToFargv[]){
69 //
70 //
71 // Set these to true to have a very verbose output.
72 //
73 Bool_t verbose = false;
74 Bool_t debug = false;
75 //
76 TString processFolder = Form("ToFFolder_%u",run);
77 if ( ToFargc > 0 ){
78 Int_t i = 0;
79 while ( i < ToFargc ){
80 if ( !strcmp(ToFargv[i],"-processFolder") ) {
81 if ( ToFargc < i+1 ){
82 throw -3;
83 };
84 processFolder = (TString)ToFargv[i+1];
85 i++;
86 };
87 if ( !strcmp(ToFargv[i],"-v") || !strcmp(ToFargv[i],"--verbose") ) {
88 verbose = true;
89 };
90 if ( !strcmp(ToFargv[i],"-g") || !strcmp(ToFargv[i],"--debug") ) {
91 verbose = true;
92 debug = true;
93 };
94 i++;
95 };
96 };
97 //
98 //
99 // Output directory is the working directoy.
100 //
101 const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath()));
102 //
103 // Variables for level2
104 //
105 TTree *tracker = 0;
106 TTree *toft = 0;
107 UInt_t nevents = 0;
108 Long64_t maxsize = 10000000000LL;
109 TTree::SetMaxTreeSize(maxsize);
110 //
111 // variables needed to reprocess data
112 //
113 TString tofversion;
114 ItoRunInfo *runinfo = 0;
115 TArrayI *runlist = 0;
116 TTree *toftclone = 0;
117 Bool_t reproc = false;
118 Bool_t reprocall = false;
119 UInt_t nobefrun = 0;
120 UInt_t noaftrun = 0;
121 UInt_t numbofrun = 0;
122 stringstream ftmpname;
123 TString fname;
124 UInt_t totfileentries = 0;
125 UInt_t idRun = 0;
126 //
127 // variables needed to handle error signals
128 //
129 Int_t code = 0;
130 Int_t sgnl;
131 //
132 // tof level2 classes
133 //
134 ToFLevel2 *tof = new ToFLevel2();
135 ToFLevel2 *tofclone = new ToFLevel2();
136 ToFdEdx *tofdedx = new ToFdEdx();
137 //
138 // tracker level2 variables
139 //
140 TrkLevel2 *trk = new TrkLevel2();
141 Int_t nevtrkl2 = 0;
142 //
143 // define variables for opening and reading level0 file
144 //
145 TFile *l0File = 0;
146 TTree *l0tr = 0;
147 TBranch *l0head = 0;
148 TBranch *l0trig = 0;
149 TBranch *l0tof = 0;
150 pamela::EventHeader *eh = 0;
151 pamela::PscuHeader *ph = 0;
152 pamela::trigger::TriggerEvent *trig = 0;
153 pamela::tof::TofEvent *tofEvent = 0;
154 //
155 // Define other basic variables
156 //
157 UInt_t procev = 0;
158 stringstream file2;
159 stringstream file3;
160 stringstream qy;
161 Int_t itr = -1;
162 Int_t totevent = 0;
163 UInt_t atime = 0;
164 UInt_t re = 0;
165 UInt_t jumped = 0;
166 //
167 // Working filename
168 //
169 TString outputfile;
170 stringstream name;
171 name.str("");
172 name << outdir << "/";
173 //
174 // temporary file and folder
175 //
176 TFile *tempfile = 0;
177 TTree *temptof = 0;
178 stringstream tempname;
179 stringstream toffolder;
180 Bool_t myfold = false;
181 tempname.str("");
182 tempname << outdir;
183 tempname << "/" << processFolder.Data();
184 toffolder.str("");
185 toffolder << tempname.str().c_str();
186 tempname << "/toftree_run";
187 tempname << run << ".root";
188 UInt_t totnorun = 0;
189 //
190 // variables needed to load magnetic field maps
191 //
192 Int_t ntrkentry = 0;
193 Int_t npmtentry = 0;
194 UInt_t tttrkpar1 = 0;
195 Bool_t trkpar1 = true;
196 UInt_t tttofpar1 = 0;
197 Bool_t tofpar1 = true;
198 //
199 // DB classes
200 //
201 GL_ROOT *glroot = new GL_ROOT();
202 GL_PARAM *glparam = new GL_PARAM();
203 GL_TIMESYNC *dbtime = 0;
204 //
205 // declaring external output and input structures
206 //
207 extern struct ToFInput tofinput_;
208 extern struct ToFOutput tofoutput_;
209 //
210 // WM variables perform dE/dx II order corrections
211 //
212 Float_t dedx_corr_m[100][48],dedx_corr[48];
213 Double_t mtime[100],t1,t2,tm;
214 Float_t yhelp1,yhelp2,slope,inter,thelp1,thelp2;
215 Float_t xmean1,xwidth1;
216 Int_t ical,ii,wj,jj;
217 //
218 // Let's start!
219 //
220 //
221 // 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
222 // if run != 0 we must process only that run but first we have to check if the tree ToF already exist in the file
223 // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
224 //
225 if ( run == 0 ) reproc = true;
226 //
227 //
228 // Output file is "outputfile"
229 //
230 if ( !file->IsOpen() ){
231 if ( verbose ) printf(" ToF - ERROR: cannot open file for writing\n");
232 throw -301;
233 };
234 //
235 // Does it contain the Tracker tree?
236 //
237 tracker = (TTree*)file->Get("Tracker");
238 if ( !tracker ) {
239 if ( verbose ) printf(" TOF - ERROR: no tracker tree\n");
240 code = -302;
241 goto closeandexit;
242 };
243 //
244 // get tracker level2 data pointer
245 //
246 tracker->SetBranchAddress("TrkLevel2",&trk);
247 nevtrkl2 = tracker->GetEntries();
248 //
249 // Retrieve GL_RUN variables from the level2 file
250 //
251 tofversion = ToFInfo(false); // we should decide how to handle versioning system
252 //
253 // create an interface to RunInfo called "runinfo"
254 //
255 // ItoRunInfo= interface with RunInfo and GL_RUN
256 runinfo = new ItoRunInfo(file);
257 //
258 // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
259 //
260 sgnl = 0;
261 sgnl = runinfo->Update(run, "TOF",tofversion);
262 if ( sgnl ){
263 if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n");
264 code = sgnl;
265 goto closeandexit;
266 } else {
267 sgnl = 0;
268 };
269 //
270 // number of events in the file BEFORE the first event of our run
271 //
272 nobefrun = runinfo->GetFirstEntry();
273 //
274 // total number of events in the file
275 //
276 totfileentries = runinfo->GetFileEntries();
277 //
278 // first file entry AFTER the last event of our run
279 //
280 noaftrun = runinfo->GetLastEntry() + 1;
281 //
282 // number of run to be processed
283 //
284 numbofrun = runinfo->GetNoRun();
285 totnorun = runinfo->GetRunEntries();
286 //
287 // Try to access the ToF tree in the file, if it exists we are reprocessing data if not we are processing a new run
288 //
289 toftclone = (TTree*)file->Get("ToF");
290 //
291 if ( !toftclone ){
292 //
293 // tree does not exist, we are not reprocessing
294 //
295 reproc = false;
296 if ( run == 0 && verbose ) printf(" ToF - WARNING: you are reprocessing data but ToF tree does not exist!\n");
297 if ( runinfo->IsReprocessing() && run != 0 && verbose ) printf(" ToF - WARNING: it seems you are not reprocessing data but ToF\n versioning information already exists in RunInfo.\n");
298
299 } else {
300 //
301 // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
302 //
303 toftclone->SetAutoSave(900000000000000LL);
304 reproc = true;
305 //
306 // update versioning information
307 //
308 if ( verbose ) printf("\n Preparing the pre-processing...\n");
309 //
310 if ( run == 0 || totnorun == 1 ){
311 //
312 // we are reprocessing all the file
313 // 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
314 //
315 reprocall = true;
316 //
317 if ( verbose ) printf("\n ToF - WARNING: Reprocessing all runs\n");
318 //
319 } else {
320 //
321 // 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
322 //
323 reprocall = false;
324 //
325 if ( verbose ) printf("\n ToF - WARNING: Reprocessing run number %u \n",run);
326 //
327 // copying old tree to a new file
328 //
329 gSystem->MakeDirectory(toffolder.str().c_str());
330 myfold = true;
331 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
332 temptof = toftclone->CloneTree(-1,"fast");
333 temptof->SetName("ToF-old");
334 tempfile->Write();
335 tempfile->Close();
336 }
337 //
338 // Delete the old tree from old file and memory
339 //
340 toftclone->Delete("all");
341 //
342 if ( verbose ) printf(" ...done!\n");
343 //
344 };
345 //
346 // create ToF detector tree toft
347 //
348 file->cd();
349 toft = new TTree("ToF-new","PAMELA Level2 ToF data");
350 toft->SetAutoSave(900000000000000LL);
351 tof->Set();//ELENA **TEMPORANEO?**
352 toft->Branch("ToFLevel2","ToFLevel2",&tof);
353 //
354 if ( reproc && !reprocall ){
355 //
356 // open new file and retrieve all tree informations
357 //
358 tempfile = new TFile(tempname.str().c_str(),"READ");
359 toftclone = (TTree*)tempfile->Get("ToF-old");
360 toftclone->SetAutoSave(900000000000000LL);
361 toftclone->SetBranchAddress("ToFLevel2",&tofclone);
362 //
363 if ( nobefrun > 0 ){
364 if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");
365 if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
366 if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
367 for (UInt_t j = 0; j < nobefrun; j++){
368 //
369 toftclone->GetEntry(j);
370 //
371 // copy tofclone to tof
372 //
373 tof->Clear();
374 memcpy(&tof,&tofclone,sizeof(tofclone));
375 //
376 // Fill entry in the new tree
377 //
378 toft->Fill();
379 //
380 };
381 if ( verbose ) printf(" Finished successful copying!\n");
382 };
383 };
384 //
385 // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
386 //
387 runlist = runinfo->GetRunList();
388 //
389 // Loop over the run to be processed
390 //
391 for (UInt_t irun=0; irun < numbofrun; irun++){
392 //
393 // retrieve the first run ID to be processed using the RunInfo list
394 //
395 idRun = runlist->At(irun);
396 if ( verbose ) printf("\n\n\n ####################################################################### \n");
397 if ( verbose ) printf(" PROCESSING RUN NUMBER %u \n",idRun);
398 if ( verbose ) printf(" ####################################################################### \n\n\n");
399 //
400 runinfo->ID_ROOT_L0 = 0;
401 //
402 // store in the runinfo class the GL_RUN variables for our run
403 //
404 sgnl = 0;
405 sgnl = runinfo->GetRunInfo(idRun);
406 if ( sgnl ){
407 if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n");
408 code = sgnl;
409 goto closeandexit;
410 } else {
411 sgnl = 0;
412 };
413 //
414 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
415 //
416 if ( runinfo->ID_ROOT_L0 == 0 ){
417 if ( verbose ) printf("\n TOF - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
418 code = -5;
419 goto closeandexit;
420 };
421 //
422 // prepare the timesync for the db
423 //
424 TString host = glt->CGetHost();
425 TString user = glt->CGetUser();
426 TString psw = glt->CGetPsw();
427 TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
428 if ( !dbc->IsConnected() ) throw -314;
429 stringstream myquery;
430 myquery.str("");
431 myquery << "SET time_zone='+0:00'";
432 dbc->Query(myquery.str().c_str());
433 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
434 //
435 // Search in the DB the path and name of the LEVEL0 file to be processed.
436 //
437 // if ( !dbc->IsConnected() ) throw -314;
438 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
439 //
440 ftmpname.str("");
441 ftmpname << glroot->PATH.Data() << "/";
442 ftmpname << glroot->NAME.Data();
443 fname = ftmpname.str().c_str();
444 //
445 // print out informations
446 //
447 totevent = runinfo->NEVENTS;
448 if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data());
449 if ( verbose ) printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
450 if ( verbose ) printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
451 if ( verbose ) printf(" %i events to be processed for run %u: from %i to %i \n\n",totevent,idRun,runinfo->EV_FROM,runinfo->EV_FROM+totevent);
452 //
453 // if ( !totevent ) goto closeandexit;
454 //
455 // Open Level0 file
456 //
457 l0File = new TFile(fname.Data());
458 if ( !l0File ) {
459 if ( verbose ) printf(" TOF - ERROR: problems opening Level0 file\n");
460 code = -6;
461 goto closeandexit;
462 };
463 l0tr = (TTree*)l0File->Get("Physics");
464 if ( !l0tr ) {
465 if ( verbose ) printf(" TOF - ERROR: no Physics tree in Level0 file\n");
466 l0File->Close();
467 code = -7;
468 goto closeandexit;
469 };
470 l0head = l0tr->GetBranch("Header");
471 if ( !l0head ) {
472 if ( verbose ) printf(" TOF - ERROR: no Header branch in Level0 tree\n");
473 l0File->Close();
474 code = -8;
475 goto closeandexit;
476 };
477 l0trig = l0tr->GetBranch("Trigger");
478 if ( !l0trig ) {
479 if ( verbose ) printf(" TOF - ERROR: no Trigger branch in Level0 tree\n");
480 l0File->Close();
481 code = -300;
482 goto closeandexit;
483 };
484 l0tof = l0tr->GetBranch("Tof");
485 if ( !l0tof ) {
486 if ( verbose ) printf(" TOF - ERROR: no ToF branch in Level0 tree\n");
487 l0File->Close();
488 code = -303;
489 goto closeandexit;
490 };
491 //
492 l0tr->SetBranchAddress("Trigger", &trig);
493 l0tr->SetBranchAddress("Tof", &tofEvent);
494 l0tr->SetBranchAddress("Header", &eh);
495 //
496 nevents = l0tof->GetEntries();
497 //
498 if ( nevents < 1 && totevent ) {
499 if ( verbose ) printf(" TOF - ERROR: Level0 file is empty\n\n");
500 l0File->Close();
501 code = -11;
502 goto closeandexit;
503 };
504 //
505 if ( runinfo->EV_TO > nevents-1 && totevent ) {
506 if ( verbose ) printf(" TOF - ERROR: too few entries in the registry tree\n");
507 l0File->Close();
508 code = -12;
509 goto closeandexit;
510 };
511 //
512 // Check if we have to load parameter files (or calibration associated to runs and not to events)
513 //
514 // for example let's assume that we could have different magnetic field maps for different runs:
515 //
516 if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runinfo->RUNHEADER_TIME ) ){
517 trkpar1 = false;
518 // read from DB infos about Magnetic filed maps
519 // if ( !dbc->IsConnected() ) throw -314;
520 glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,1,dbc); // parameters stored in DB in GL_PRAM table
521 tttrkpar1 = glparam->TO_TIME;
522 // ----------------------------
523 // Read the magnetic field
524 // ----------------------------
525 if ( verbose ) printf(" Reading magnetic field maps: \n");
526 trk->LoadField(glparam->PATH+glparam->NAME);
527 if ( verbose ) printf("\n");
528 };
529 //
530 // variable to save information about the tof calibration used
531 //
532 Bool_t defcal = true;
533 //
534 if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){
535 tofpar1 = false;
536 //
537 // if ( !dbc->IsConnected() ) throw -314;
538 Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,201,dbc); // parameters stored in DB in GL_PRAM table
539 if ( error<0 ) {
540 code = error;
541 goto closeandexit;
542 };
543 //
544 if ( verbose ) printf(" Reading ToF parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
545 //
546 if ( (UInt_t)glparam->TO_TIME != (UInt_t)4294967295UL ) defcal = false;
547 //
548 tttofpar1 = glparam->TO_TIME;
549 Int_t nlen = (Int_t)(glparam->PATH+glparam->NAME).Length();
550 rdtofcal((char *)(glparam->PATH+glparam->NAME).Data(),&nlen);
551 //
552 };
553 //
554 Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,204,dbc); // parameters stored in DB in GL_PRAM table
555 if ( error<0 ) {
556 code = error;
557 goto closeandexit;
558 };
559 //
560 if ( verbose ) printf(" Reading ToF attenuation parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
561 tofdedx->ReadParAtt((glparam->PATH+glparam->NAME).Data());
562
563 //
564 error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,205,dbc); // parameters stored in DB in GL_PRAM table
565 if ( error<0 ) {
566 code = error;
567 goto closeandexit;
568 };
569 //
570 if ( verbose ) printf(" Reading ToF desaturation on position parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
571 tofdedx->ReadParPos((glparam->PATH+glparam->NAME).Data());
572
573 //
574 error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,206,dbc); // parameters stored in DB in GL_PRAM table
575 if ( error<0 ) {
576 code = error;
577 goto closeandexit;
578 };
579 //
580 if ( verbose ) printf(" Reading ToF BetheBloch parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
581 tofdedx->ReadParBBneg((glparam->PATH+glparam->NAME).Data());
582
583 //
584 error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,207,dbc); // parameters stored in DB in GL_PRAM table
585 if ( error<0 ) {
586 code = error;
587 goto closeandexit;
588 };
589 //
590 if ( verbose ) printf(" Reading ToF Bethe-Bloch parameter file for beta gt1: %s \n",(glparam->PATH+glparam->NAME).Data());
591 tofdedx->ReadParBBpos((glparam->PATH+glparam->NAME).Data());
592
593 //
594 error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,208,dbc); // parameters stored in DB in GL_PRAM table
595 if ( error<0 ) {
596 code = error;
597 goto closeandexit;
598 };
599 //
600 if ( verbose ) printf(" Reading ToF desaturation on beta parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
601 tofdedx->ReadParDesatBB((glparam->PATH+glparam->NAME).Data());
602
603
604 tofdedx->CheckConnectors(runinfo->RUNHEADER_TIME,glparam,dbc);
605
606 //
607 // WM reading parameter file for dE/dx II order corrections
608 //
609 memset(dedx_corr_m,0,100*48*sizeof(Float_t));
610 memset(dedx_corr,0,48*sizeof(Float_t));
611 memset(mtime,0,100*sizeof(Double_t));
612 //
613 // Query the DB to get the file
614 //
615 error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,203,dbc); // parameters stored in DB in GL_PRAM table
616 if ( error<0 ) {
617 code = error;
618 goto closeandexit;
619 };
620 //
621 if ( verbose ) printf(" Reading ToF dE/dx II order correction parameter file: %s \n",(glparam->PATH+glparam->NAME).Data());
622 //
623 ical=0; // counter set to zero if first-time reading
624 //-----------------------------------------------------------
625 // Here I read the dEdx_korr parameters
626 //-----------------------------------------------------------
627 jj=0;
628 ifstream fin((glparam->PATH+glparam->NAME).Data());
629 UInt_t window = 200000;
630 Bool_t first = true;
631 Bool_t last = true;
632 Float_t sdedx_corr_m[48];
633 memset(sdedx_corr_m,0,48*sizeof(Float_t));
634 Double_t stm = 0;
635 while ( !fin.eof() ){
636 stm = tm;
637 // if ( jj > 0 ) memcpy(sdedx_corr_m,dedx_corr_m[jj-1],48*sizeof(Float_t)); // BUG sdedx should be the previous in time not the previous saved [absurd dE/dx for 8th reduction March and > March 2008 data - fixed on 2009/02/04
638 fin>>t1>>tm>>t2;
639 if ( verbose ) cout << setiosflags(ios::fixed) << setw(10) << setprecision(3) << tm << endl;
640 if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last) ){
641 if ( first ){
642 mtime[jj]=stm;
643 jj++;
644 if ( jj >= 100 ){
645 code = -318;
646 goto closeandexit;
647 };
648 };
649 mtime[jj]=tm;
650 };
651 for (ii=0; ii<48;ii++){
652 fin>>wj>>xmean1>>xwidth1;
653 if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last) ){
654 if ( first ){
655 dedx_corr_m[jj-1][ii]=sdedx_corr_m[ii];
656 };
657 dedx_corr_m[jj][ii]=xmean1;
658 };
659 sdedx_corr_m[ii]=xmean1; // BUG sdedx should be the previous in time not the previous saved [absurd dE/dx for 8th reduction March and > March 2008 data - fixed on 2009/02/04
660 };
661 if ( (tm >= (runinfo->RUNHEADER_TIME-window) && tm <= (runinfo->RUNTRAILER_TIME+window)) || (tm > (runinfo->RUNTRAILER_TIME+window) && last)){
662 if ( first ) first = false;
663 if ( tm > (runinfo->RUNTRAILER_TIME+window) ) last = false;
664 jj++;
665 };
666 if ( jj >= 100 ){
667 code = -318;
668 goto closeandexit;
669 };
670 };
671 //
672 fin.close();
673 // this is a possible bug...
674 // Bool_t ff = false;
675 // while ( runinfo->RUNHEADER_TIME > mtime[ical] && ical < 100 ) {
676 // ical = ical+1;
677 // ff = true;
678 // };
679 while ( (mtime[ical] > runinfo->RUNHEADER_TIME || runinfo->RUNHEADER_TIME > mtime[ical+1] ) && ical < 99 ) {
680 ical = ical+1;
681 // ff = true;
682 };
683 // if ( ff ) ical = ical-1;
684 if ( verbose ) cout<<"rh time "<<runinfo->RUNHEADER_TIME<<" rt time "<<runinfo->RUNTRAILER_TIME<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<< " jj " << jj<< endl;
685 if ( ical < 0 || ical >= 98 ){
686 code = -315;
687 goto closeandexit;
688 };
689 //
690 // run over all the events of the run
691 //
692 if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n");
693 //
694 if ( dbc ){
695 dbc->Close();
696 delete dbc;
697 };
698 //
699 jumped = 0;
700 //
701 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
702 //
703 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
704 //
705 l0head->GetEntry(re);
706 //
707 // absolute time of this event
708 //
709 ph = eh->GetPscuHeader();
710 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
711 //
712 tof->Clear();
713 Int_t pmt_id = 0;
714 ToFPMT *t_pmt = new ToFPMT();
715 if(!(tof->PMT))tof->PMT = new TClonesArray("ToFPMT",12); //ELENA
716 TClonesArray &tpmt = *tof->PMT;
717 ToFTrkVar *t_tof = new ToFTrkVar();
718 if(!(tof->ToFTrk))tof->ToFTrk = new TClonesArray("ToFTrkVar",2); //ELENA
719 TClonesArray &t = *tof->ToFTrk;
720 //
721 // paranoid check
722 //
723 if ( atime > (runinfo->RUNTRAILER_TIME+1) || atime < (runinfo->RUNHEADER_TIME-1) ) {
724 if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n");
725 jumped++;
726 goto jumpev;
727 };
728 //
729 // retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr"
730 //
731 if ( !reprocall ){
732 itr = nobefrun + (re - runinfo->EV_FROM -jumped);
733 } else {
734 itr = runinfo->GetFirstEntry() + (re - runinfo->EV_FROM -jumped);
735 };
736 if ( itr > nevtrkl2 ){ // nevtrkl2 tracker entry number
737 if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr);
738 l0File->Close();
739 code = -313;
740 goto closeandexit;
741 };
742 //
743 trk->Clear();
744 //
745 tracker->GetEntry(itr);
746 ///
747 //
748 l0tof->GetEntry(re);
749 l0trig->GetEntry(re);
750 ///
751 //
752 procev++;
753 //
754 // start processing
755 //
756 // dE/dx II order correction: check time limits and interpolate time correction
757 //==================================================================
758 //== if time is outside time limits:
759 //==================================================================
760 if ( atime<mtime[ical] || atime>mtime[ical+1] ){
761 if ( verbose ) cout<<"Checking Time Limits!"<<endl;
762 ical=0;
763 //Bool_t fg = false;
764 while ( ( mtime[ical] > atime || atime > mtime[ical+1]) && ical < 99 ){
765 ical = ical+1;
766 //fg = true;
767 }
768 // if ( fg ) ical = ical-1;
769 if ( ical < 0 || ical >= 98 ){
770 code = -317;
771 goto closeandexit;
772 };
773 if ( verbose ) cout<<"abs time "<<atime<<" limit low "<<mtime[ical]<<" limit up "<<mtime[ical+1]<<" ical "<<ical<<endl;
774 };
775 //==================================================================
776 //== interpolate betwen time limits
777 //==================================================================
778 thelp1 = mtime[ical];
779 thelp2 = mtime[ical+1];
780 for (ii=0; ii<48;ii++) {
781 yhelp1 = fabs(dedx_corr_m[ical][ii]);
782 if ( yhelp1 < 0.1 ) yhelp1 = 6.;
783 yhelp2 = fabs(dedx_corr_m[ical+1][ii]);
784 if ( yhelp2 < 0.1 ) yhelp2 = 6.;
785 slope = (yhelp2-yhelp1)/(thelp2-thelp1);
786 inter = yhelp1 - slope*thelp1;
787 dedx_corr[ii] = slope*atime + inter;
788 if ( fabs(dedx_corr[ii]) <= 1e-15 ){
789 if ( verbose ) printf("ii %i slope %f atime %u inter %f dedx_corr %f \n",ii,slope,atime,inter,dedx_corr[ii]);
790 if ( verbose ) printf("ical %i yhelp2 %f yhelp1 %f thelp2 %f thelp1 %f \n",ical,yhelp2,yhelp1,thelp2,thelp1);
791 code = -316;
792 goto closeandexit;
793 };
794 };
795 //================================================================
796 //================================================================
797 //
798 // Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure
799 //
800 for (Int_t gg=0; gg<4;gg++){
801 for (Int_t hh=0; hh<12;hh++){
802 tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];
803 tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh];
804 };
805 };
806 //
807 tofdedx->Init(tofEvent);
808 //
809 for (Int_t hh=0; hh<5;hh++){
810 tofinput_.patterntrig[hh]=trig->patterntrig[hh];
811 };
812 //
813 // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related variables.
814 //
815 npmtentry = 0;
816 //
817 ntrkentry = 0;
818 //
819 // Calculate tracks informations from ToF alone
820 //
821 tofl2com();
822 //
823 memcpy(tof->tof_j_flag,tofoutput_.tof_j_flag,6*sizeof(Int_t));
824 //
825 t_tof->trkseqno = -1;
826 //
827 // and now we must copy from the output structure to the level2 class:
828 //
829 t_tof->npmttdc = 0;
830 //
831 for (Int_t hh=0; hh<12;hh++){
832 for (Int_t kk=0; kk<4;kk++){
833 if ( tofoutput_.tofmask[hh][kk] != 0 ){
834 pmt_id = tof->GetPMTid(kk,hh);
835 t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
836 t_tof->tdcflag.AddAt(tofoutput_.tdcflagtof[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
837 t_tof->npmttdc++;
838 };
839 };
840 };
841 for (Int_t kk=0; kk<13;kk++){
842 t_tof->beta[kk] = tofoutput_.betatof_a[kk];
843 }
844 //
845
846 //
847 memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
848 memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
849 memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
850 memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
851 //
852 {
853 Float_t xtof_temp[6]={0.,t_tof->xtofpos[0],t_tof->xtofpos[1],0.,0.,t_tof->xtofpos[2]};
854 Float_t ytof_temp[6]={t_tof->ytofpos[0],0.,0.,t_tof->ytofpos[1],t_tof->ytofpos[2],0.};
855
856 tofdedx->Process(atime,t_tof->beta[12], (Float_t *)xtof_temp,(Float_t *)ytof_temp);
857 }
858 t_tof->npmtadc = 0;
859 for (Int_t hh=0; hh<12;hh++){
860 for (Int_t kk=0; kk<4;kk++){
861 pmt_id = tof->GetPMTid(kk,hh);
862 if ( tofdedx->GetdEdx_pmt(pmt_id)>-1. ){
863 t_tof->dedx.AddAt((tofdedx->GetdEdx_pmt(pmt_id)*36./pow(dedx_corr[pmt_id],2)),t_tof->npmtadc);
864 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
865 t_tof->adcflag.AddAt(0,t_tof->npmtadc); // gf: Jan 09/07
866 t_tof->npmtadc++;
867 };
868 };
869 };
870
871 /*
872 cout<<"ToFCore tofl2com"<<endl;
873 cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
874 cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
875 cout<<tofoutput_.xtr_tof[0]<<" "<<tofoutput_.xtr_tof[1]<<" "<<tofoutput_.xtr_tof[2]<<" "<<tofoutput_.xtr_tof[3]<<" "<<tofoutput_.xtr_tof[4]<<" "<<tofoutput_.xtr_tof[5]<<endl;
876 cout<<tofoutput_.ytr_tof[0]<<" "<<tofoutput_.ytr_tof[1]<<" "<<tofoutput_.ytr_tof[2]<<" "<<tofoutput_.ytr_tof[3]<<" "<<tofoutput_.ytr_tof[4]<<" "<<tofoutput_.ytr_tof[5]<<endl;
877 */
878
879
880 new(t[ntrkentry]) ToFTrkVar(*t_tof);
881 ntrkentry++;
882 t_tof->Clear();
883 //
884 //
885 //
886 t_pmt->Clear();
887 //
888 for (Int_t hh=0; hh<12;hh++){
889 for (Int_t kk=0; kk<4;kk++){
890 // new WM
891 if ( tofoutput_.tdc_c[hh][kk] < 4095 || tofEvent->adc[kk][hh] < 4095 || tofEvent->tdc[kk][hh] < 4095 ){
892 //
893 t_pmt->pmt_id = tof->GetPMTid(kk,hh);
894 t_pmt->tdc_tw = tofoutput_.tdc_c[hh][kk];
895 t_pmt->adc = (Float_t)tofEvent->adc[kk][hh];
896 t_pmt->tdc = (Float_t)tofEvent->tdc[kk][hh];
897 //
898 new(tpmt[npmtentry]) ToFPMT(*t_pmt);
899 npmtentry++;
900 t_pmt->Clear();
901 };
902 };
903 };
904 //
905 if ( debug ) printf(" ATIME %u re %u \n",atime,(UInt_t)re);
906 // Calculate track-related variables
907 //
908 if ( trk->ntrk() > 0 ){
909 //
910 // We have at least one track
911 //
912 //
913 // Run over tracks
914 //
915 for(Int_t nt=0; nt < trk->ntrk(); nt++){
916 //
917 TrkTrack *ptt = trk->GetStoredTrack(nt);
918 //
919 // Copy the alpha vector in the input structure
920 //
921 for (Int_t e = 0; e < 5 ; e++){
922 tofinput_.al_pp[e] = ptt->al[e];
923 };
924 //
925 // Get tracker related variables for this track
926 //
927 toftrk();
928 //
929 // Copy values in the class from the structure (we need to use a temporary class to store variables).
930 //
931 t_tof->npmttdc = 0;
932 for (Int_t hh=0; hh<12;hh++){
933 for (Int_t kk=0; kk<4;kk++){
934 if ( tofoutput_.tofmask[hh][kk] != 0 ){
935 pmt_id = tof->GetPMTid(kk,hh);
936 t_tof->pmttdc.AddAt(pmt_id,t_tof->npmttdc);
937 t_tof->tdcflag.AddAt(tofoutput_.tdcflag[hh][kk],t_tof->npmttdc); // gf: Jan 09/07
938 t_tof->npmttdc++;
939 };
940 };
941 };
942 for (Int_t kk=0; kk<13;kk++){
943 t_tof->beta[kk] = tofoutput_.beta_a[kk];
944 };
945 memcpy(t_tof->xtofpos,tofoutput_.xtofpos,sizeof(t_tof->xtofpos));
946 memcpy(t_tof->ytofpos,tofoutput_.ytofpos,sizeof(t_tof->ytofpos));
947 memcpy(t_tof->xtr_tof,tofoutput_.xtr_tof,sizeof(t_tof->xtr_tof));
948 memcpy(t_tof->ytr_tof,tofoutput_.ytr_tof,sizeof(t_tof->ytr_tof));
949 //
950 tofdedx->Process(atime,t_tof->beta[12], (Float_t *)t_tof->xtr_tof,(Float_t *)t_tof->ytr_tof);
951 t_tof->npmtadc = 0;
952 for (Int_t hh=0; hh<12;hh++){
953 for (Int_t kk=0; kk<4;kk++){
954 pmt_id = tof->GetPMTid(kk,hh);
955 if ( tofdedx->GetdEdx_pmt(pmt_id) > -1. ){
956 t_tof->dedx.AddAt((tofdedx->GetdEdx_pmt(pmt_id)*36./pow(dedx_corr[pmt_id],2)),t_tof->npmtadc);
957 if ( debug ) printf(" nt %i npmtadc %i dedx %f dedx corr %f\n",nt,t_tof->npmtadc,(tofdedx->GetdEdx_pmt(pmt_id)*36./pow(dedx_corr[pmt_id],2)),dedx_corr[pmt_id]);
958 t_tof->pmtadc.AddAt(pmt_id,t_tof->npmtadc);
959 t_tof->adcflag.AddAt(0,t_tof->npmtadc); // gf: Jan 09/07
960 t_tof->npmtadc++;
961 };
962
963 };
964 };
965 //
966
967 /*
968 cout<<"ToFCore toftrk"<<endl;
969 cout<<tofoutput_.xtofpos[0]<<" "<<tofoutput_.xtofpos[1]<<" "<<tofoutput_.xtofpos[2]<<endl;
970 cout<<tofoutput_.ytofpos[0]<<" "<<tofoutput_.ytofpos[1]<<" "<<tofoutput_.ytofpos[2]<<endl;
971 cout<<tofoutput_.xtr_tof[0]<<" "<<tofoutput_.xtr_tof[1]<<" "<<tofoutput_.xtr_tof[2]<<" "<<tofoutput_.xtr_tof[3]<<" "<<tofoutput_.xtr_tof[4]<<" "<<tofoutput_.xtr_tof[5]<<endl;
972 cout<<tofoutput_.ytr_tof[0]<<" "<<tofoutput_.ytr_tof[1]<<" "<<tofoutput_.ytr_tof[2]<<" "<<tofoutput_.ytr_tof[3]<<" "<<tofoutput_.ytr_tof[4]<<" "<<tofoutput_.ytr_tof[5]<<endl;
973 */
974
975 //
976 // Store the tracker track number in order to be sure to have shyncronized data during analysis
977 //
978 t_tof->trkseqno = nt;
979 //
980 // create a new object for this event with track-related variables
981 //
982 new(t[ntrkentry]) ToFTrkVar(*t_tof);
983 ntrkentry++;
984 t_tof->Clear();
985 //
986 }; // loop on all the tracks
987 };
988 //
989 tof->unpackError = tofEvent->unpackError;
990 if ( defcal ){
991 tof->default_calib = 1;
992 } else {
993 tof->default_calib = 0;
994 };
995 //
996 // Fill the rootple
997 //
998 toft->Fill();
999 //
1000 //
1001 //
1002 delete t_tof;
1003 //
1004 //
1005 //
1006 jumpev:
1007 if ( !debug ) debug = false;
1008 //
1009 };
1010 //
1011 // Here you may want to clear some variables before processing another run
1012 //
1013 delete dbtime;
1014 }; // process all the runs
1015 //
1016 if ( verbose ) printf("\n Finished processing data \n");
1017 //
1018 closeandexit:
1019 //
1020 // 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.
1021 //
1022 if ( !reprocall && reproc && code >= 0 ){
1023 if ( totfileentries > noaftrun ){
1024 if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");
1025 if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
1026 if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
1027 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
1028 //
1029 // Get entry from old tree
1030 //
1031 toftclone->GetEntry(j);
1032 //
1033 // copy tofclone to tof
1034 //
1035 tof->Clear();
1036 memcpy(&tof,&tofclone,sizeof(tofclone));
1037 //
1038 // Fill entry in the new tree
1039 //
1040 toft->Fill();
1041 };
1042 if ( verbose ) printf(" Finished successful copying!\n");
1043 };
1044 };
1045 //
1046 // Close files, delete old tree(s), write and close level2 file
1047 //
1048 if ( l0File ) l0File->Close();
1049 if ( tempfile ) tempfile->Close();
1050 if ( myfold ) gSystem->Unlink(tempname.str().c_str());
1051 if ( tracker ) tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file!
1052 //
1053 if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n");
1054 if ( verbose ) printf("\n Writing and closing rootple\n");
1055 if ( runinfo ) runinfo->Close();
1056 if ( toft ) toft->SetName("ToF");
1057 if ( file ){
1058 file->cd();
1059 file->Write("ToF");
1060 };
1061 //
1062 if ( myfold ) gSystem->Unlink(toffolder.str().c_str());
1063 //
1064 // the end
1065 //
1066 if ( verbose ) printf("\n Exiting...\n");
1067 if(toft)toft->Delete();
1068 //
1069 if ( tof ) delete tof;
1070 if ( tofclone ) delete tofclone;
1071 if ( tofdedx ) delete tofdedx;
1072 if ( glroot ) delete glroot;
1073 if ( runinfo ) delete runinfo;
1074 //
1075 if ( code < 0 ) throw code;
1076 return(code);
1077 }

  ViewVC Help
Powered by ViewVC 1.1.23