| 1 | 
// | 
| 2 | 
// C/C++ headers | 
| 3 | 
// | 
| 4 | 
#include <fstream> | 
| 5 | 
#include <string.h> | 
| 6 | 
// | 
| 7 | 
// ROOT headers | 
| 8 | 
// | 
| 9 | 
#include <TTree.h> | 
| 10 | 
#include <TClassEdit.h> | 
| 11 | 
#include <TObject.h> | 
| 12 | 
#include <TList.h> | 
| 13 | 
#include <TArrayL.h> | 
| 14 | 
#include <TSystem.h> | 
| 15 | 
#include <TSystemDirectory.h> | 
| 16 | 
#include <TString.h> | 
| 17 | 
#include <TFile.h> | 
| 18 | 
#include <TClass.h> | 
| 19 | 
#include <TCanvas.h> | 
| 20 | 
#include <TH1.h> | 
| 21 | 
#include <TH1F.h> | 
| 22 | 
#include <TH2D.h> | 
| 23 | 
#include <TLatex.h> | 
| 24 | 
#include <TPad.h> | 
| 25 | 
#include <TSQLServer.h> | 
| 26 | 
#include <TSQLRow.h> | 
| 27 | 
#include <TSQLResult.h> | 
| 28 | 
#include <TClonesArray.h> | 
| 29 | 
// | 
| 30 | 
// YODA headers | 
| 31 | 
// | 
| 32 | 
#include <PamelaRun.h> | 
| 33 | 
#include <RegistryEvent.h> | 
| 34 | 
#include <physics/trigger/TriggerEvent.h> | 
| 35 | 
#include <physics/tof/TofEvent.h> | 
| 36 | 
// | 
| 37 | 
// RunInfo header | 
| 38 | 
// | 
| 39 | 
#include <RunInfo.h> | 
| 40 | 
// | 
| 41 | 
// This program headers | 
| 42 | 
// | 
| 43 | 
#include <ToFCore.h> | 
| 44 | 
#include <ToFLevel2.h> | 
| 45 | 
#include <ToFVerl2.h> | 
| 46 | 
// | 
| 47 | 
// | 
| 48 | 
// Declaration of the core fortran routines | 
| 49 | 
// | 
| 50 | 
#define tofl2com tofl2com_ | 
| 51 | 
extern "C" int tofl2com(); | 
| 52 | 
#define toftrk toftrk_ | 
| 53 | 
extern "C" int toftrk(); | 
| 54 | 
#define rdtofcal rdtofcal_ | 
| 55 | 
extern "C" int rdtofcal(char []); | 
| 56 | 
 | 
