/[PAMELA software]/DarthVader/S4Level2/src/S4Core.cpp
ViewVC logotype

Contents of /DarthVader/S4Level2/src/S4Core.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations) (download)
Tue May 22 08:15:07 2007 UTC (17 years, 8 months ago) by mocchiut
Branch: MAIN
CVS Tags: v3r06
Changes since 1.14: +2 -1 lines
Reprocessing tool tuning

1 //
2 // Given a calibration and a data file this program create an ntuple with LEVEL2 S4 variables
3 //
4 //
5 // C/C++ headers
6 //
7 #include <fstream>
8 #include <string.h>
9 #include <iostream>
10 #include <cstring>
11 #include <stdio.h>
12 //
13 // ROOT headers
14 //
15 #include <TGraph.h>
16 #include <TF1.h>
17 #include <TTree.h>
18 #include <TClassEdit.h>
19 #include <TObject.h>
20 #include <TList.h>
21 #include <TArrayI.h>
22 #include <TArrayD.h>
23 #include <TSystem.h>
24 #include <TSystemDirectory.h>
25 #include <TString.h>
26 #include <TFile.h>
27 #include <TClass.h>
28 #include <TSQLServer.h>
29 #include <TSQLRow.h>
30 #include <TSQLResult.h>
31 #include <TClonesArray.h>
32 #include <stdlib.h>
33 #include <math.h>
34 //
35 // RunInfo header
36 //
37 #include <RunInfo.h>
38 #include <GLTables.h>
39 //
40 // YODA headers
41 //
42 #include <PamelaRun.h>
43 #include <PscuHeader.h>
44 #include <PscuEvent.h>
45 #include <EventHeader.h>
46 #include <CalibS4Event.h>
47 #include <physics/S4/S4Event.h>
48 //
49 // This program headers
50 //
51 #include <S4Level2.h>
52 #include <S4Core.h>
53 #include <S4Verl2.h>
54 //
55 using namespace std;
56 //
57 /*
58 * Fit function Received from Valeria Malvezzi 06/02/2006
59 */
60 Double_t fitf(Double_t *x, Double_t *par){
61 Double_t fitval =(par[0]*x[0])+par[1];
62 return fitval;
63 }
64
65 /*
66 * Fit the S4 calibration with a straight line - Received from Valeria Malvezzi 06/02/2006
67 */
68 TArrayD *S4_paramfit(UInt_t atime, TSQLServer *dbc){
69 //
70 TArrayD *parametri = new TArrayD(2);
71 //
72 GL_S4_CALIB *glS4calib = new GL_S4_CALIB();
73 //
74 if ( !dbc->IsConnected() ) throw -504;
75 glS4calib->Query_GL_S4_CALIB(atime, dbc);
76 //
77 GL_ROOT *glroot = new GL_ROOT();
78 if ( !dbc->IsConnected() ) throw -504;
79 glroot->Query_GL_ROOT(glS4calib->ID_ROOT_L0,dbc);
80 //
81 stringstream ftmpname;
82 ftmpname.str("");
83 ftmpname << glroot->PATH.Data() << "/";
84 ftmpname << glroot->NAME.Data();
85 //
86 TFile *file = 0;
87 file = new TFile(ftmpname.str().c_str());
88 //
89 if ( !file ) return(NULL);
90 //
91 TTree *tr = 0;
92 tr = (TTree*)file->Get("CalibS4");
93 if ( !tr ){
94 file->Close();
95 return(NULL);
96 };
97 //
98 pamela::CalibS4Event *S4CalibEvent = 0;
99 tr->SetBranchAddress("CalibS4", &S4CalibEvent);
100 if ( tr->GetEntries() < glS4calib->EV_ROOT ) return(NULL);
101 //
102 tr->GetEntry(glS4calib->EV_ROOT);
103 //
104 // Variables initialization
105 //
106 Double_t mip[3]={1, 30, 300};
107 Double_t adc[3] = {0.,0.,0.};
108 //
109 // Fit calibrations and find parameters to calibrate data
110 //
111 pamela::S4::S4Event *s4Record = 0;
112 //
113 for (Int_t j = 0; j < 4; j++){
114 for (Int_t i = 0; i < 128; i++){
115 s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i));
116 switch (j) {
117 case 0 :{
118 adc[0]=adc[0]+((s4Record->S4_DATA)-32);
119 break;
120 };
121 case 1 :{
122 adc[1]=adc[1]+((s4Record->S4_DATA)-32);
123 break;
124 };
125 case 3 :{
126 adc[2]=adc[2]+((s4Record->S4_DATA)-32);
127 break;
128 };
129 };
130 };
131 };
132 //
133 adc[0]=adc[0]/128;
134 adc[1]=adc[1]/128;
135 adc[2]=adc[2]/128;
136 // if ( IsDebug() ) printf(" adc1 = %g adc2 = %g adc3 = %g\n",adc[0],adc[1],adc[2]);
137 TGraph *fitpar = new TGraph (3, adc, mip);
138 TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // function definition with 2 parameters
139 //
140 func->SetParameters(0.3,1.); // parameters initialization to 1
141 func->SetParNames("m","q"); //parameter's name
142 fitpar->Fit(func,"qr"); //function fit
143 parametri->AddAt(func->GetParameter(0),0);
144 parametri->AddAt(func->GetParameter(1),1);
145 // if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.7 || parametri[1] > 1.1 ) valid = 0;
146 //
147 delete glroot;
148 delete glS4calib;
149 delete fitpar;
150 delete func;
151 file->Close();
152 //
153 return parametri;
154 };
155
156 //
157 // CORE ROUTINE
158 //
159 //
160 int S4Core(UInt_t run, TFile *file, TSQLServer *dbc, Int_t S4argc, char *S4argv[]){
161 //
162 // Set these to true to have a verbose output.
163 //
164 Bool_t debug = false;
165 Bool_t verbose = false;
166 //
167 //
168 // Output directory is the working directoy.
169 //
170 const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath()));
171 //
172 Int_t i = 0;
173 TString processFolder = Form("S4Folder_%u",run);
174 if ( S4argc > 0 ){
175 i = 0;
176 while ( i < S4argc ){
177 if ( !strcmp(S4argv[i],"-processFolder") ) {
178 if ( S4argc < i+1 ){
179 throw -3;
180 };
181 processFolder = (TString)S4argv[i+1];
182 i++;
183 };
184 if ( (!strcmp(S4argv[i],"--debug")) || (!strcmp(S4argv[i],"-g"))) {
185 verbose = true;
186 debug = true;
187 };
188 if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) {
189 verbose = true;
190 };
191 i++;
192 };
193 };
194 // Variables for level2
195 //
196 TTree *S4tr = 0;
197 UInt_t nevents = 0;
198 //
199 // Variables needed to reprocess data
200 //
201 Long64_t maxsize = 10000000000LL;
202 TTree::SetMaxTreeSize(maxsize);
203 //
204 TString S4version;
205 ItoRunInfo *runinfo = 0;
206 TArrayI *runlist = 0;
207 TTree *S4trclone = 0;
208 Bool_t reproc = false;
209 Bool_t reprocall = false;
210 UInt_t nobefrun = 0;
211 UInt_t noaftrun = 0;
212 UInt_t numbofrun = 0;
213 stringstream ftmpname;
214 TString fname;
215 UInt_t totfileentries = 0;
216 UInt_t idRun = 0;
217 Double_t ParamFit0 = 0.;
218 Double_t ParamFit1 = 0.;
219 //
220 // variables needed to handle error signals
221 //
222 Int_t code = 0;
223 Int_t sgnl;
224 //
225 // S4 level2 classes
226 //
227 S4Level2 *s4 = new S4Level2();
228 S4Level2 *s4clone = new S4Level2();
229 //
230 // define variables for opening and reading level0 file
231 //
232 TFile *l0File = 0;
233 TTree *l0tr = 0;
234 TBranch *l0head = 0;
235 TBranch *l0S4 =0;
236 pamela::S4::S4Event *l0s4e = 0;
237 pamela::EventHeader *eh = 0;
238 pamela::PscuHeader *ph = 0;
239 //
240 // Define other basic variables
241 //
242 UInt_t procev = 0;
243 stringstream file2;
244 stringstream file3;
245 stringstream qy;
246 Int_t totevent = 0;
247 UInt_t atime = 0;
248 // Int_t ei = 0;
249 UInt_t re = 0;
250 //
251 // Working filename
252 //
253 TString outputfile;
254 stringstream name;
255 name.str("");
256 name << outDir << "/";
257 //
258 // temporary file and folder
259 //
260 TFile *tempfile = 0;
261 TTree *tempS4 = 0;
262 stringstream tempname;
263 stringstream S4folder;
264 tempname.str("");
265 tempname << outDir;
266 tempname << "/" << processFolder.Data();
267 S4folder.str("");
268 S4folder << tempname.str().c_str();
269 gSystem->MakeDirectory(S4folder.str().c_str());
270 tempname << "/S4tree_run";
271 tempname << run << ".root";
272 //
273 // DB classes
274 //
275 GL_ROOT *glroot = new GL_ROOT();
276 GL_TIMESYNC *dbtime = 0;
277 //
278 // Let's start!
279 //
280 // 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
281 // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file
282 // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
283 //
284 if ( run == 0 ) reproc = true;
285 //
286 //
287 // Output file is "outputfile"
288 //
289 if ( !file->IsOpen() ){
290 //printf(" S4 - ERROR: cannot open file for writing\n");
291 throw -501;
292 };
293 //
294 // Retrieve GL_RUN variables from the level2 file
295 //
296 S4version = S4Info(false); // we should decide how to handle versioning system
297 //
298 // create an interface to RunInfo called "runinfo"
299 //
300 runinfo = new ItoRunInfo(file);
301 //
302 // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
303 //
304 sgnl = 0;
305 sgnl = runinfo->Update(run,"S4",S4version);
306 if ( sgnl ){
307 //printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
308 code = sgnl;
309 goto closeandexit;
310 } else {
311 sgnl = 0;
312 };
313 //
314 // number of events in the file BEFORE the first event of our run
315 //
316 nobefrun = runinfo->GetFirstEntry();
317 //
318 // total number of events in the file
319 //
320 totfileentries = runinfo->GetFileEntries();
321 //
322 // first file entry AFTER the last event of our run
323 //
324 noaftrun = runinfo->GetLastEntry() + 1;
325 //
326 // number of run to be processed
327 //
328 numbofrun = runinfo->GetNoRun();
329 UInt_t totnorun = runinfo->GetRunEntries();
330 //
331 // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run
332 //
333 S4trclone = (TTree*)file->Get("S4");
334 //
335 if ( !S4trclone ){
336 //
337 // tree does not exist, we are not reprocessing
338 //
339 reproc = false;
340 if ( run == 0 ){
341 if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n");
342 }
343 if ( runinfo->IsReprocessing() && run != 0 ) {
344 if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n");
345 }
346 } else {
347 //
348 // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
349 //
350 S4trclone->SetAutoSave(900000000000000LL);
351 reproc = true;
352 //
353 // update versioning information
354 //
355 if (verbose) printf("\n Preparing the pre-processing...\n");
356 //
357 if ( run == 0 || totnorun == 1 ){
358 //
359 // we are reprocessing all the file
360 // 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
361 //
362 reprocall = true;
363 //
364 if (verbose) printf("\n S4 - WARNING: Reprocessing all runs\n");
365 //
366 } else {
367 //
368 // 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
369 //
370 reprocall = false;
371 //
372 if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run);
373 //
374 // copying old tree to a new file
375 //
376 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
377 tempS4 = S4trclone->CloneTree(-1,"fast");
378 tempS4->SetName("S4-old");
379 tempfile->Write();
380 tempfile->Close();
381 }
382 //
383 // Delete the old tree from old file and memory
384 //
385 S4trclone->Delete("all");
386 //
387 if (verbose) printf(" ...done!\n");
388 //
389 };
390 //
391 // create mydetector tree mydect
392 //
393 file->cd();
394 S4tr = new TTree("S4-new","PAMELA Level2 S4 data");
395 S4tr->SetAutoSave(900000000000000LL);
396 S4tr->Branch("S4Level2","S4Level2",&s4);
397 //
398 if ( reproc && !reprocall ){
399 //
400 // open new file and retrieve also tree informations
401 //
402 tempfile = new TFile(tempname.str().c_str(),"READ");
403 S4trclone = (TTree*)tempfile->Get("S4-old");
404 S4trclone->SetAutoSave(900000000000000LL);
405 S4trclone->SetBranchAddress("S4Level2",&s4clone);
406 //
407 if ( nobefrun > 0 ){
408 if (verbose){
409 printf("\n Pre-processing: copying events from the old tree before the processed run\n");
410 printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
411 printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
412 }
413 for (UInt_t j = 0; j < nobefrun; j++){
414 //
415 S4trclone->GetEntry(j);
416 //
417 // copy s4clone to mydec
418 //
419 s4->Clear();
420 //
421 memcpy(&s4,&s4clone,sizeof(s4clone));
422 //
423 // Fill entry in the new tree
424 //
425 S4tr->Fill();
426 //
427 };
428 if (verbose) printf(" Finished successful copying!\n");
429 };
430 };
431 //
432 // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
433 //
434 runlist = runinfo->GetRunList();
435 //
436 // Loop over the run to be processed
437 //
438 for (UInt_t irun=0; irun < numbofrun; irun++){
439 //
440 // retrieve the first run ID to be processed using the RunInfo list
441 //
442 idRun = runlist->At(irun);
443 if (verbose){
444 printf("\n\n\n ####################################################################### \n");
445 printf(" PROCESSING RUN NUMBER %u \n",idRun);
446 printf(" ####################################################################### \n\n\n");
447 }
448 //
449 runinfo->ID_ROOT_L0 = 0;
450 //
451 // store in the runinfo class the GL_RUN variables for our run
452 //
453 sgnl = 0;
454 sgnl = runinfo->GetRunInfo(idRun);
455 if ( sgnl ){
456 if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
457 code = sgnl;
458 goto closeandexit;
459 } else {
460 sgnl = 0;
461 };
462 //
463 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
464 //
465 if ( runinfo->ID_ROOT_L0 == 0 ){
466 if ( debug ) printf("\n S4 - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
467 code = -5;
468 goto closeandexit;
469 };
470 //
471 // prepare the timesync for the db
472 //
473 if ( !dbc->IsConnected() ) throw -504;
474 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
475 //
476 // Search in the DB the path and name of the LEVEL0 file to be processed.
477 //
478 if ( !dbc->IsConnected() ) throw -504;
479 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
480 //
481 ftmpname.str("");
482 ftmpname << glroot->PATH.Data() << "/";
483 ftmpname << glroot->NAME.Data();
484 fname = ftmpname.str().c_str();
485 //
486 // print out informations
487 //
488 totevent = runinfo->NEVENTS;
489 if (verbose){
490 printf("\n LEVEL0 data file: %s \n",fname.Data());
491 printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
492 printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
493 printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent));
494 }
495 //
496 // Open Level0 file
497 //
498 l0File = new TFile(fname.Data());
499 if ( !l0File ) {
500 if ( debug ) printf(" S4 - ERROR: problems opening Level0 file\n");
501 code = -6;
502 goto closeandexit;
503 };
504 //
505 l0tr = (TTree*)l0File->Get("Physics");
506 if ( !l0tr ) {
507 if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n");
508 l0File->Close();
509 code = -7;
510 goto closeandexit;
511 };
512 l0head = l0tr->GetBranch("Header");
513 if ( !l0head ) {
514 if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n");
515 l0File->Close();
516 code = -8;
517 goto closeandexit;
518 };
519 l0S4 = l0tr->GetBranch("S4");
520 if ( !l0S4 ) {
521 if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n");
522 l0File->Close();
523 code = -503;
524 goto closeandexit;
525 };
526 //
527 l0tr->SetBranchAddress("S4", &l0s4e);
528 l0tr->SetBranchAddress("Header", &eh);
529 //
530 nevents = l0S4->GetEntries();
531 //
532 if ( nevents < 1 ) {
533 if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");
534 l0File->Close();
535 code = -11;
536 goto closeandexit;
537 };
538 //
539 if ( runinfo->EV_TO > nevents-1 ) {
540 if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");
541 l0File->Close();
542 code = -12;
543 goto closeandexit;
544 };
545 //
546 // Check if we have to load parameter files (or calibration associated to runs and not to events)
547 // second query: which is the value of paramfit relative to the calibration?
548 //
549 TArrayD *params = 0;
550 params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc);
551 if ( !params ){
552 code = -13;
553 goto closeandexit;
554 };
555 //
556 ParamFit0 = params->At(0);
557 ParamFit1 = params->At(1);
558 //
559 // run over all the events of the run
560 //
561 if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
562 //
563 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
564 //
565 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
566 //
567 l0head->GetEntry(re);
568 //
569 // absolute time of this event
570 //
571 ph = eh->GetPscuHeader();
572 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
573 //
574 // paranoid check
575 //
576 if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME) ) {
577 if (verbose) printf(" S4 - WARNING: event at time outside the run time window, skipping it\n");
578 goto jumpev;
579 };
580 //
581 procev++;
582 //
583 // start processing
584 //
585 s4->Clear();
586 l0S4->GetEntry(re);
587 if (l0s4e->unpackError == 0){
588 s4->S4adc = l0s4e->S4_DATA;
589 //
590 if ((l0s4e->S4_DATA) > 31 ){
591 s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;
592 }else{
593 s4->S4calibrated = 0;
594 }
595 };
596 //
597 s4->unpackError = l0s4e->unpackError;
598 //
599 S4tr->Fill();
600 //
601 //
602 jumpev:
603 debug = false;
604 //
605 };
606 //
607 // Here you may want to clear some variables before processing another run
608 //
609 delete dbtime;
610 if ( params ) delete params;
611 //
612 }; // process all the runs
613 //
614 if (verbose) printf("\n Finished processing data \n");
615 //
616 closeandexit:
617 //
618 // 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.
619 //
620 if ( !reprocall && reproc && code >= 0 ){
621 if ( totfileentries > noaftrun ){
622 if (verbose){
623 printf("\n Post-processing: copying events from the old tree after the processed run\n");
624 printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
625 printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
626 }
627 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
628 //
629 // Get entry from old tree
630 //
631 S4trclone->GetEntry(j);
632 //
633 // copy s4clone to s4
634 //
635 // s4 = new S4Level2();
636 s4->Clear();
637 memcpy(&s4,&s4clone,sizeof(s4clone));
638 //
639 // Fill entry in the new tree
640 //
641 S4tr->Fill();
642 };
643 if (verbose) printf(" Finished successful copying!\n");
644 };
645 };
646 //
647 // Close files, delete old tree(s), write and close level2 file
648 //
649 if ( l0File ) l0File->Close();
650 if ( tempfile ) tempfile->Close();
651 gSystem->Unlink(tempname.str().c_str());
652 //
653 if ( runinfo ) runinfo->Close();
654 if ( S4tr ) S4tr->SetName("S4");
655 if ( file ){
656 file->cd();
657 file->Write();
658 };
659 //
660 gSystem->Unlink(S4folder.str().c_str());
661 //
662 // the end
663 //
664 if (verbose) printf("\n Exiting...\n");
665 if ( S4tr ) S4tr->Delete();
666 //
667 if ( s4 ) delete s4;
668 if ( s4clone ) delete s4clone;
669 if ( glroot ) delete glroot;
670 if ( runinfo ) delete runinfo;
671 //
672 if(code < 0) throw code;
673 return(code);
674 }
675
676

  ViewVC Help
Powered by ViewVC 1.1.23