/[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.8 - (show annotations) (download)
Fri Nov 17 10:08:10 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
CVS Tags: v2r01
Changes since 1.7: +4 -0 lines
Added some checks on MySQL connection

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

  ViewVC Help
Powered by ViewVC 1.1.23