| 57 | 
// | 
| 58 | 
// Tracker classes headers and definitions | 
| 59 | 
// | 
| 60 | 
#include <TrkLevel2.h> | 
| 61 | 
// | 
| 62 | 
using namespace std; | 
| 63 | 
// | 
| 64 | 
// | 
| 65 | 
// CORE ROUTINE | 
| 66 | 
// | 
| 67 | 
// | 
| 68 | 
int ToFCore(ULong64_t run, TFile *file, TSQLServer *dbc, Int_t ToFargc, char *ToFargv[]){ | 
| 69 | 
  // | 
| 70 | 
  // | 
| 71 | 
  // Set these to true to have a very verbose output. | 
| 72 | 
  // | 
| 73 | 
  Bool_t verbose = false; | 
| 74 | 
  Bool_t debug = false; | 
| 75 | 
  // | 
| 76 | 
  TString processFolder = "ToFFolder"; | 
| 77 | 
  if ( ToFargc > 0 ){ | 
| 78 | 
    Int_t i = 0; | 
| 79 | 
    while ( i < ToFargc ){ | 
| 80 | 
      if ( !strcmp(ToFargv[i],"-processFolder") ) { | 
| 81 | 
        if ( ToFargc < i+1 ){ | 
| 82 | 
          throw -3; | 
| 83 | 
        }; | 
| 84 | 
        processFolder = (TString)ToFargv[i+1]; | 
| 85 | 
        i++; | 
| 86 | 
      }; | 
| 87 | 
      if ( !strcmp(ToFargv[i],"-v") ||  !strcmp(ToFargv[i],"--verbose") ) { | 
| 88 | 
        verbose = true; | 
| 89 | 
      }; | 
| 90 | 
      if ( !strcmp(ToFargv[i],"-g") ||  !strcmp(ToFargv[i],"--debug") ) { | 
| 91 | 
        debug = true; | 
| 92 | 
      }; | 
| 93 | 
      i++; | 
| 94 | 
    }; | 
| 95 | 
  }; | 
| 96 | 
  // | 
| 97 | 
  // | 
| 98 | 
  // Output directory is the working directoy. | 
| 99 | 
  // | 
| 100 | 
  const char* outdir = gSystem->DirName(gSystem->DirName(file->GetPath())); | 
| 101 | 
  // | 
| 102 | 
  // Variables for level2  | 
| 103 | 
  // | 
| 104 | 
  TTree *tracker = 0; | 
| 105 | 
  TTree *toft = 0; | 
| 106 | 
  Long64_t nevents = 0LL; | 
| 107 | 
  // | 
| 108 | 
  // variables needed to reprocess data | 
| 109 | 
  // | 
| 110 | 
  TString tofversion; | 
| 111 | 
  ItoRunInfo *runinfo = 0; | 
| 112 | 
  TArrayL *runlist = 0; | 
| 113 | 
  TTree *toftclone = 0; | 
| 114 | 
  Bool_t reproc = false; | 
| 115 | 
  Bool_t reprocall = false; | 
| 116 | 
  UInt_t nobefrun = 0; | 
| 117 | 
  UInt_t noaftrun = 0; | 
| 118 | 
  UInt_t numbofrun = 0; | 
| 119 | 
  stringstream ftmpname; | 
| 120 | 
  TString fname; | 
| 121 | 
  Long64_t totfileentries = 0ULL; | 
| 122 | 
  Long64_t idRun = 0LL; | 
| 123 | 
  // | 
| 124 | 
  // variables needed to handle error signals | 
| 125 | 
  // | 
| 126 | 
  Int_t code = 0; | 
| 127 | 
  Int_t sgnl; | 
| 128 | 
  // | 
| 129 | 
  // tof level2 classes | 
| 130 | 
  // | 
| 131 | 
  ToFLevel2 *tof = new ToFLevel2(); | 
| 132 | 
  ToFLevel2 *tofclone = new ToFLevel2(); | 
| 133 | 
  // | 
| 134 | 
  // tracker level2 variables | 
| 135 | 
  // | 
| 136 | 
  TrkLevel2 *trk = new TrkLevel2(); | 
| 137 | 
  Long64_t nevtrkl2 = 0; | 
| 138 | 
  // | 
| 139 | 
  // define variables for opening and reading level0 file | 
| 140 | 
  // | 
| 141 | 
  TFile *l0File = 0; | 
| 142 | 
  TTree *l0tr = 0; | 
| 143 | 
  TBranch *l0head = 0; | 
| 144 | 
  TBranch *l0registry = 0; | 
| 145 | 
  TBranch *l0trig = 0; | 
| 146 | 
  TBranch *l0tof = 0; | 
| 147 | 
  pamela::RegistryEvent *l0reg=0; | 
| 148 | 
  pamela::trigger::TriggerEvent *trig = 0; | 
| 149 | 
  pamela::tof::TofEvent *tofEvent = 0; | 
| 150 | 
  // | 
| 151 | 
  // Define other basic variables | 
| 152 | 
  //  | 
| 153 | 
  UInt_t procev = 0; | 
| 154 | 
  stringstream file2; | 
| 155 | 
  stringstream file3; | 
| 156 | 
  stringstream qy; | 
| 157 | 
  Int_t itr = -1;     | 
| 158 | 
  Int_t totevent = 0; | 
| 159 | 
  ULong64_t atime = 0ULL; | 
| 160 | 
  Int_t ei = 0;  | 
| 161 | 
  Int_t re = 0; | 
| 162 | 
  // | 
| 163 | 
  // Working filename | 
| 164 | 
  // | 
| 165 | 
  TString outputfile; | 
| 166 | 
  stringstream name; | 
| 167 | 
  name.str(""); | 
| 168 | 
  name << outdir << "/"; | 
| 169 | 
  // | 
| 170 | 
  // temporary file and folder | 
| 171 | 
  // | 
| 172 | 
  TFile *tempfile = 0; | 
| 173 | 
  TTree *temptof = 0; | 
| 174 | 
  stringstream tempname; | 
| 175 | 
  stringstream toffolder; | 
| 176 | 
  tempname.str(""); | 
| 177 | 
  tempname << outdir; | 
| 178 | 
  tempname << "/" << processFolder.Data(); | 
| 179 | 
  toffolder.str(""); | 
| 180 | 
  toffolder << tempname.str().c_str(); | 
| 181 | 
  gSystem->MakeDirectory(toffolder.str().c_str()); | 
| 182 | 
  tempname << "/toftree_run"; | 
| 183 | 
  tempname << run << ".root";   | 
| 184 | 
  // | 
| 185 | 
  // variables needed to load magnetic field maps | 
| 186 | 
  //  | 
| 187 | 
  Int_t ntrkentry = 0; | 
| 188 | 
  ULong64_t tttrkpar1 = 0ULL; | 
| 189 | 
  Bool_t trkpar1 = true; | 
| 190 | 
  ULong64_t tttofpar1 = 0ULL; | 
| 191 | 
  Bool_t tofpar1 = true; | 
| 192 | 
  // | 
| 193 | 
  // DB classes | 
| 194 | 
  // | 
| 195 | 
  GL_ROOT *glroot = new GL_ROOT(); | 
| 196 | 
  GL_PARAM *glparam = new GL_PARAM(); | 
| 197 | 
  // | 
| 198 | 
  // declaring external output and input structures | 
| 199 | 
  // | 
| 200 | 
  extern struct ToFInput  tofinput_; | 
| 201 | 
  extern struct ToFOutput tofoutput_; | 
| 202 | 
  // | 
| 203 | 
  // Let's start! | 
| 204 | 
  // | 
| 205 | 
  // | 
| 206 | 
  // 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 | 
| 207 | 
  // if run != 0 we must process only that run but first we have to check if the tree ToF already exist in the file | 
| 208 | 
  // if it exists we are reprocessing data and we must delete that entries, if not we must create it. | 
| 209 | 
  //  | 
| 210 | 
  if ( run == 0ULL )  reproc = true; | 
| 211 | 
  // | 
| 212 | 
  // | 
| 213 | 
  // Output file is "outputfile" | 
| 214 | 
  // | 
| 215 | 
  if ( !file->IsOpen() ){ | 
| 216 | 
    if ( verbose ) printf(" ToF - ERROR: cannot open file for writing\n"); | 
| 217 | 
    throw -301;     | 
| 218 | 
  }; | 
| 219 | 
  // | 
| 220 | 
  // Does it contain the Tracker tree? | 
| 221 | 
  // | 
| 222 | 
  tracker = (TTree*)file->Get("Tracker"); | 
| 223 | 
  if ( !tracker ) { | 
| 224 | 
    if ( verbose ) printf(" TOF - ERROR: no tracker tree\n"); | 
| 225 | 
    code = -302; | 
| 226 | 
    goto closeandexit; | 
| 227 | 
  }; | 
| 228 | 
  // | 
| 229 | 
  // get tracker level2 data pointer | 
| 230 | 
  // | 
| 231 | 
  tracker->SetBranchAddress("TrkLevel2",&trk);   | 
| 232 | 
  nevtrkl2 = tracker->GetEntries(); | 
| 233 | 
  // | 
| 234 | 
  // Retrieve GL_RUN variables from the level2 file | 
| 235 | 
  //   | 
| 236 | 
  tofversion = ToFInfo(false); // we should decide how to handle versioning system | 
| 237 | 
  // | 
| 238 | 
  // create an interface to RunInfo called "runinfo" | 
| 239 | 
  // | 
| 240 | 
  // ItoRunInfo= interface with RunInfo and GL_RUN | 
| 241 | 
  runinfo = new ItoRunInfo(file); | 
| 242 | 
  // | 
| 243 | 
  // open "Run" tree in level2 file, if not existing return an error (sngl != 0) | 
| 244 | 
  // | 
| 245 | 
  sgnl = 0; | 
| 246 | 
  sgnl = runinfo->Update(run, "TOF",tofversion); | 
| 247 | 
  if ( sgnl ){ | 
| 248 | 
    if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n"); | 
| 249 | 
    code = sgnl; | 
| 250 | 
    goto closeandexit; | 
| 251 | 
  } else { | 
| 252 | 
    sgnl = 0; | 
| 253 | 
  }; | 
| 254 | 
  // | 
| 255 | 
  // number of events in the file BEFORE the first event of our run | 
| 256 | 
  // | 
| 257 | 
  nobefrun = runinfo->GetFirstEntry(); | 
| 258 | 
  // | 
| 259 | 
  // total number of events in the file  | 
| 260 | 
  // | 
| 261 | 
  totfileentries = runinfo->GetFileEntries(); | 
| 262 | 
  // | 
| 263 | 
  // first file entry AFTER the last event of our run | 
| 264 | 
  // | 
| 265 | 
  noaftrun = runinfo->GetLastEntry() + 1; | 
| 266 | 
  // | 
| 267 | 
  // number of run to be processed | 
| 268 | 
  // | 
| 269 | 
  numbofrun = runinfo->GetNoRun(); | 
| 270 | 
  // | 
| 271 | 
  // Try to access the ToF tree in the file, if it exists we are reprocessing data if not we are processing a new run | 
| 272 | 
  // | 
| 273 | 
  toftclone = (TTree*)file->Get("ToF"); | 
| 274 | 
  // | 
| 275 | 
  if ( !toftclone ){ | 
| 276 | 
    // | 
| 277 | 
    // tree does not exist, we are not reprocessing | 
| 278 | 
    // | 
| 279 | 
    reproc = false; | 
| 280 | 
    if ( run == 0ULL && verbose ) printf(" ToF - WARNING: you are reprocessing data but ToF tree does not exist!\n"); | 
| 281 | 
    if ( runinfo->IsReprocessing() && run != 0ULL && verbose ) printf(" ToF - WARNING: it seems you are not reprocessing data but ToF\n versioning information already exists in RunInfo.\n"); | 
| 282 | 
 | 
| 283 | 
  } else { | 
| 284 | 
    // | 
| 285 | 
    // tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? | 
| 286 | 
    // | 
| 287 | 
    reproc = true; | 
| 288 | 
    // | 
| 289 | 
    // update versioning information | 
| 290 | 
    // | 
| 291 | 
    if ( verbose ) printf("\n Preparing the pre-processing...\n"); | 
| 292 | 
    // | 
| 293 | 
    if ( run == 0ULL ){ | 
| 294 | 
      // | 
| 295 | 
      // we are reprocessing all the file | 
| 296 | 
      // 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 | 
| 297 | 
      // | 
| 298 | 
      reprocall = true; | 
| 299 | 
      // | 
| 300 | 
      if ( verbose ) printf("\n ToF - WARNING: Reprocessing all runs\n"); | 
| 301 | 
      // | 
| 302 | 
    } else { | 
| 303 | 
      // | 
| 304 | 
      // 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 | 
| 305 | 
      // | 
| 306 | 
      reprocall = false; | 
| 307 | 
      // | 
| 308 | 
      if ( verbose ) printf("\n ToF - WARNING: Reprocessing run number %llu \n",run); | 
| 309 | 
      // | 
| 310 | 
      // copying old tree to a new file | 
| 311 | 
      // | 
| 312 | 
      tempfile = new TFile(tempname.str().c_str(),"RECREATE"); | 
| 313 | 
      temptof = toftclone->CloneTree(-1,"fast"); | 
| 314 | 
      temptof->SetName("ToF-old"); | 
| 315 | 
      tempfile->Write(); | 
| 316 | 
      tempfile->Close();   | 
| 317 | 
    } | 
| 318 | 
    // | 
| 319 | 
    // Delete the old tree from old file and memory | 
| 320 | 
    // | 
| 321 | 
    toftclone->Delete("all"); | 
| 322 | 
    // | 
| 323 | 
    if ( verbose ) printf(" ...done!\n"); | 
| 324 | 
    // | 
| 325 | 
  }; | 
| 326 | 
  // | 
| 327 | 
  // create ToF detector tree toft | 
| 328 | 
  //  | 
| 329 | 
  file->cd(); | 
| 330 | 
  toft = new TTree("ToF-new","PAMELA Level2 ToF data"); | 
| 331 | 
  toft->Branch("ToFLevel2","ToFLevel2",&tof); | 
| 332 | 
  // | 
| 333 | 
  if ( reproc && !reprocall ){ | 
| 334 | 
    // | 
| 335 | 
    //  open new file and retrieve all tree informations | 
| 336 | 
    // | 
| 337 | 
    tempfile = new TFile(tempname.str().c_str(),"READ"); | 
| 338 | 
    toftclone = (TTree*)tempfile->Get("ToF-old"); | 
| 339 | 
    toftclone->SetBranchAddress("ToFLevel2",&tofclone); | 
| 340 | 
    //       | 
| 341 | 
    if ( nobefrun > 0 ){ | 
| 342 | 
      if ( verbose ) printf("\n Pre-processing: copying events from the old tree before the processed run\n");    | 
| 343 | 
      if ( verbose ) printf(" Copying %u events in the file which are before the beginning of the run %llu \n",nobefrun,run); | 
| 344 | 
      if ( verbose ) printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); | 
| 345 | 
      for (UInt_t j = 0; j < nobefrun; j++){ | 
| 346 | 
        // | 
| 347 | 
        toftclone->GetEntry(j);    | 
| 348 | 
        // | 
| 349 | 
        // copy tofclone to tof | 
| 350 | 
        // | 
| 351 | 
        tof = new ToFLevel2(); | 
| 352 | 
        memcpy(&tof,&tofclone,sizeof(tofclone)); | 
| 353 | 
        // | 
| 354 | 
        // Fill entry in the new tree | 
| 355 | 
        // | 
| 356 | 
        toft->Fill(); | 
| 357 | 
        // | 
| 358 | 
      }; | 
| 359 | 
      if ( verbose ) printf(" Finished successful copying!\n"); | 
| 360 | 
    };           | 
| 361 | 
  }; | 
