| 1 | // | // | 
| 2 |  | // Given a calibration and a data file this program create an ntuple with LEVEL2 S4 variables | 
| 3 |  | // | 
| 4 |  | // | 
| 5 | // C/C++ headers | // C/C++ headers | 
| 6 | // | // | 
| 7 | #include <fstream> | #include <fstream> | 
| 12 | // | // | 
| 13 | // ROOT headers | // ROOT headers | 
| 14 | // | // | 
| 15 |  | #include <TGraph.h> | 
| 16 |  | #include <TF1.h> | 
| 17 | #include <TTree.h> | #include <TTree.h> | 
| 18 | #include <TClassEdit.h> | #include <TClassEdit.h> | 
| 19 | #include <TObject.h> | #include <TObject.h> | 
| 20 | #include <TList.h> | #include <TList.h> | 
| 21 | #include <TArrayL.h> | #include <TArrayI.h> | 
| 22 |  | #include <TArrayD.h> | 
| 23 | #include <TSystem.h> | #include <TSystem.h> | 
| 24 | #include <TSystemDirectory.h> | #include <TSystemDirectory.h> | 
| 25 | #include <TString.h> | #include <TString.h> | 
| 32 | #include <stdlib.h> | #include <stdlib.h> | 
| 33 | #include <math.h> | #include <math.h> | 
| 34 | // | // | 
| 35 |  | // RunInfo header | 
| 36 |  | // | 
| 37 |  | #include <RunInfo.h> | 
| 38 |  | #include <GLTables.h> | 
| 39 |  | // | 
| 40 | // YODA headers | // YODA headers | 
| 41 | // | // | 
| 42 | #include <PamelaRun.h> | #include <PamelaRun.h> | 
|  | #include <RegistryEvent.h> |  | 
| 43 | #include <PscuHeader.h> | #include <PscuHeader.h> | 
| 44 | #include <PscuEvent.h> | #include <PscuEvent.h> | 
| 45 | #include <EventHeader.h> | #include <EventHeader.h> | 
|  | #include <RegistryEvent.h> |  | 
| 46 | #include <CalibS4Event.h> | #include <CalibS4Event.h> | 
| 47 | #include <physics/S4/S4Event.h> | #include <physics/S4/S4Event.h> | 
|  | //#include <yodaUtility.h> |  | 
|  | // |  | 
|  | // RunInfo header |  | 
|  | // |  | 
|  | #include <RunInfo.h> |  | 
|  | #include <GLTables.h> |  | 
| 48 | // | // | 
| 49 | // This program headers | // This program headers | 
| 50 | // | // | 
| 51 | #include <S4Level2.h> | #include <S4Level2.h> | 
| 52 | #include <S4Core.h> | #include <S4Core.h> | 
| 53 | #include <S4Verl2.h> | #include <S4Verl2.h> | 
| 54 |  | // | 
| 55 | using namespace std; | 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 |  | Int_t s4sig = glS4calib->Query_GL_S4_CALIB(atime, dbc); | 
| 76 |  | if ( s4sig != -57 && s4sig < 0 ){ | 
| 77 |  | parametri->AddAt(0.,0); | 
| 78 |  | parametri->AddAt(0.,1); | 
| 79 |  | return parametri; | 
| 80 |  | }; | 
| 81 |  | if ( s4sig < 0 ) throw s4sig; | 
| 82 |  | // | 
| 83 |  | GL_ROOT *glroot = new GL_ROOT(); | 
| 84 |  | //  if ( !dbc->IsConnected() ) throw -504; | 
| 85 |  | glroot->Query_GL_ROOT(glS4calib->ID_ROOT_L0,dbc); | 
| 86 |  | // | 
| 87 |  | stringstream ftmpname; | 
| 88 |  | ftmpname.str(""); | 
| 89 |  | ftmpname << glroot->PATH.Data() << "/"; | 
| 90 |  | ftmpname << glroot->NAME.Data(); | 
| 91 |  | // | 
| 92 |  | TFile *file = 0; | 
| 93 |  | file = new TFile(ftmpname.str().c_str()); | 
| 94 |  | // | 
| 95 |  | if ( !file ) return(NULL); | 
| 96 |  | // | 
| 97 |  | TTree *tr = 0; | 
| 98 |  | tr = (TTree*)file->Get("CalibS4"); | 
| 99 |  | if ( !tr ){ | 
| 100 |  | file->Close(); | 
| 101 |  | return(NULL); | 
| 102 |  | }; | 
| 103 |  | // | 
| 104 |  | pamela::CalibS4Event *S4CalibEvent = 0; | 
| 105 |  | tr->SetBranchAddress("CalibS4", &S4CalibEvent); | 
| 106 |  | if ( tr->GetEntries() < glS4calib->EV_ROOT ) return(NULL); | 
| 107 |  | // | 
| 108 |  | tr->GetEntry(glS4calib->EV_ROOT); | 
| 109 |  | // | 
| 110 |  | // Variables initialization | 
| 111 |  | // | 
| 112 |  | Double_t mip[3]={1, 30, 300}; | 
| 113 |  | Double_t adc[3] = {0.,0.,0.}; | 
| 114 |  | // | 
| 115 |  | // Fit calibrations and find parameters to calibrate data | 
| 116 |  | // | 
| 117 |  | pamela::S4::S4Event  *s4Record = 0; | 
| 118 |  | // | 
| 119 |  | for (Int_t j = 0; j < 4; j++){ | 
| 120 |  | for (Int_t i = 0; i < 128; i++){ | 
| 121 |  | s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i)); | 
| 122 |  | switch (j) { | 
| 123 |  | case 0 :{ | 
| 124 |  | adc[0]=adc[0]+((s4Record->S4_DATA)-32); | 
| 125 |  | break; | 
| 126 |  | }; | 
| 127 |  | case 1 :{ | 
| 128 |  | adc[1]=adc[1]+((s4Record->S4_DATA)-32); | 
| 129 |  | break; | 
| 130 |  | }; | 
| 131 |  | case 3 :{ | 
| 132 |  | adc[2]=adc[2]+((s4Record->S4_DATA)-32); | 
| 133 |  | break; | 
| 134 |  | }; | 
| 135 |  | }; | 
| 136 |  | }; | 
| 137 |  | }; | 
| 138 |  | // | 
| 139 |  | adc[0]=adc[0]/128; | 
| 140 |  | adc[1]=adc[1]/128; | 
| 141 |  | adc[2]=adc[2]/128; | 
| 142 |  | //  if ( IsDebug() ) printf(" adc1 = %g adc2 = %g adc3 = %g\n",adc[0],adc[1],adc[2]); | 
| 143 |  | TGraph *fitpar = new TGraph (3, adc, mip); | 
| 144 |  | TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // function definition with 2 parameters | 
| 145 |  | // | 
| 146 |  | func->SetParameters(0.3,1.); // parameters initialization to 1 | 
| 147 |  | func->SetParNames("m","q"); //parameter's name | 
| 148 |  | fitpar->Fit(func,"qr"); //function fit | 
| 149 |  | parametri->AddAt(func->GetParameter(0),0); | 
| 150 |  | parametri->AddAt(func->GetParameter(1),1); | 
| 151 |  | //  if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.7 || parametri[1] > 1.1 ) valid = 0; | 
| 152 |  | // | 
| 153 |  | delete glroot; | 
| 154 |  | delete glS4calib; | 
| 155 |  | delete fitpar; | 
| 156 |  | delete func; | 
| 157 |  | file->Close(); | 
| 158 |  | // | 
| 159 |  | return parametri; | 
| 160 |  | }; | 
| 161 |  |  | 
| 162 | // | // | 
| 163 | // CORE ROUTINE | // CORE ROUTINE | 
| 164 | // | // | 
| 165 | // | // | 
| 166 |  | int S4Core(UInt_t run, TFile *file, TSQLServer *dbc, Int_t S4argc, char *S4argv[]){ | 
|  | int S4Core(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t S4argc, char *S4argv[]){ |  | 
|  | Int_t i = 0; |  | 
| 167 | // | // | 
| 168 | TString processFolder = "S4Folder"; | // Set these to true to have a verbose output. | 
| 169 | // | // | 
| 170 | // Set these to true to have a very verbose output. | Bool_t debug = false; | 
| 171 | Bool_t debug = false; | Bool_t verbose = false; | 
|  | // |  | 
|  | Bool_t verbose = false; |  | 
| 172 | // | // | 
| 173 |  | // | 
| 174 |  | // Output directory is the working directoy. | 
| 175 |  | // | 
| 176 |  | const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath())); | 
| 177 |  | // | 
| 178 |  | Int_t i = 0; | 
| 179 |  | TString processFolder = Form("S4Folder_%u",run); | 
| 180 | if ( S4argc > 0 ){ | if ( S4argc > 0 ){ | 
| 181 | i = 0; | i = 0; | 
| 182 | while ( i < S4argc ){ | while ( i < S4argc ){ | 
| 189 | }; | }; | 
| 190 | if ( (!strcmp(S4argv[i],"--debug")) || (!strcmp(S4argv[i],"-g"))) { | if ( (!strcmp(S4argv[i],"--debug")) || (!strcmp(S4argv[i],"-g"))) { | 
| 191 | verbose = true; | verbose = true; | 
| 192 |  | debug = true; | 
| 193 | }; | }; | 
| 194 | if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) { | if ( (!strcmp(S4argv[i],"--verbose")) || (!strcmp(S4argv[i],"-v"))) { | 
| 195 | verbose = true; | verbose = true; | 
| 197 | i++; | i++; | 
| 198 | }; | }; | 
| 199 | }; | }; | 
|  |  |  | 
|  | const char* outDir = gSystem->DirName(gSystem->DirName(file->GetPath())); |  | 
| 200 | // Variables for level2 | // Variables for level2 | 
| 201 | // | // | 
| 202 | TTree *S4tr = 0; | TTree *S4tr = 0; | 
| 203 | Long64_t nevents = 0LL; | UInt_t nevents = 0; | 
| 204 |  | // | 
| 205 |  | // Variables needed to reprocess data | 
| 206 | // | // | 
| 207 | // variables needed to reprocess data | Long64_t maxsize = 10000000000LL; | 
| 208 |  | TTree::SetMaxTreeSize(maxsize); | 
| 209 | // | // | 
| 210 | TString S4version; | TString S4version; | 
| 211 | ItoRunInfo *runinfo = 0; | ItoRunInfo *runinfo = 0; | 
| 212 | TArrayL *runlist = 0; | TArrayI *runlist = 0; | 
| 213 | TTree *S4trclone = 0; | TTree *S4trclone = 0; | 
| 214 | Bool_t reproc = false; | Bool_t reproc = false; | 
| 215 | Bool_t reprocall = false; | Bool_t reprocall = false; | 
| 218 | UInt_t numbofrun = 0; | UInt_t numbofrun = 0; | 
| 219 | stringstream ftmpname; | stringstream ftmpname; | 
| 220 | TString fname; | TString fname; | 
| 221 | Long64_t totfileentries = 0ULL; | UInt_t totfileentries = 0; | 
| 222 | Long64_t idRun = 0LL; | UInt_t idRun = 0; | 
| 223 | Double_t  ParamFit0 =  0ULL; | Double_t  ParamFit0 =  0.; | 
| 224 | Double_t  ParamFit1 =  0ULL; | Double_t  ParamFit1 =  0.; | 
| 225 | // | // | 
| 226 | // variables needed to handle error signals | // variables needed to handle error signals | 
| 227 | // | // | 
| 238 | TFile *l0File = 0; | TFile *l0File = 0; | 
| 239 | TTree *l0tr = 0; | TTree *l0tr = 0; | 
| 240 | TBranch *l0head = 0; | TBranch *l0head = 0; | 
|  | TBranch *l0registry = 0; |  | 
| 241 | TBranch *l0S4 =0; | TBranch *l0S4 =0; | 
|  | pamela::RegistryEvent *l0reg=0; |  | 
| 242 | pamela::S4::S4Event  *l0s4e = 0; | pamela::S4::S4Event  *l0s4e = 0; | 
| 243 | pamela::EventHeader *eh = 0; | pamela::EventHeader *eh = 0; | 
| 244 |  | pamela::PscuHeader *ph = 0; | 
| 245 | // | // | 
| 246 | // Define other basic variables | // Define other basic variables | 
| 247 | // | // | 
| 250 | stringstream file3; | stringstream file3; | 
| 251 | stringstream qy; | stringstream qy; | 
| 252 | Int_t totevent = 0; | Int_t totevent = 0; | 
| 253 | ULong64_t atime = 0ULL; | UInt_t atime = 0; | 
| 254 | Int_t ei = 0; | //  Int_t ei = 0; | 
| 255 | Int_t re = 0; | UInt_t re = 0; | 
| 256 | // | // | 
| 257 | // Working filename | // Working filename | 
| 258 | // | // | 
| 267 | TTree *tempS4 = 0; | TTree *tempS4 = 0; | 
| 268 | stringstream tempname; | stringstream tempname; | 
| 269 | stringstream S4folder; | stringstream S4folder; | 
| 270 |  | Bool_t myfold = false; | 
| 271 | tempname.str(""); | tempname.str(""); | 
| 272 | tempname << outDir; | tempname << outDir; | 
| 273 | tempname << "/" << processFolder.Data(); | tempname << "/" << processFolder.Data(); | 
| 274 | S4folder.str(""); | S4folder.str(""); | 
| 275 | S4folder << tempname.str().c_str(); | S4folder << tempname.str().c_str(); | 
|  | gSystem->MakeDirectory(S4folder.str().c_str()); |  | 
| 276 | tempname << "/S4tree_run"; | tempname << "/S4tree_run"; | 
| 277 | tempname << run << ".root"; | tempname << run << ".root"; | 
| 278 | // | // | 
| 279 | // DB classes | // DB classes | 
| 280 | // | // | 
| 281 | GL_ROOT *glroot = new GL_ROOT(); | GL_ROOT *glroot = new GL_ROOT(); | 
| 282 | GL_S4_CALIB *glS4calib = new GL_S4_CALIB(); | GL_TIMESYNC *dbtime = 0; | 
| 283 | // | // | 
| 284 | // Let's start! | // Let's start! | 
| 285 | // | // | 
| 287 | // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file | // if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file | 
| 288 | // if it exists we are reprocessing data and we must delete that entries, if not we must create it. | // if it exists we are reprocessing data and we must delete that entries, if not we must create it. | 
| 289 | // | // | 
| 290 | if ( run == 0ULL )  reproc = true; | if ( run == 0 )  reproc = true; | 
| 291 | // | // | 
| 292 | // | // | 
| 293 | // Output file is "outputfile" | // Output file is "outputfile" | 
| 332 | // number of run to be processed | // number of run to be processed | 
| 333 | // | // | 
| 334 | numbofrun = runinfo->GetNoRun(); | numbofrun = runinfo->GetNoRun(); | 
| 335 |  | UInt_t totnorun = runinfo->GetRunEntries(); | 
| 336 | // | // | 
| 337 | // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run | // Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run | 
| 338 | // | // | 
| 343 | // tree does not exist, we are not reprocessing | // tree does not exist, we are not reprocessing | 
| 344 | // | // | 
| 345 | reproc = false; | reproc = false; | 
| 346 | if ( run == 0ULL ){ | if ( run == 0 ){ | 
| 347 | if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n"); | if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n"); | 
| 348 | } | } | 
| 349 | if ( runinfo->IsReprocessing() && run != 0ULL ) { | if ( runinfo->IsReprocessing() && run != 0 ) { | 
| 350 | if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n"); | if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n"); | 
| 351 | } | } | 
| 352 | } else { | } else { | 
| 353 | // | // | 
| 354 | // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? | // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? | 
| 355 | // | // | 
| 356 |  | S4trclone->SetAutoSave(900000000000000LL); | 
| 357 | reproc = true; | reproc = true; | 
| 358 | // | // | 
| 359 | // update versioning information | // update versioning information | 
| 360 | // | // | 
|  | //sgnl = runinfo->Update(run, "S4",S4version); |  | 
| 361 | if (verbose) printf("\n Preparing the pre-processing...\n"); | if (verbose) printf("\n Preparing the pre-processing...\n"); | 
| 362 | // | // | 
| 363 | if ( run == 0ULL ){ | if ( run == 0 || totnorun == 1 ){ | 
| 364 | // | // | 
| 365 | // we are reprocessing all the file | // we are reprocessing all the file | 
| 366 | // 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 | // 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 | 
| 375 | // | // | 
| 376 | reprocall = false; | reprocall = false; | 
| 377 | // | // | 
| 378 | if (verbose) printf("\n S4 - WARNING: Reprocessing run number %llu \n",run); | if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run); | 
| 379 | // | // | 
| 380 | // copying old tree to a new file | // copying old tree to a new file | 
| 381 | // | // | 
| 382 |  | gSystem->MakeDirectory(S4folder.str().c_str()); | 
| 383 |  | myfold = true; | 
| 384 | tempfile = new TFile(tempname.str().c_str(),"RECREATE"); | tempfile = new TFile(tempname.str().c_str(),"RECREATE"); | 
| 385 | tempS4 = S4trclone->CloneTree(-1,"fast"); | tempS4 = S4trclone->CloneTree(-1,"fast"); | 
| 386 | tempS4->SetName("S4-old"); | tempS4->SetName("S4-old"); | 
| 400 | // | // | 
| 401 | file->cd(); | file->cd(); | 
| 402 | S4tr = new TTree("S4-new","PAMELA Level2 S4 data"); | S4tr = new TTree("S4-new","PAMELA Level2 S4 data"); | 
| 403 |  | S4tr->SetAutoSave(900000000000000LL); | 
| 404 | S4tr->Branch("S4Level2","S4Level2",&s4); | S4tr->Branch("S4Level2","S4Level2",&s4); | 
| 405 | // | // | 
| 406 | if ( reproc && !reprocall ){ | if ( reproc && !reprocall ){ | 
| 409 | // | // | 
| 410 | tempfile = new TFile(tempname.str().c_str(),"READ"); | tempfile = new TFile(tempname.str().c_str(),"READ"); | 
| 411 | S4trclone = (TTree*)tempfile->Get("S4-old"); | S4trclone = (TTree*)tempfile->Get("S4-old"); | 
| 412 |  | S4trclone->SetAutoSave(900000000000000LL); | 
| 413 | S4trclone->SetBranchAddress("S4Level2",&s4clone); | S4trclone->SetBranchAddress("S4Level2",&s4clone); | 
| 414 | // | // | 
| 415 | if ( nobefrun > 0 ){ | if ( nobefrun > 0 ){ | 
| 416 | if (verbose){ | if (verbose){ | 
| 417 | printf("\n Pre-processing: copying events from the old tree before the processed run\n"); | printf("\n Pre-processing: copying events from the old tree before the processed run\n"); | 
| 418 | printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run); | printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run); | 
| 419 | printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); | printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); | 
| 420 | } | } | 
| 421 | for (UInt_t j = 0; j < nobefrun; j++){ | for (UInt_t j = 0; j < nobefrun; j++){ | 
| 424 | // | // | 
| 425 | // copy s4clone to mydec | // copy s4clone to mydec | 
| 426 | // | // | 
| 427 | s4 = new S4Level2(); | s4->Clear(); | 
| 428 |  | // | 
| 429 | memcpy(&s4,&s4clone,sizeof(s4clone)); | memcpy(&s4,&s4clone,sizeof(s4clone)); | 
| 430 | // | // | 
| 431 | // Fill entry in the new tree | // Fill entry in the new tree | 
| 450 | idRun = runlist->At(irun); | idRun = runlist->At(irun); | 
| 451 | if (verbose){ | if (verbose){ | 
| 452 | printf("\n\n\n ####################################################################### \n"); | printf("\n\n\n ####################################################################### \n"); | 
| 453 | printf("                    PROCESSING RUN NUMBER %i \n",(int)idRun); | printf("                    PROCESSING RUN NUMBER %u \n",idRun); | 
| 454 | printf(" ####################################################################### \n\n\n"); | printf(" ####################################################################### \n\n\n"); | 
| 455 | } | } | 
| 456 | // | // | 
| 457 | runinfo->ID_REG_RUN = 0ULL; | runinfo->ID_ROOT_L0 = 0; | 
| 458 | // | // | 
| 459 | // store in the runinfo class the GL_RUN variables for our run | // store in the runinfo class the GL_RUN variables for our run | 
| 460 | // | // | 
| 461 | sgnl = 0; | sgnl = 0; | 
| 462 | sgnl = runinfo->GetRunInfo(idRun); | sgnl = runinfo->GetRunInfo(idRun); | 
| 463 | if ( sgnl ){ | if ( sgnl ){ | 
| 464 | //printf(" S4 - ERROR: RunInfo exited with non-zero status\n"); | if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n"); | 
| 465 | code = sgnl; | code = sgnl; | 
| 466 | goto closeandexit; | goto closeandexit; | 
| 467 | } else { | } else { | 
| 468 | sgnl = 0; | sgnl = 0; | 
| 469 | }; | }; | 
| 470 | // | // | 
| 471 | // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN | // now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0 | 
| 472 | // | // | 
| 473 | if ( runinfo->ID_REG_RUN == 0 ){ | if ( runinfo->ID_ROOT_L0 == 0 ){ | 
| 474 | //printf("\n S4 - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun); | if ( debug ) printf("\n S4 - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun); | 
| 475 | code = -5; | code = -5; | 
| 476 | goto closeandexit; | goto closeandexit; | 
| 477 | }; | }; | 
| 478 | // | // | 
| 479 |  | // prepare the timesync for the db | 
| 480 |  | // | 
| 481 |  | //    if ( !dbc->IsConnected() ) throw -504; | 
| 482 |  | dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc); | 
| 483 |  | // | 
| 484 | // Search in the DB the path and name of the LEVEL0 file to be processed. | // Search in the DB the path and name of the LEVEL0 file to be processed. | 
| 485 | // | // | 
| 486 | glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc); | //    if ( !dbc->IsConnected() ) throw -504; | 
| 487 |  | glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc); | 
| 488 | // | // | 
| 489 | ftmpname.str(""); | ftmpname.str(""); | 
| 490 | ftmpname << glroot->PATH.Data() << "/"; | ftmpname << glroot->PATH.Data() << "/"; | 
| 493 | // | // | 
| 494 | // print out informations | // print out informations | 
| 495 | // | // | 
| 496 | totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1; | totevent = runinfo->NEVENTS; | 
| 497 | if (verbose){ | if (verbose){ | 
| 498 | printf("\n LEVEL0 data file: %s \n",fname.Data()); | printf("\n LEVEL0 data file: %s \n",fname.Data()); | 
| 499 | printf(" RUN HEADER absolute time is:  %llu \n",runinfo->RUNHEADER_TIME); | printf(" RUN HEADER absolute time is:  %u \n",runinfo->RUNHEADER_TIME); | 
| 500 | printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME); | printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME); | 
| 501 | printf(" %i events to be processed for run %llu: from %i to %i (reg entries)\n\n",totevent,idRun,runinfo->EV_REG_PHYS_FROM,runinfo->EV_REG_PHYS_TO); | printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent)); | 
| 502 | } | } | 
| 503 | // | // | 
| 504 | // Open Level0 file | // Open Level0 file | 
| 505 | // | // | 
| 506 | l0File = new TFile(fname.Data()); | l0File = new TFile(fname.Data()); | 
| 507 | if ( !l0File ) { | if ( !l0File ) { | 
| 508 | //printf(" S4 - ERROR: problems opening  Level0 file\n"); | if ( debug ) printf(" S4 - ERROR: problems opening  Level0 file\n"); | 
| 509 | code = -6; | code = -6; | 
| 510 | goto closeandexit; | goto closeandexit; | 
| 511 | }; | }; | 
| 512 |  | // | 
| 513 | l0tr = (TTree*)l0File->Get("Physics"); | l0tr = (TTree*)l0File->Get("Physics"); | 
| 514 | if ( !l0tr ) { | if ( !l0tr ) { | 
| 515 | //printf(" S4 - ERROR: no Physics tree in Level0 file\n"); | if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n"); | 
| 516 | l0File->Close(); | l0File->Close(); | 
| 517 | code = -7; | code = -7; | 
| 518 | goto closeandexit; | goto closeandexit; | 
| 519 | }; | }; | 
| 520 | l0head = l0tr->GetBranch("Header"); | l0head = l0tr->GetBranch("Header"); | 
| 521 | if ( !l0head ) { | if ( !l0head ) { | 
| 522 | //printf(" S4 - ERROR: no Header branch in Level0 tree\n"); | if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n"); | 
| 523 | l0File->Close(); | l0File->Close(); | 
| 524 | code = -8; | code = -8; | 
| 525 | goto closeandexit; | goto closeandexit; | 
| 526 | }; | }; | 
|  | l0registry = l0tr->GetBranch("Registry"); |  | 
|  | if ( !l0registry ) { |  | 
|  | //printf(" S4 - ERROR: no Registry branch in Level0 tree\n"); |  | 
|  | l0File->Close(); |  | 
|  | code = -9; |  | 
|  | goto closeandexit; |  | 
|  | }; |  | 
| 527 | l0S4 = l0tr->GetBranch("S4"); | l0S4 = l0tr->GetBranch("S4"); | 
| 528 | if ( !l0S4 ) { | if ( !l0S4 ) { | 
| 529 | //printf(" S4 - ERROR: no S4 branch in Level0 tree\n"); | if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n"); | 
| 530 | l0File->Close(); | l0File->Close(); | 
| 531 | code = -503; | code = -503; | 
| 532 | goto closeandexit; | goto closeandexit; | 
| 533 | }; | }; | 
|  |  |  | 
| 534 | // | // | 
|  | l0tr->SetBranchAddress("Registry", &l0reg); |  | 
| 535 | l0tr->SetBranchAddress("S4", &l0s4e); | l0tr->SetBranchAddress("S4", &l0s4e); | 
| 536 | l0tr->SetBranchAddress("Header", &eh); | l0tr->SetBranchAddress("Header", &eh); | 
| 537 | // | // | 
| 538 | nevents = l0registry->GetEntries(); | nevents = l0S4->GetEntries(); | 
| 539 | // | // | 
| 540 | if ( nevents < 1 ) { | if ( nevents < 1 ) { | 
| 541 | //printf(" S4 - ERROR: Level0 file is empty\n\n"); | if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n"); | 
| 542 | l0File->Close(); | l0File->Close(); | 
| 543 | code = -11; | code = -11; | 
| 544 | goto closeandexit; | goto closeandexit; | 
| 545 | }; | }; | 
| 546 | // | // | 
| 547 | if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) { | if ( runinfo->EV_TO > nevents-1 ) { | 
| 548 | //printf(" S4 - ERROR: too few entries in the registry tree\n"); | if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n"); | 
| 549 | l0File->Close(); | l0File->Close(); | 
| 550 | code = -12; | code = -12; | 
| 551 | goto closeandexit; | goto closeandexit; | 
| 554 | // Check if we have to load parameter files (or calibration associated to runs and not to events) | // Check if we have to load parameter files (or calibration associated to runs and not to events) | 
| 555 | // second query: which is the value of paramfit relative to the calibration? | // second query: which is the value of paramfit relative to the calibration? | 
| 556 | // | // | 
| 557 | glS4calib->Query_GL_S4_CALIB(runinfo->RUNHEADER_TIME, dbc); | TArrayD *params = 0; | 
| 558 | ParamFit0 = glS4calib->PARAM_FIT0; | params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc); | 
| 559 | ParamFit1 = glS4calib->PARAM_FIT1; | if ( !params ){ | 
| 560 |  | code = -13; | 
| 561 |  | goto closeandexit; | 
| 562 |  | }; | 
| 563 |  | // | 
| 564 |  | ParamFit0 = params->At(0); | 
| 565 |  | ParamFit1 = params->At(1); | 
| 566 | // | // | 
| 567 | // run over all the events of the run | // run over all the events of the run | 
| 568 | // | // | 
| 569 | if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n"); | if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n"); | 
| 570 | // | // | 
| 571 | for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){ | if ( dbc ){ | 
| 572 |  | dbc->Close(); | 
| 573 |  | //      delete dbc; | 
| 574 |  | }; | 
| 575 |  | // | 
| 576 |  | for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){ | 
| 577 | // | // | 
| 578 | if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000); | if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000); | 
| 579 | // | // | 
| 580 | l0registry->GetEntry(re); | l0head->GetEntry(re); | 
| 581 | // | // | 
| 582 | // absolute time of this event | // absolute time of this event | 
| 583 | // | // | 
| 584 | atime = l0reg->absTime; | ph = eh->GetPscuHeader(); | 
| 585 | // | atime = dbtime->DBabsTime(ph->GetOrbitalTime()); | 
|  | // physics events is at entry number ei where |  | 
|  | // |  | 
|  | ei = l0reg->event; |  | 
| 586 | // | // | 
| 587 | // paranoid check | // paranoid check | 
| 588 | // | // | 
| 595 | // | // | 
| 596 | // start processing | // start processing | 
| 597 | // | // | 
| 598 | s4 = new S4Level2(); | s4->Clear(); | 
| 599 | l0S4->GetEntry(ei); | l0S4->GetEntry(re); | 
| 600 | if (l0s4e->unpackError == 1)  continue; | if (l0s4e->unpackError == 0){ | 
| 601 | s4->S4adc = l0s4e->S4_DATA; | s4->S4adc = l0s4e->S4_DATA; | 
| 602 | if ((l0s4e->S4_DATA) > 31 ){ | // | 
| 603 | s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1; | if ((l0s4e->S4_DATA) > 31 ){ | 
| 604 | }else{ | s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1; | 
| 605 | s4->S4calibrated = 0; | }else{ | 
| 606 | } | s4->S4calibrated = 0; | 
| 607 |  | } | 
| 608 |  | }; | 
| 609 |  | // | 
| 610 |  | s4->unpackError = l0s4e->unpackError; | 
| 611 |  | // | 
| 612 | S4tr->Fill(); | S4tr->Fill(); | 
| 613 | // | // | 
| 614 | // | // | 
| 619 | // | // | 
| 620 | // Here you may want to clear some variables before processing another run | // Here you may want to clear some variables before processing another run | 
| 621 | // | // | 
| 622 | ei = 0; | delete dbtime; | 
| 623 |  | if ( params ) delete params; | 
| 624 |  | // | 
| 625 | }; // process all the runs | }; // process all the runs | 
| 626 | // | // | 
| 627 | if (verbose) printf("\n Finished processing data \n"); | if (verbose) printf("\n Finished processing data \n"); | 
| 645 | // | // | 
| 646 | // copy s4clone to s4 | // copy s4clone to s4 | 
| 647 | // | // | 
| 648 | s4 = new S4Level2(); | //      s4 = new S4Level2(); | 
| 649 |  | s4->Clear(); | 
| 650 | memcpy(&s4,&s4clone,sizeof(s4clone)); | memcpy(&s4,&s4clone,sizeof(s4clone)); | 
| 651 | // | // | 
| 652 | // Fill entry in the new tree | // Fill entry in the new tree | 
| 661 | // | // | 
| 662 | if ( l0File ) l0File->Close(); | if ( l0File ) l0File->Close(); | 
| 663 | if ( tempfile ) tempfile->Close(); | if ( tempfile ) tempfile->Close(); | 
| 664 | gSystem->Unlink(tempname.str().c_str()); | if ( myfold) gSystem->Unlink(tempname.str().c_str()); | 
| 665 |  | // | 
|  | //if ( code < 0 ) printf("\n S4 - ERROR: an error occurred, try to save anyway...\n"); |  | 
|  | //printf("\n Writing and closing rootple\n"); |  | 
| 666 | if ( runinfo ) runinfo->Close(); | if ( runinfo ) runinfo->Close(); | 
| 667 | if ( S4tr ) S4tr->SetName("S4"); | if ( S4tr ) S4tr->SetName("S4"); | 
| 668 | if ( file ){ | if ( file ){ | 
| 670 | file->Write(); | file->Write(); | 
| 671 | }; | }; | 
| 672 | // | // | 
| 673 | gSystem->Unlink(S4folder.str().c_str()); | if ( myfold ) gSystem->Unlink(S4folder.str().c_str()); | 
| 674 | // | // | 
| 675 | // the end | // the end | 
| 676 | // | // | 
| 677 | if (verbose) printf("\n Exiting...\n"); | if (verbose) printf("\n Exiting...\n"); | 
| 678 | if(S4tr)S4tr->Delete(); | if ( S4tr ) S4tr->Delete(); | 
| 679 |  | // | 
| 680 |  | if ( s4 ) delete s4; | 
| 681 |  | if ( s4clone ) delete s4clone; | 
| 682 |  | if ( glroot ) delete glroot; | 
| 683 |  | if ( runinfo ) delete runinfo; | 
| 684 |  | // | 
| 685 | if(code < 0)  throw code; | if(code < 0)  throw code; | 
| 686 | return(code); | return(code); | 
| 687 | } | } | 
| 688 |  |  | 
| 689 |  |  | 
|  |  |  |