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