| 362 | 
  // | 
| 363 | 
  // Get the list of run to be processed, if only one run has to be processed the list will contain one entry only. | 
| 364 | 
  // | 
| 365 | 
  runlist = runinfo->GetRunList(); | 
| 366 | 
  // | 
| 367 | 
  // Loop over the run to be processed | 
| 368 | 
  // | 
| 369 | 
  for (UInt_t irun=0; irun < numbofrun; irun++){ | 
| 370 | 
    // | 
| 371 | 
    // retrieve the first run ID to be processed using the RunInfo list | 
| 372 | 
    // | 
| 373 | 
    idRun = runlist->At(irun); | 
| 374 | 
    if ( verbose ) printf("\n\n\n ####################################################################### \n"); | 
| 375 | 
    if ( verbose ) printf("                    PROCESSING RUN NUMBER %i \n",(int)idRun); | 
| 376 | 
    if ( verbose ) printf(" ####################################################################### \n\n\n"); | 
| 377 | 
    // | 
| 378 | 
    runinfo->ID_REG_RUN = 0ULL; | 
| 379 | 
    // | 
| 380 | 
    // store in the runinfo class the GL_RUN variables for our run | 
| 381 | 
    // | 
| 382 | 
    sgnl = 0; | 
| 383 | 
    sgnl = runinfo->GetRunInfo(idRun); | 
| 384 | 
    if ( sgnl ){ | 
| 385 | 
      if ( verbose ) printf(" TOF - ERROR: RunInfo exited with non-zero status\n"); | 
| 386 | 
      code = sgnl; | 
| 387 | 
      goto closeandexit; | 
| 388 | 
    } else { | 
| 389 | 
      sgnl = 0; | 
| 390 | 
    }; | 
| 391 | 
    // | 
| 392 | 
    // now you can access that variables using the RunInfo class this way runinfo->ID_REG_RUN | 
| 393 | 
    // | 
| 394 | 
    if ( runinfo->ID_REG_RUN == 0 ){ | 
| 395 | 
      if ( verbose ) printf("\n TOF - ERROR: no run with ID_RUN = %i \n\n Exiting... \n\n",(int)idRun); | 
| 396 | 
      code = -5; | 
| 397 | 
      goto closeandexit;     | 
| 398 | 
    }; | 
| 399 | 
    // | 
| 400 | 
    // Search in the DB the path and name of the LEVEL0 file to be processed. | 
| 401 | 
    // | 
| 402 | 
    glroot->Query_GL_ROOT(runinfo->ID_REG_RUN,dbc); | 
| 403 | 
    // | 
| 404 | 
    ftmpname.str(""); | 
| 405 | 
    ftmpname << glroot->PATH.Data() << "/"; | 
| 406 | 
    ftmpname << glroot->NAME.Data(); | 
| 407 | 
    fname = ftmpname.str().c_str(); | 
| 408 | 
    // | 
| 409 | 
    // print out informations | 
| 410 | 
    // | 
| 411 | 
    totevent = runinfo->EV_REG_PHYS_TO - runinfo->EV_REG_PHYS_FROM + 1; | 
| 412 | 
    if ( verbose ) printf("\n LEVEL0 data file: %s \n",fname.Data()); | 
| 413 | 
    if ( verbose ) printf(" RUN HEADER absolute time is:  %llu \n",runinfo->RUNHEADER_TIME); | 
| 414 | 
    if ( verbose ) printf(" RUN TRAILER absolute time is: %llu \n",runinfo->RUNTRAILER_TIME); | 
| 415 | 
    if ( verbose ) 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); | 
