/[PAMELA software]/DarthVader/TrackerLevel2/src/TrkCore.cpp
ViewVC logotype

Contents of /DarthVader/TrackerLevel2/src/TrkCore.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download)
Wed Jun 14 10:01:44 2006 UTC (18 years, 5 months ago) by pam-fi
Branch: MAIN
CVS Tags: v0r02, v1r01beta, v1r00
Changes since 1.2: +6 -3 lines
some methods added

1 /**
2 * \file TrkCore.cpp
3 * \author Elena Vannuccini
4 */
5 // .........................................................
6 // ROOT header files
7 // .........................................................
8 #include <TTree.h>
9 #include <TClassEdit.h>
10 #include <TObject.h>
11 #include <TList.h>
12 #include <TSystem.h>
13 #include <TSystemDirectory.h>
14 #include <TString.h>
15 #include <TFile.h>
16 #include <TClass.h>
17 #include <TCanvas.h>
18 #include <TH1.h>
19 #include <TH1F.h>
20 #include <TH2D.h>
21 #include <TLatex.h>
22 #include <TPad.h>
23 #include <TSQLServer.h>
24 #include <TSQLRow.h>
25 #include <TSQLResult.h>
26 // .........................................................
27 // other general header files
28 // .........................................................
29 #include <fstream>
30 #include <iostream>
31 // .........................................................
32 // files in the inc directory
33 // .........................................................
34 #include <RunInfo.h>
35 #include <GLTables.h>
36 #include <TrkVerl2.h>
37 #include <TrkProcess.h>
38 //#include <TrkStruct.h>
39 #include <TrkLevel2.h>
40 //#include <TrkLevel1.h>
41 #include <TrkLevel0.h>
42 // .........................................................
43 // YODA header files
44 // .........................................................
45 #include <PamelaRun.h>
46 #include <RegistryEvent.h>
47 #include <physics/tracker/TrackerEvent.h>
48 #include <CalibTrk1Event.h>
49 #include <CalibTrk2Event.h>
50
51 // .........................................................
52 // namespaces
53 // .........................................................
54 using namespace std;
55 using namespace pamela;
56 // ================================================================================
57 //
58 //
59 // ================================================================================
60 /**
61 * \brief Tracker data reduction routine.
62 *
63 * It performs data reduction LEVEL0->LEVEL1->LEVEL2, producing LEVEL1 and or LEVEL2 output, in ROOT or HBOOK format.
64 * Input parameters:
65 * @param run id of the run to be processed (if run=0 a whole file is reprocessed)
66 * @param dbc pointer to BD server
67 * @param file1 output LEVEL1 file name (null if no LEVEL1 output is required)
68 * @param file2 output LEVEL2 file name (null if no LEVEL2 output is required)
69 * @param standalone (flag to run the program in standalone mode, that is without reading RunInfo)
70 */
71 //short int TrkCore(Int_t run, TSQLServer *dbc, TString file1, TString file2, Bool_t standalone)
72 int TrkCore(ULong64_t run, TFile *f2, TSQLServer *dbc, int ncustom, char*vcustom[])
73 {
74 // ---------------------------
75 // Define some basic variables
76 // ---------------------------
77
78 TString filename = 0;
79 TString trkcalibfile = 0;
80 Int_t last_trk_calib_used = 0;
81 Long64_t nevents = 0LL;
82
83 // LEVEL0 input
84 TFile *f0 = 0;
85 TFile *f0_c = 0;
86 TTree *physicsTree = 0;
87 TBranch *b_registry = 0;
88 TBranch *b_trk = 0;
89 TBranch *b_header = 0;
90 RegistryEvent *reg = 0;
91 EventHeader *header = 0;
92 PscuHeader *pscu = 0;
93 TrkLevel0 *l0_event = 0;
94 // RunInfo
95 ItoRunInfo *runinfo = 0;
96 TArrayL *runlist = 0;
97 ULong64_t from_run;
98 ULong64_t to_run;
99 Bool_t reprocessing = false;
100 //LEVEL1 output - ROOT
101 // TFile *f1;
102 // TTree *t_level1;
103 // TrkLevel1 *l1_event;
104 //LEVEL2 output - ROOT
105 TTree *t_level2 = 0;
106 TTree *t_clone = 0;
107 TrkLevel2 *l2_event = new TrkLevel2();
108 TrkLevel2 *l2_clone = new TrkLevel2();
109 // -----------------------
110 // -----------------------
111 // Handle input parameters
112 // (data)
113 //
114 // - open/create output files, determining the environment root/hbook from the estension
115 // - create the level1/level2 tree+branch/nt-uple
116 // -----------------------
117 // -----------------------
118 TrkProcess *p = new TrkProcess(run,f2);
119 p->HandleCustomPar(ncustom,vcustom);
120 if( p->DebugMode() )p->Dump();
121
122
123
124 // ===================================
125 // Open/Create level1 output file
126 // (NB output files other than LEVEL2 are
127 // created in a separate folder)
128 // ===================================
129 if(p->get1){
130 if(p->ifroot1){
131 throw -299;
132 }else{
133 throw -299;
134 };
135 };
136 // ===================================
137 // Open/Create level2 output file
138 // ===================================
139 if(p->get2){
140 // if(p->ifroot2){ // root
141 //-------------------------------------------
142 // read RunInfo
143 //-------------------------------------------
144 if(!(p->standalone)){
145 // Open "RunInfo" tree and get list of run
146 runinfo = new ItoRunInfo(f2);
147 char *trkversion = TrkInfo(false);
148 int runinfo_error = runinfo->Update(run,"TRK",trkversion);
149 if( runinfo_error ) throw runinfo_error;
150 runlist = runinfo->GetRunList();
151 reprocessing = runinfo->IsReprocessing();//????
152 if(p->VerboseMode()){
153 cout << "#events "<< runinfo->GetFileEntries() << endl;// #events in the file
154 cout << "#runs "<< runinfo->GetRunEntries() << endl;// #runs in the file
155 cout << "1-st run "<< runlist->At(0) << endl;
156 cout << "last run "<< runlist->At(runinfo->GetRunEntries()-1) << endl;
157 cout << "1-st event "<< runinfo->GetFirstEntry() << endl;// first event of our run
158 cout << "last event+1 "<< runinfo->GetLastEntry() << endl;// first event AFTER the last event of our run
159 cout << "reprocessing "<< runinfo->IsReprocessing() << endl << endl;
160 };
161 };
162 //-------------------------------------------
163 //
164 //-------------------------------------------
165 // Take (if present) the old Tracker tree
166 t_clone = (TTree*)f2->Get("Tracker");
167 if( t_clone != NULL ) t_clone->SetBranchAddress("TrkLevel2",&l2_clone);
168 // Create NEW Tracker tree
169 t_level2 = new TTree("Tracker","PAMELA tracker level2 data ");
170 t_level2->Branch("TrkLevel2","TrkLevel2",&l2_event);
171 // }else{ // hbook
172 // throw -299;
173 // }
174 };
175
176
177 // -------------------------------------------
178 // define runs to be processed/reprocessed
179 // -------------------------------------------
180 if(run == 0){
181 // reprocessing ALL runs
182 if(p->standalone)throw -298; // reprocessing not implemented
183 from_run = runlist->At(0);
184 to_run = runlist->At(runinfo->GetRunEntries()-1);
185 }else{
186 // processing/reprocessing ONE single run
187 from_run = run;
188 to_run = run;
189 };
190
191 //
192 // init "expiration date" of calibrations and parameters
193 //
194 ULong64_t pedsig_time =0ULL;
195 ULong64_t B_time =0ULL;
196 ULong64_t mip_time =0ULL;
197 ULong64_t charge_time =0ULL;
198 ULong64_t eta_time =0ULL;
199 ULong64_t align_time =0ULL;
200 ULong64_t mask_time =0ULL;
201 //
202 // init event counter
203 //
204 Int_t ev_count =0;
205 //
206 // create query-results objects
207 //
208 GL_RUN q1 = GL_RUN();
209 GL_TRK_CALIB q2 = GL_TRK_CALIB();
210 GL_ROOT q3 = GL_ROOT();
211 GL_PARAM q4 = GL_PARAM();
212
213 // ------------------------------------------------------------
214 // if reprocessing one run, copy all the events BEFORE the run
215 // ------------------------------------------------------------
216 if( !(p->standalone) ){
217 for(UInt_t i=0; i<runinfo->GetFirstEntry(); i++){
218 t_clone->GetEntry(i);
219 *l2_event = *l2_clone;
220 t_level2->Fill();
221 l2_event->Clear();
222 // COPY COPY COPY
223 };
224 };
225 // ------------------------------------------------------------
226 // ------------------------------------------------------------
227 // START LOOP OVER RUNS TO PROCESSED/REPROCESSED
228 // ------------------------------------------------------------
229 // ------------------------------------------------------------
230 for(ULong64_t idRun=from_run; idRun <= to_run; idRun++){
231
232 if(p->VerboseMode()) cout << endl<<" ========================= Run: "<< idRun << endl;
233 ULong64_t runheadtime = 0ULL;
234 ULong64_t runtrailtime = 0ULL;
235 UInt_t evfrom = 0;
236 UInt_t evto = 0;
237 Int_t id_reg_run =-1;
238 Int_t trk_calib_used = 0;
239
240 if(p->standalone){
241 // ==============================
242 // first query: retrieve run info
243 // ==============================
244 if (q1.Query_GL_RUN(idRun,dbc) )throw -50;
245 id_reg_run = q1.ID_REG_RUN;
246 runheadtime = q1.RUNHEADER_TIME;
247 runtrailtime = q1.RUNTRAILER_TIME;
248 evfrom = q1.EV_REG_PHYS_FROM;
249 evto = q1.EV_REG_PHYS_TO;
250 trk_calib_used = q1.TRK_CALIB_USED;
251 }else{
252 // ==============================
253 // get run info from RunInfo tree
254 // ==============================
255 int runinfo_error = runinfo->GetRunInfo(idRun);
256 if( runinfo_error ) throw runinfo_error;
257 id_reg_run = runinfo->ID_REG_RUN;
258 runheadtime = runinfo->RUNHEADER_TIME;
259 runtrailtime = runinfo->RUNTRAILER_TIME;
260 evfrom = runinfo->EV_REG_PHYS_FROM;
261 evto = runinfo->EV_REG_PHYS_TO;
262 trk_calib_used = runinfo->TRK_CALIB_USED;
263 };
264 //
265 if(p->VerboseMode()){
266 cout << "ROOT file ID "<< id_reg_run << endl;
267 cout << "RunHeader time "<< runheadtime << endl;
268 cout << "RunTrailer time "<< runtrailtime << endl;
269 cout << " from event "<< evfrom << endl;
270 cout << " to event "<< evto << endl;
271 cout << "trk-calibration used "<< trk_calib_used << endl;
272 };
273 // ========================================================
274 // second query: search the LEVEL0 file that contains idRun
275 // ========================================================
276 TString lastfilename = filename;
277 if( q3.Query_GL_ROOT(id_reg_run,dbc) )throw -51;
278 filename = q3.PATH + q3.NAME;
279 // ========================================================
280 // Open the input LEVEL0 data file
281 // ========================================================
282 if(filename.CompareTo(lastfilename)){
283 if(!lastfilename.IsNull())f0->Close();
284 //if( debug ) cout << "Opening LEVEL0 file: "<< filename << endl;
285 if(p->VerboseMode()) cout << "Opening LEVEL0 file: "<< filename << endl;
286 FileStat_t t;
287 if( gSystem->GetPathInfo(filename.Data(),t) )throw -6;
288 f0 = new TFile(filename);
289 if ( !f0 ) throw -6;
290 physicsTree = (TTree*)f0->Get("Physics"); if(!physicsTree) throw -7;
291 b_header = physicsTree ->GetBranch("Header"); if(!b_header ) throw -8;
292 b_registry = physicsTree ->GetBranch("Registry"); if(!b_registry ) throw -9;
293 b_trk = physicsTree ->GetBranch("Tracker"); if(!b_trk ) throw -203;
294 physicsTree->SetBranchAddress("Tracker" ,&l0_event);
295 physicsTree->SetBranchAddress("Registry",&reg);
296 physicsTree->SetBranchAddress("Header",&header);
297
298 nevents = physicsTree->GetEntries();
299 if ( nevents < 1 ) throw -11;
300 if ( nevents < (evto+1)) throw -12;
301 };
302
303 // =============================================================
304 // retrieve calibration file needed to reduce data
305 // =============================================================
306 // if run OBT is > last calibration "expiration date"
307 // - search for new calibration packet
308 // - load calibration parameters (full + truncated)
309 if(p->VerboseMode() )cout << "Full pedestals for cluster finding:" << endl;
310 if(runheadtime > pedsig_time){
311 if( q2.Query_GL_TRK_CALIB(runheadtime,dbc) )throw -53;
312 pedsig_time = q2.TO_TIME;
313 if(q2.EV_REG_CALIBTRK1 != q2.EV_REG_CALIBTRK2)
314 printf("WARNING!! ---> EV_REG_CALIBTRK1=%d it's different from EV_REG_CALIBTRK2=%d \n\n",q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
315 if( q3.Query_GL_ROOT(q2.ID_REG_CALIBTRK,dbc) )throw -51;
316 trkcalibfile = q3.PATH + q3.NAME;
317 if(trkcalibfile.CompareTo(filename)){
318 // if( f0_c->IsOpen() )f0_c->Close();
319 if(p->VerboseMode()) cout << "Opening LEVEL0 file: "<< trkcalibfile << endl;
320 FileStat_t t;
321 if( gSystem->GetPathInfo(trkcalibfile.Data(),t) )throw -6;
322 f0_c=new TFile(trkcalibfile);
323 if ( !f0_c ) throw -6;
324 }else{
325 f0_c = f0;
326 };
327 if(p->VerboseMode()) {
328 cout << "Loading calibration entries "<< q2.EV_REG_CALIBTRK1 << " " << q2.EV_REG_CALIBTRK2;
329 cout << " (from time "<< q2.FROM_TIME <<" to time "<< q2.TO_TIME <<")"<<endl;
330 };
331 pedsigbad_.FillACalibFrom(f0_c,q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
332 };
333 if( p->DebugMode() ) for(int i=0; i<12; i++) cout << " DSP "<< i << " "<< pedsigbad_.pedestal[64][12][i] << endl;
334 // =============================================================
335 // retrieve calibration file needed to uncompress data
336 // =============================================================
337 // if the run was compressed using default calib
338 // load truncated pedestals from default
339 // otherwise reload them from on-line calibration
340 if(p->VerboseMode() )cout << "Truncated pedestals for uncompression:";
341 if(trk_calib_used==104 && last_trk_calib_used!=104){
342
343 if(p->VerboseMode() )cout << ">> Loading default calibration " << endl;
344 if (q4.Query_GL_PARAM(runheadtime,"default calibration",dbc) )throw -52;
345 pedsigbad_.FillTCalibFrom(q4.PATH+q4.NAME);
346
347 }else if( (trk_calib_used==1 | trk_calib_used==2) && last_trk_calib_used==104 ){
348
349 if(p->VerboseMode()) cout << ">> Re-loading on-line calibration " << endl;
350 pedsigbad_.FillTCalibFrom(f0_c,q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
351 }else{
352 if(p->VerboseMode()) cout << ">> Using on-line calibration " << endl;
353 };
354 if( p->DebugMode() ) for(int i=0; i<12; i++) cout << " DSP "<< i << " "<< pedsigbad_.pedestal_t[64][12][i] << endl;
355 last_trk_calib_used = trk_calib_used;
356
357 /* // =============================================================
358 // retrieve calibration file needed to uncompress data
359 // =============================================================
360 if(trk_calib_used==104 && last_trk_calib_used!=104){
361
362 if(p->VerboseMode() )cout << "** Using default calibration **" << endl;
363
364 if (q4.Query_GL_PARAM(runheadtime,"default calibration",dbc) )throw -52;
365 path_.FillWith(q4.PATH+q4.NAME);
366 fillpedsigfromdefault_();
367 if(path_.error) throw -216;
368
369 }else if( (trk_calib_used==1 | trk_calib_used==2) && runheadtime > pedsig_time){
370
371 if( q2.Query_GL_TRK_CALIB(runheadtime,dbc) )throw -53;
372 pedsig_time = q2.TO_TIME;
373 if(q2.EV_REG_CALIBTRK1 != q2.EV_REG_CALIBTRK2)
374 printf("WARNING!! ---> EV_REG_CALIBTRK1=%d it's different from EV_REG_CALIBTRK2=%d \n\n",q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
375
376 if( q3.Query_GL_ROOT(q2.ID_REG_CALIBTRK,dbc) )throw -51;
377
378 trkcalibfile = q3.PATH + q3.NAME;
379 // store calibration informations
380 if(p->VerboseMode()) cout << "Online calibration: "<< trkcalibfile << endl;
381 // pedsigbad_ = TrkFlightPede(trkcalibfile,q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
382 TFile* f0_c= f0;
383 if(trkcalibfile.CompareTo(filename))f0_c = new TFile(trkcalibfile);
384
385 pedsigbad_.FillAllFrom(f0_c,q2.EV_REG_CALIBTRK1,q2.EV_REG_CALIBTRK2);
386
387 if(trkcalibfile.CompareTo(filename))f0_c->Close();
388 };
389 last_trk_calib_used = trk_calib_used;*/
390 // =============================================================
391 // retrieve information about parameters to process LEVEL2
392 // =============================================================
393 //
394 // magnetic field
395 //
396 if(p->VerboseMode()) cout << "Loading parameter files : "<< endl;
397
398 if(runheadtime > B_time){
399 if( q4.Query_GL_PARAM(runheadtime,"field",dbc) )throw -52;
400 B_time = q4.TO_TIME;
401 l2_event->LoadField(q4.PATH+q4.NAME);
402 // l2_event->LoadField(q4.PATH+q4.NAME);
403 if(path_.error) throw -215;
404 };
405 //
406 // mip conversion parameters
407 //
408 if(runheadtime > mip_time){
409 if( q4.Query_GL_PARAM(runheadtime,"trk mip",dbc) )throw -52;
410 mip_time = q4.TO_TIME;
411 path_.FillWith(q4.PATH+q4.NAME);
412 readmipparam_();
413 if(path_.error) throw -212;
414 };
415 //
416 // charge correlation parameters
417 //
418 if(runheadtime > charge_time){
419 if( q4.Query_GL_PARAM(runheadtime,"trk charge",dbc) )throw -52;
420 charge_time = q4.TO_TIME;
421 path_.FillWith(q4.PATH+q4.NAME);
422 readchargeparam_();
423 if(path_.error) throw -213;
424 };
425 //
426 // eta p.f.a. parameters
427 //
428 if(runheadtime > eta_time){
429 if( q4.Query_GL_PARAM(runheadtime,"trk pfa",dbc) )throw -52;
430 eta_time = q4.TO_TIME;
431 path_.FillWith(q4.PATH+q4.NAME);
432 readetaparam_();
433 if(path_.error) throw -214;
434 };
435 //
436 // alignment parameters
437 //
438 if(runheadtime > align_time){
439 if( q4.Query_GL_PARAM(runheadtime,"trk alignment",dbc) )throw -52;
440 align_time = q4.TO_TIME;
441 path_.FillWith(q4.PATH+q4.NAME);
442 readalignparam_();
443 if(path_.error) throw -211;
444 };
445 //
446 // viking mask
447 //
448 if(runheadtime > mask_time){
449 if( q4.Query_GL_PARAM(runheadtime,"trk mask",dbc) )throw -52;
450 mask_time = q4.TO_TIME;
451 path_.FillWith(q4.PATH+q4.NAME);
452 readvkmask_();
453 if(path_.error) throw -210;
454 };
455
456 //
457 // ====================================================
458 // start looping on events cointained in the data file
459 // ====================================================
460 for (UInt_t re = evfrom; re <= evto; re++){
461
462 ev_count++;
463 // if ( p->DEBUG && re%100 == 0 && re > 0 ) printf(" %iK \n",re/1000);
464 if ( p->DebugMode() && re%100 == 0 && re > 0 ) cout << ".";
465
466 b_registry->GetEntry(re);
467 b_trk->GetEntry(reg->event);
468 b_header->GetEntry(reg->event);
469 pscu = header->GetPscuHeader();
470 // pscu->Print();
471 // cout <<endl<< " *************** Pkt ID "<< (UINT32)pscu->Counter << endl;
472
473 p->ProcessEvent(l0_event);
474
475 // ----------------
476 // LEVEL1 output
477 // ----------------
478 if(p->get1){
479 if(p->ifroot1){ // root
480 }else{ // hbook
481 };
482 };
483 // ----------------
484 // LEVEL2 output
485 // ----------------
486 if(p->get2){
487 // if(p->ifroot2){ // root
488 l2_event->FillCommonVar(&level2event_);
489 // l2_event->Dump();
490 t_level2->Fill();
491 l2_event->Clear();
492 // }else{ // hbook
493 // }
494 };
495
496 }; // end loop on events
497 if(p->VerboseMode())cout << " >>> processed "<< ev_count <<" events"<< endl;
498 }; // end loop on runs
499 // ------------------------------------------------------------
500 // if reprocessing one run, copy all the events AFTER the run
501 // ------------------------------------------------------------
502 if( !(p->standalone) ){
503 for(UInt_t i=runinfo->GetLastEntry()+1; i<runinfo->GetFileEntries(); i++){
504 t_clone->GetEntry(i);
505 *l2_event = *l2_clone;
506 t_level2->Fill();
507 l2_event->Clear();
508 // COPY COPY COPY
509 };
510 };
511 // ---------------
512 // close the files
513 // ---------------
514 if(p->get1){
515 if(p->ifroot1){ // root
516 }else{ // hbook
517 }
518 };
519 if(p->get2){
520 // if(p->ifroot2){ // root
521 if( t_clone )t_clone->Delete("all");//delete old tree from file
522 if( !(p->standalone) )runinfo->Close();
523 f2->Write("Tracker");
524 if( t_level2 )t_level2->Delete(); //delete new tree from memory
525
526 // }else{ // hbook
527 // }
528 };
529 if( f0->IsOpen() ) f0->Close();
530 if( f0_c->IsOpen() )f0_c->Close();
531
532 return(p->ostatus);
533 }
534

  ViewVC Help
Powered by ViewVC 1.1.23