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