| 416 | 
    // | 
| 417 | 
    // Open Level0 file | 
| 418 | 
    // | 
| 419 | 
    l0File = new TFile(fname.Data()); | 
| 420 | 
    if ( !l0File ) { | 
| 421 | 
      if ( verbose ) printf(" TOF - ERROR: problems opening  Level0 file\n"); | 
| 422 | 
      code = -6; | 
| 423 | 
      goto closeandexit; | 
| 424 | 
    }; | 
| 425 | 
    l0tr = (TTree*)l0File->Get("Physics"); | 
| 426 | 
    if ( !l0tr ) { | 
| 427 | 
      if ( verbose ) printf(" TOF - ERROR: no Physics tree in Level0 file\n"); | 
| 428 | 
      l0File->Close(); | 
| 429 | 
      code = -7; | 
| 430 | 
      goto closeandexit; | 
| 431 | 
    }; | 
| 432 | 
    l0head = l0tr->GetBranch("Header"); | 
| 433 | 
    if ( !l0head ) { | 
| 434 | 
      if ( verbose ) printf(" TOF - ERROR: no Header branch in Level0 tree\n"); | 
| 435 | 
      l0File->Close(); | 
| 436 | 
      code = -8; | 
| 437 | 
      goto closeandexit;     | 
| 438 | 
    }; | 
