/[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.12 - (show annotations) (download)
Tue Apr 17 11:58:35 2007 UTC (17 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.11: +1 -1 lines
XXXFolder bug fixed

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 = "S4Folder_"+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 };
187 if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) {
188 verbose = true;
189 };
190 i++;
191 };
192 };
193 // Variables for level2
194 //
195 TTree *S4tr = 0;
196 UInt_t nevents = 0;
197 //
198 // Variables needed to reprocess data
199 //
200 Long64_t maxsize = 10000000000LL;
201 TTree::SetMaxTreeSize(maxsize);
202 //
203 TString S4version;
204 ItoRunInfo *runinfo = 0;
205 TArrayI *runlist = 0;
206 TTree *S4trclone = 0;
207 Bool_t reproc = false;
208 Bool_t reprocall = false;
209 UInt_t nobefrun = 0;
210 UInt_t noaftrun = 0;
211 UInt_t numbofrun = 0;
212 stringstream ftmpname;
213 TString fname;
214 UInt_t totfileentries = 0;
215 UInt_t idRun = 0;
216 Double_t ParamFit0 = 0.;
217 Double_t ParamFit1 = 0.;
218 //
219 // variables needed to handle error signals
220 //
221 Int_t code = 0;
222 Int_t sgnl;
223 //
224 // S4 level2 classes
225 //
226 S4Level2 *s4 = new S4Level2();
227 S4Level2 *s4clone = new S4Level2();
228 //
229 // define variables for opening and reading level0 file
230 //
231 TFile *l0File = 0;
232 TTree *l0tr = 0;
233 TBranch *l0head = 0;
234 TBranch *l0S4 =0;
235 pamela::S4::S4Event *l0s4e = 0;
236 pamela::EventHeader *eh = 0;
237 pamela::PscuHeader *ph = 0;
238 //
239 // Define other basic variables
240 //
241 UInt_t procev = 0;
242 stringstream file2;
243 stringstream file3;
244 stringstream qy;
245 Int_t totevent = 0;
246 UInt_t atime = 0;
247 // Int_t ei = 0;
248 UInt_t re = 0;
249 //
250 // Working filename
251 //
252 TString outputfile;
253 stringstream name;
254 name.str("");
255 name << outDir << "/";
256 //
257 // temporary file and folder
258 //
259 TFile *tempfile = 0;
260 TTree *tempS4 = 0;
261 stringstream tempname;
262 stringstream S4folder;
263 tempname.str("");
264 tempname << outDir;
265 tempname << "/" << processFolder.Data();
266 S4folder.str("");
267 S4folder << tempname.str().c_str();
268 gSystem->MakeDirectory(S4folder.str().c_str());
269 tempname << "/S4tree_run";
270 tempname << run << ".root";
271 //
272 // DB classes
273 //
274 GL_ROOT *glroot = new GL_ROOT();
275 GL_TIMESYNC *dbtime = 0;
276 //
277 // Let's start!
278 //
279 // 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
280 // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file
281 // if it exists we are reprocessing data and we must delete that entries, if not we must create it.
282 //
283 if ( run == 0 ) reproc = true;
284 //
285 //
286 // Output file is "outputfile"
287 //
288 if ( !file->IsOpen() ){
289 //printf(" S4 - ERROR: cannot open file for writing\n");
290 throw -501;
291 };
292 //
293 // Retrieve GL_RUN variables from the level2 file
294 //
295 S4version = S4Info(false); // we should decide how to handle versioning system
296 //
297 // create an interface to RunInfo called "runinfo"
298 //
299 runinfo = new ItoRunInfo(file);
300 //
301 // open "Run" tree in level2 file, if not existing return an error (sngl != 0)
302 //
303 sgnl = 0;
304 sgnl = runinfo->Update(run,"S4",S4version);
305 if ( sgnl ){
306 //printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
307 code = sgnl;
308 goto closeandexit;
309 } else {
310 sgnl = 0;
311 };
312 //
313 // number of events in the file BEFORE the first event of our run
314 //
315 nobefrun = runinfo->GetFirstEntry();
316 //
317 // total number of events in the file
318 //
319 totfileentries = runinfo->GetFileEntries();
320 //
321 // first file entry AFTER the last event of our run
322 //
323 noaftrun = runinfo->GetLastEntry() + 1;
324 //
325 // number of run to be processed
326 //
327 numbofrun = runinfo->GetNoRun();
328 //
329 // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run
330 //
331 S4trclone = (TTree*)file->Get("S4");
332 //
333 if ( !S4trclone ){
334 //
335 // tree does not exist, we are not reprocessing
336 //
337 reproc = false;
338 if ( run == 0 ){
339 if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n");
340 }
341 if ( runinfo->IsReprocessing() && run != 0 ) {
342 if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n");
343 }
344 } else {
345 //
346 // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file?
347 //
348 S4trclone->SetAutoSave(900000000000000LL);
349 reproc = true;
350 //
351 // update versioning information
352 //
353 if (verbose) printf("\n Preparing the pre-processing...\n");
354 //
355 if ( run == 0 ){
356 //
357 // we are reprocessing all the file
358 // 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
359 //
360 reprocall = true;
361 //
362 if (verbose) printf("\n S4 - WARNING: Reprocessing all runs\n");
363 //
364 } else {
365 //
366 // 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
367 //
368 reprocall = false;
369 //
370 if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run);
371 //
372 // copying old tree to a new file
373 //
374 tempfile = new TFile(tempname.str().c_str(),"RECREATE");
375 tempS4 = S4trclone->CloneTree(-1,"fast");
376 tempS4->SetName("S4-old");
377 tempfile->Write();
378 tempfile->Close();
379 }
380 //
381 // Delete the old tree from old file and memory
382 //
383 S4trclone->Delete("all");
384 //
385 if (verbose) printf(" ...done!\n");
386 //
387 };
388 //
389 // create mydetector tree mydect
390 //
391 file->cd();
392 S4tr = new TTree("S4-new","PAMELA Level2 S4 data");
393 S4tr->SetAutoSave(900000000000000LL);
394 S4tr->Branch("S4Level2","S4Level2",&s4);
395 //
396 if ( reproc && !reprocall ){
397 //
398 // open new file and retrieve also tree informations
399 //
400 tempfile = new TFile(tempname.str().c_str(),"READ");
401 S4trclone = (TTree*)tempfile->Get("S4-old");
402 S4trclone->SetAutoSave(900000000000000LL);
403 S4trclone->SetBranchAddress("S4Level2",&s4clone);
404 //
405 if ( nobefrun > 0 ){
406 if (verbose){
407 printf("\n Pre-processing: copying events from the old tree before the processed run\n");
408 printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run);
409 printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun);
410 }
411 for (UInt_t j = 0; j < nobefrun; j++){
412 //
413 S4trclone->GetEntry(j);
414 //
415 // copy s4clone to mydec
416 //
417 s4->Clear();
418 //
419 memcpy(&s4,&s4clone,sizeof(s4clone));
420 //
421 // Fill entry in the new tree
422 //
423 S4tr->Fill();
424 //
425 };
426 if (verbose) printf(" Finished successful copying!\n");
427 };
428 };
429 //
430 // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only.
431 //
432 runlist = runinfo->GetRunList();
433 //
434 // Loop over the run to be processed
435 //
436 for (UInt_t irun=0; irun < numbofrun; irun++){
437 //
438 // retrieve the first run ID to be processed using the RunInfo list
439 //
440 idRun = runlist->At(irun);
441 if (verbose){
442 printf("\n\n\n ####################################################################### \n");
443 printf(" PROCESSING RUN NUMBER %u \n",idRun);
444 printf(" ####################################################################### \n\n\n");
445 }
446 //
447 runinfo->ID_ROOT_L0 = 0;
448 //
449 // store in the runinfo class the GL_RUN variables for our run
450 //
451 sgnl = 0;
452 sgnl = runinfo->GetRunInfo(idRun);
453 if ( sgnl ){
454 if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n");
455 code = sgnl;
456 goto closeandexit;
457 } else {
458 sgnl = 0;
459 };
460 //
461 // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0
462 //
463 if ( runinfo->ID_ROOT_L0 == 0 ){
464 if ( debug ) printf("\n S4 - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun);
465 code = -5;
466 goto closeandexit;
467 };
468 //
469 // prepare the timesync for the db
470 //
471 if ( !dbc->IsConnected() ) throw -504;
472 dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc);
473 //
474 // Search in the DB the path and name of the LEVEL0 file to be processed.
475 //
476 if ( !dbc->IsConnected() ) throw -504;
477 glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc);
478 //
479 ftmpname.str("");
480 ftmpname << glroot->PATH.Data() << "/";
481 ftmpname << glroot->NAME.Data();
482 fname = ftmpname.str().c_str();
483 //
484 // print out informations
485 //
486 totevent = runinfo->NEVENTS;
487 if (verbose){
488 printf("\n LEVEL0 data file: %s \n",fname.Data());
489 printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME);
490 printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME);
491 printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent));
492 }
493 //
494 // Open Level0 file
495 //
496 l0File = new TFile(fname.Data());
497 if ( !l0File ) {
498 if ( debug ) printf(" S4 - ERROR: problems opening Level0 file\n");
499 code = -6;
500 goto closeandexit;
501 };
502 //
503 l0tr = (TTree*)l0File->Get("Physics");
504 if ( !l0tr ) {
505 if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n");
506 l0File->Close();
507 code = -7;
508 goto closeandexit;
509 };
510 l0head = l0tr->GetBranch("Header");
511 if ( !l0head ) {
512 if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n");
513 l0File->Close();
514 code = -8;
515 goto closeandexit;
516 };
517 l0S4 = l0tr->GetBranch("S4");
518 if ( !l0S4 ) {
519 if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n");
520 l0File->Close();
521 code = -503;
522 goto closeandexit;
523 };
524 //
525 l0tr->SetBranchAddress("S4", &l0s4e);
526 l0tr->SetBranchAddress("Header", &eh);
527 //
528 nevents = l0S4->GetEntries();
529 //
530 if ( nevents < 1 ) {
531 if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n");
532 l0File->Close();
533 code = -11;
534 goto closeandexit;
535 };
536 //
537 if ( runinfo->EV_TO > nevents-1 ) {
538 if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n");
539 l0File->Close();
540 code = -12;
541 goto closeandexit;
542 };
543 //
544 // Check if we have to load parameter files (or calibration associated to runs and not to events)
545 // second query: which is the value of paramfit relative to the calibration?
546 //
547 TArrayD *params = 0;
548 params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc);
549 if ( !params ){
550 code = -13;
551 goto closeandexit;
552 };
553 //
554 ParamFit0 = params->At(0);
555 ParamFit1 = params->At(1);
556 //
557 // run over all the events of the run
558 //
559 if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n");
560 //
561 for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){
562 //
563 if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);
564 //
565 l0head->GetEntry(re);
566 //
567 // absolute time of this event
568 //
569 ph = eh->GetPscuHeader();
570 atime = dbtime->DBabsTime(ph->GetOrbitalTime());
571 //
572 // paranoid check
573 //
574 if ( (atime > runinfo->RUNTRAILER_TIME) || (atime < runinfo->RUNHEADER_TIME) ) {
575 if (verbose) printf(" S4 - WARNING: event at time outside the run time window, skipping it\n");
576 goto jumpev;
577 };
578 //
579 procev++;
580 //
581 // start processing
582 //
583 s4->Clear();
584 l0S4->GetEntry(re);
585 if (l0s4e->unpackError == 0){
586 s4->S4adc = l0s4e->S4_DATA;
587 //
588 if ((l0s4e->S4_DATA) > 31 ){
589 s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1;
590 }else{
591 s4->S4calibrated = 0;
592 }
593 };
594 //
595 s4->unpackError = l0s4e->unpackError;
596 //
597 S4tr->Fill();
598 //
599 //
600 jumpev:
601 debug = false;
602 //
603 };
604 //
605 // Here you may want to clear some variables before processing another run
606 //
607 delete dbtime;
608 if ( params ) delete params;
609 //
610 }; // process all the runs
611 //
612 if (verbose) printf("\n Finished processing data \n");
613 //
614 closeandexit:
615 //
616 // 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.
617 //
618 if ( !reprocall && reproc && code >= 0 ){
619 if ( totfileentries > noaftrun ){
620 if (verbose){
621 printf("\n Post-processing: copying events from the old tree after the processed run\n");
622 printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run);
623 printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries);
624 }
625 for (UInt_t j = noaftrun; j < totfileentries; j++ ){
626 //
627 // Get entry from old tree
628 //
629 S4trclone->GetEntry(j);
630 //
631 // copy s4clone to s4
632 //
633 // s4 = new S4Level2();
634 s4->Clear();
635 memcpy(&s4,&s4clone,sizeof(s4clone));
636 //
637 // Fill entry in the new tree
638 //
639 S4tr->Fill();
640 };
641 if (verbose) printf(" Finished successful copying!\n");
642 };
643 };
644 //
645 // Close files, delete old tree(s), write and close level2 file
646 //
647 if ( l0File ) l0File->Close();
648 if ( tempfile ) tempfile->Close();
649 gSystem->Unlink(tempname.str().c_str());
650 //
651 if ( runinfo ) runinfo->Close();
652 if ( S4tr ) S4tr->SetName("S4");
653 if ( file ){
654 file->cd();
655 file->Write();
656 };
657 //
658 gSystem->Unlink(S4folder.str().c_str());
659 //
660 // the end
661 //
662 if (verbose) printf("\n Exiting...\n");
663 if ( S4tr ) S4tr->Delete();
664 //
665 if ( s4 ) delete s4;
666 if ( s4clone ) delete s4clone;
667 if ( glroot ) delete glroot;
668 if ( runinfo ) delete runinfo;
669 //
670 if(code < 0) throw code;
671 return(code);
672 }
673
674

  ViewVC Help
Powered by ViewVC 1.1.23