| 439 | 
    l0registry = l0tr->GetBranch("Registry"); | 
| 440 | 
    if ( !l0registry ) { | 
| 441 | 
      if ( verbose ) printf(" TOF - ERROR: no Registry branch in Level0 tree\n"); | 
| 442 | 
      l0File->Close(); | 
| 443 | 
      code = -9; | 
| 444 | 
      goto closeandexit;     | 
| 445 | 
    }; | 
| 446 | 
    l0trig = l0tr->GetBranch("Trigger"); | 
| 447 | 
    if ( !l0trig ) { | 
| 448 | 
      if ( verbose ) printf(" TOF - ERROR: no Trigger branch in Level0 tree\n"); | 
| 449 | 
      l0File->Close(); | 
| 450 | 
      code = -300; | 
| 451 | 
      goto closeandexit; | 
| 452 | 
    }; | 
| 453 | 
    l0tof = l0tr->GetBranch("Tof"); | 
| 454 | 
    if ( !l0tof ) { | 
| 455 | 
      if ( verbose ) printf(" TOF - ERROR: no ToF branch in Level0 tree\n"); | 
| 456 | 
      l0File->Close(); | 
| 457 | 
      code = -303; | 
| 458 | 
      goto closeandexit; | 
| 459 | 
    }; | 
| 460 | 
    // | 
| 461 | 
    l0tr->SetBranchAddress("Trigger", &trig); | 
| 462 | 
    l0tr->SetBranchAddress("Tof", &tofEvent); | 
| 463 | 
    l0tr->SetBranchAddress("Registry", &l0reg); | 
| 464 | 
    // | 
| 465 | 
    nevents = l0registry->GetEntries(); | 
| 466 | 
    // | 
| 467 | 
    if ( nevents < 1 ) { | 
| 468 | 
      if ( verbose ) printf(" TOF - ERROR: Level0 file is empty\n\n"); | 
| 469 | 
      l0File->Close(); | 
| 470 | 
      code = -11; | 
| 471 | 
      goto closeandexit; | 
| 472 | 
    }; | 
| 473 | 
    // | 
| 474 | 
    if ( runinfo->EV_REG_PHYS_TO > nevents-1 ) { | 
| 475 | 
      if ( verbose ) printf(" TOF - ERROR: too few entries in the registry tree\n"); | 
| 476 | 
      l0File->Close(); | 
| 477 | 
      code = -12; | 
| 478 | 
      goto closeandexit; | 
| 479 | 
    }; | 
| 480 | 
    // | 
| 481 | 
    // Check if we have to load parameter files (or calibration associated to runs and not to events) | 
| 482 | 
    // | 
| 483 | 
    // for example let's assume that we could have different magnetic field maps for different runs: | 
| 484 | 
    // | 
| 485 | 
    if ( trkpar1 || ( tttrkpar1 != 0 && tttrkpar1 < runinfo->RUNHEADER_TIME ) ){ | 
| 486 | 
      trkpar1 = false; | 
| 487 | 
      // read from DB infos about Magnetic filed maps | 
| 488 | 
      glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"field",dbc); // parameters stored in DB in GL_PRAM table | 
| 489 | 
      tttrkpar1 = glparam->TO_TIME;   | 
| 490 | 
      // ---------------------------- | 
| 491 | 
      // Read the magnetic field | 
| 492 | 
      // ---------------------------- | 
| 493 | 
      if ( verbose ) printf(" Reading magnetic field maps: \n"); | 
| 494 | 
      trk->LoadField(glparam->PATH+glparam->NAME); | 
| 495 | 
      if ( verbose ) printf("\n"); | 
| 496 | 
    }; | 
| 497 | 
    // Bug Found: Emiliano 30 May 2006 | 
| 498 | 
    //     if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){ | 
| 499 | 
    //       tofpar1 = false; | 
| 500 | 
    //       // | 
| 501 | 
    //       glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"tof param",dbc); // parameters stored in DB in GL_PRAM table | 
| 502 | 
    //       // | 
| 503 | 
    //       tttofpar1 = glparam->TO_TIME;   | 
| 504 | 
    //       rdtofcal((char *)(glparam->PATH+glparam->NAME).Data()); | 
| 505 | 
    //     }; | 
| 506 | 
    // Corrected: | 
| 507 | 
    if ( tofpar1 || ( tttofpar1 != 0 && tttofpar1 < runinfo->RUNHEADER_TIME ) ){ | 
| 508 | 
      tofpar1 = false; | 
| 509 | 
      // | 
| 510 | 
      Int_t error=glparam->Query_GL_PARAM(runinfo->RUNHEADER_TIME,"tof param",dbc); // parameters stored in DB in GL_PRAM table | 
| 511 | 
      if ( error<0 ) { | 
| 512 | 
        code = error; | 
| 513 | 
        goto closeandexit; | 
| 514 | 
      }; | 
| 515 | 
      // | 
| 516 | 
      tttofpar1 = glparam->TO_TIME;   | 
| 517 | 
      rdtofcal((char *)(glparam->PATH+glparam->NAME).Data()); | 
| 518 | 
    }; | 
| 519 | 
    // | 
| 520 | 
    // run over all the events of the run | 
| 521 | 
    // | 
| 522 | 
    if ( verbose ) printf("\n Ready to start! \n\n Processed events: \n\n"); | 
| 523 | 
    // | 
| 524 | 
    for ( re = runinfo->EV_REG_PHYS_FROM; re <= runinfo->EV_REG_PHYS_TO; re++){ | 
| 525 | 
      // | 
| 526 | 
      if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000);    | 
| 527 | 
      // | 
| 528 | 
      l0registry->GetEntry(re); | 
| 529 | 
      // | 
| 530 | 
      // absolute time of this event | 
| 531 | 
      // | 
| 532 | 
      atime = l0reg->absTime; | 
| 533 | 
      // | 
| 534 | 
      // physics events is at entry number ei where | 
| 535 | 
      // | 
| 536 | 
      ei = l0reg->event; | 
| 537 | 
      // | 
| 538 | 
      // paranoid check | 
| 539 | 
      // | 
| 540 | 
      if ( atime > runinfo->RUNTRAILER_TIME || atime < runinfo->RUNHEADER_TIME  ) { | 
| 541 | 
        if ( verbose ) printf(" TOF - WARNING: event at time outside the run time window, skipping it\n"); | 
| 542 | 
        goto jumpev; | 
| 543 | 
      }; | 
| 544 | 
      // | 
| 545 | 
      // retrieve tracker informations, the LEVEL2 entry which correspond to our event will be "itr" | 
| 546 | 
      // | 
| 547 | 
      if ( !reprocall ){ | 
| 548 | 
        itr = nobefrun + (re - runinfo->EV_REG_PHYS_FROM); | 
| 549 | 
      } else { | 
| 550 | 
        itr = runinfo->GetFirstEntry() + (re - runinfo->EV_REG_PHYS_FROM); | 
| 551 | 
      }; | 
| 552 | 
      if ( itr > nevtrkl2 ){  // nevtrkl2 tracker entry number | 
| 553 | 
        if ( verbose ) printf(" TOF - ERROR: no tracker events with entry = %i in Level2 file\n",itr); | 
| 554 | 
        l0File->Close(); | 
| 555 | 
        code = -313; | 
| 556 | 
        goto closeandexit;  | 
| 557 | 
      }; | 
| 558 | 
      tracker->GetEntry(itr);  | 
| 559 | 
      /// | 
| 560 | 
      // | 
| 561 | 
      l0tof->GetEntry(ei); | 
| 562 | 
      l0trig->GetEntry(ei); | 
| 563 | 
      /// | 
| 564 | 
      // | 
| 565 | 
      procev++; | 
| 566 | 
      // | 
| 567 | 
      // start processing | 
| 568 | 
      // | 
| 569 | 
      tof = new ToFLevel2(); | 
| 570 | 
 | 
| 571 | 
      // | 
| 572 | 
      // Here we will use some procedure to calibrate our data and put some kind of informations in the cinput structure | 
| 573 | 
 | 
| 574 | 
      for (Int_t gg=0; gg<4;gg++){ | 
| 575 | 
        for (Int_t hh=0; hh<12;hh++){ | 
| 576 | 
          tofinput_.tdc[hh][gg]=tofEvent->tdc[gg][hh];           | 
| 577 | 
          tofinput_.adc[hh][gg]=tofEvent->adc[gg][hh]; | 
| 578 | 
        };           | 
| 579 | 
      }; | 
| 580 | 
 | 
| 581 | 
      for (Int_t hh=0; hh<5;hh++){ | 
| 582 | 
        tofinput_.patterntrig[hh]=trig->patterntrig[hh]; | 
| 583 | 
      };             | 
| 584 | 
      // | 
| 585 | 
      // Here we have calibrated data, ready to be passed to the FORTRAN routine which will extract common and track-related  variables. | 
| 586 | 
      // | 
| 587 | 
      // | 
| 588 | 
      // Calculate tracks informations from ToF alone | 
| 589 | 
      // | 
| 590 | 
      tofl2com(); | 
| 591 | 
      // | 
| 592 | 
      // and now we must copy from the output structure to the level2 class: | 
| 593 | 
      // | 
| 594 | 
      // common variables not related to the track | 
| 595 | 
      for (Int_t kk=0; kk<3;kk++){ | 
| 596 | 
        tof->xtofpos[kk] = tofoutput_.xtofpos[kk]; | 
| 597 | 
        tof->ytofpos[kk] = tofoutput_.ytofpos[kk]; | 
| 598 | 
      } | 
| 599 | 
 | 
| 600 | 
      for (Int_t kk=0; kk<6;kk++){ | 
| 601 | 
        tof->tof_i_flag[kk]=tofoutput_.tof_i_flag[kk]; | 
| 602 | 
        tof->tof_j_flag[kk]=tofoutput_.tof_j_flag[kk]; | 
| 603 | 
      } | 
| 604 | 
 | 
| 605 | 
      for (Int_t kk=0; kk<13;kk++){ | 
| 606 | 
        tof->betatof_a[kk] = tofoutput_.betatof_a[kk];   | 
| 607 | 
      } | 
| 608 | 
       | 
| 609 | 
      for (Int_t hh=0; hh<12;hh++){ | 
| 610 | 
        for (Int_t kk=0; kk<4;kk++){ | 
| 611 | 
          tof->adctof_c[hh][kk] = tofoutput_.adctof_c[hh][kk]; | 
| 612 | 
        } | 
| 613 | 
      } | 
| 614 | 
 | 
| 615 | 
      for (Int_t hh=0; hh<12;hh++){ | 
| 616 | 
        for (Int_t kk=0; kk<4;kk++){ | 
| 617 | 
          tof->tdc_c[hh][kk] = tofoutput_.tdc_c[hh][kk]; | 
| 618 | 
        } | 
| 619 | 
      } | 
| 620 | 
       | 
| 621 | 
      // | 
| 622 | 
      // Calculate track-related variables  | 
| 623 | 
      // | 
| 624 | 
      // | 
| 625 | 
      // Calculate variables related to tracks only if we have at least one track (from selftrigger and/or tracker) | 
| 626 | 
      // | 
| 627 | 
      ntrkentry = 0; | 
| 628 | 
      // | 
| 629 | 
      //      if ( trk->GetNTracks() > 0 ){ | 
| 630 | 
      if ( trk->ntrk() > 0 ){ | 
| 631 | 
        // | 
| 632 | 
        // We have at least one track | 
| 633 | 
        // | 
| 634 | 
        // | 
| 635 | 
        // Run over tracks | 
| 636 | 
        // | 
| 637 | 
        // example: how to use tracker data | 
| 638 | 
        for(Int_t nt=0; nt < trk->ntrk(); nt++){   | 
| 639 | 
          // | 
| 640 | 
          TrkTrack *ptt = trk->GetStoredTrack(nt);  | 
| 641 | 
          // | 
| 642 | 
          // Copy the alpha vector in the input structure | 
| 643 | 
          // | 
| 644 | 
          for (Int_t e = 0; e < 5 ; e++){ | 
| 645 | 
            tofinput_.al_pp[e] = ptt->al[e]; | 
| 646 | 
          };       | 
| 647 | 
          // | 
| 648 | 
          // Get tracker related variables for this track | 
| 649 | 
          // | 
| 650 | 
          toftrk(); | 
| 651 | 
          // | 
| 652 | 
          // Copy values in the class from the structure (we need to use a temporary class to store variables). | 
| 653 | 
          // | 
| 654 | 
          ToFTrkVar *t_tof = new ToFTrkVar(); | 
| 655 | 
          for (Int_t kk=0; kk<13;kk++){ | 
| 656 | 
            t_tof->beta_a[kk] = tofoutput_.beta_a[kk]; | 
| 657 | 
          } | 
| 658 | 
           | 
| 659 | 
          for (Int_t hh=0; hh<12;hh++){ | 
| 660 | 
            for (Int_t kk=0; kk<4;kk++){ | 
| 661 | 
              t_tof->adc_c[hh][kk] = tofoutput_.adc_c[hh][kk]; | 
| 662 | 
            } | 
| 663 | 
          } | 
| 664 | 
          // | 
| 665 | 
          // Store the tracker track number in order to be sure to have shyncronized data during analysis | 
| 666 | 
          // | 
| 667 | 
          t_tof->trkseqno = nt; | 
| 668 | 
          // | 
| 669 | 
          // create a new object for this event with track-related variables | 
| 670 | 
          // | 
| 671 | 
          TClonesArray &t = *tof->ToFTrk; | 
| 672 | 
          new(t[ntrkentry]) ToFTrkVar(*t_tof); | 
| 673 | 
          // | 
| 674 | 
          ntrkentry++;   | 
| 675 | 
          // | 
| 676 | 
        }; // loop on all the tracks | 
| 677 | 
      };  | 
| 678 | 
      // | 
| 679 | 
      // Fill the rootple  | 
| 680 | 
      // | 
| 681 | 
      toft->Fill(); | 
| 682 | 
      //     | 
| 683 | 
      // | 
| 684 | 
    jumpev: | 
| 685 | 
      debug = false; | 
| 686 | 
      // | 
| 687 | 
    }; | 
| 688 | 
  }; // process all the runs | 
| 689 | 
  // | 
| 690 | 
  if ( verbose ) printf("\n Finished processing data \n"); | 
| 691 | 
  // | 
| 692 | 
 closeandexit: | 
| 693 | 
  // | 
| 694 | 
  // 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. | 
| 695 | 
  // | 
| 696 | 
  if ( !reprocall && reproc && code >= 0 ){ | 
| 697 | 
    if ( totfileentries > noaftrun ){ | 
| 698 | 
      if ( verbose ) printf("\n Post-processing: copying events from the old tree after the processed run\n");    | 
| 699 | 
      if ( verbose ) printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run); | 
| 700 | 
      if ( verbose ) printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries); | 
| 701 | 
      for (UInt_t j = noaftrun; j < totfileentries; j++ ){ | 
| 702 | 
        // | 
| 703 | 
        // Get entry from old tree | 
| 704 | 
        // | 
| 705 | 
        toftclone->GetEntry(j); | 
| 706 | 
        // | 
| 707 | 
        // copy tofclone to tof | 
| 708 | 
        // | 
| 709 | 
        tof = new ToFLevel2(); | 
| 710 | 
        memcpy(&tof,&tofclone,sizeof(tofclone)); | 
| 711 | 
        // | 
| 712 | 
        // Fill entry in the new tree | 
| 713 | 
        // | 
| 714 | 
        toft->Fill(); | 
| 715 | 
      }; | 
| 716 | 
      if ( verbose ) printf(" Finished successful copying!\n"); | 
| 717 | 
    }; | 
| 718 | 
  }; | 
| 719 | 
  // | 
| 720 | 
  // Close files, delete old tree(s), write and close level2 file  | 
| 721 | 
  // | 
| 722 | 
  if ( l0File ) l0File->Close(); | 
| 723 | 
  if ( tempfile ) tempfile->Close();             | 
| 724 | 
  gSystem->Unlink(tempname.str().c_str()); | 
| 725 | 
  if ( tracker )  tracker->Delete(); // delete tracker tree from memory only to avoid writing a copy to file! | 
| 726 | 
  // | 
| 727 | 
  if ( code < 0 && verbose ) printf("\n TOF - ERROR: an error occurred, try to save anyway...\n"); | 
| 728 | 
  if ( verbose ) printf("\n Writing and closing rootple\n"); | 
| 729 | 
  if ( runinfo ) runinfo->Close();     | 
| 730 | 
  if ( toft ) toft->SetName("ToF");     | 
| 731 | 
  if ( file ){ | 
| 732 | 
    file->cd(); | 
| 733 | 
    file->Write("ToF"); | 
| 734 | 
  }; | 
| 735 | 
  // | 
| 736 | 
  gSystem->Unlink(toffolder.str().c_str()); | 
| 737 | 
  // | 
| 738 | 
  // the end | 
| 739 | 
  // | 
| 740 | 
  if ( verbose ) printf("\n Exiting...\n"); | 
| 741 | 
  if(toft)toft->Delete(); | 
| 742 | 
  if ( code < 0 ) throw code; | 
| 743 | 
  return(code); | 
| 744 | 
} |