| 1 |
mocchiut |
1.1 |
// |
| 2 |
pam-rm2 |
1.11 |
// Given a calibration and a data file this program create an ntuple with LEVEL2 S4 variables |
| 3 |
|
|
// |
| 4 |
|
|
// |
| 5 |
mocchiut |
1.1 |
// 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 |
mocchiut |
1.4 |
#include <TGraph.h> |
| 16 |
|
|
#include <TF1.h> |
| 17 |
mocchiut |
1.1 |
#include <TTree.h> |
| 18 |
|
|
#include <TClassEdit.h> |
| 19 |
|
|
#include <TObject.h> |
| 20 |
|
|
#include <TList.h> |
| 21 |
mocchiut |
1.4 |
#include <TArrayI.h> |
| 22 |
|
|
#include <TArrayD.h> |
| 23 |
mocchiut |
1.1 |
#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 |
mocchiut |
1.10 |
// RunInfo header |
| 36 |
|
|
// |
| 37 |
|
|
#include <RunInfo.h> |
| 38 |
|
|
#include <GLTables.h> |
| 39 |
|
|
// |
| 40 |
mocchiut |
1.1 |
// 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 |
pam-rm2 |
1.11 |
// |
| 55 |
mocchiut |
1.1 |
using namespace std; |
| 56 |
mocchiut |
1.4 |
// |
| 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 |
mocchiut |
1.18 |
// if ( !dbc->IsConnected() ) throw -504; |
| 75 |
mocchiut |
1.16 |
Int_t s4sig = glS4calib->Query_GL_S4_CALIB(atime, dbc); |
| 76 |
mocchiut |
1.17 |
if ( s4sig != -57 && s4sig < 0 ){ |
| 77 |
|
|
parametri->AddAt(0.,0); |
| 78 |
|
|
parametri->AddAt(0.,1); |
| 79 |
|
|
return parametri; |
| 80 |
|
|
}; |
| 81 |
mocchiut |
1.16 |
if ( s4sig < 0 ) throw s4sig; |
| 82 |
mocchiut |
1.4 |
// |
| 83 |
|
|
GL_ROOT *glroot = new GL_ROOT(); |
| 84 |
mocchiut |
1.18 |
// if ( !dbc->IsConnected() ) throw -504; |
| 85 |
mocchiut |
1.4 |
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 |
mocchiut |
1.25 |
if ( tr->GetEntry(glS4calib->EV_ROOT) <= 0 ) throw -36; |
| 109 |
mocchiut |
1.4 |
// |
| 110 |
pam-rm2 |
1.11 |
// Variables initialization |
| 111 |
mocchiut |
1.4 |
// |
| 112 |
|
|
Double_t mip[3]={1, 30, 300}; |
| 113 |
|
|
Double_t adc[3] = {0.,0.,0.}; |
| 114 |
|
|
// |
| 115 |
pam-rm2 |
1.11 |
// Fit calibrations and find parameters to calibrate data |
| 116 |
mocchiut |
1.4 |
// |
| 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 |
pam-rm2 |
1.11 |
TF1 *func = new TF1("fitf", fitf, -1., 400., 2); // function definition with 2 parameters |
| 145 |
mocchiut |
1.4 |
// |
| 146 |
pam-rm2 |
1.11 |
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 |
mocchiut |
1.4 |
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 |
mocchiut |
1.1 |
// CORE ROUTINE |
| 164 |
|
|
// |
| 165 |
|
|
// |
| 166 |
mocchiut |
1.19 |
int S4Core(UInt_t run, TFile *file,GL_TABLES *glt, Int_t S4argc, char *S4argv[]){ |
| 167 |
mocchiut |
1.1 |
// |
| 168 |
pam-rm2 |
1.11 |
// Set these to true to have a verbose output. |
| 169 |
mocchiut |
1.1 |
// |
| 170 |
mocchiut |
1.4 |
Bool_t debug = false; |
| 171 |
|
|
Bool_t verbose = false; |
| 172 |
mocchiut |
1.1 |
// |
| 173 |
pam-rm2 |
1.11 |
// |
| 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 |
mocchiut |
1.14 |
TString processFolder = Form("S4Folder_%u",run); |
| 180 |
mocchiut |
1.1 |
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 |
mocchiut |
1.13 |
debug = true; |
| 193 |
mocchiut |
1.1 |
}; |
| 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 |
mocchiut |
1.4 |
UInt_t nevents = 0; |
| 204 |
mocchiut |
1.1 |
// |
| 205 |
pam-rm2 |
1.11 |
// Variables needed to reprocess data |
| 206 |
mocchiut |
1.1 |
// |
| 207 |
mocchiut |
1.9 |
Long64_t maxsize = 10000000000LL; |
| 208 |
|
|
TTree::SetMaxTreeSize(maxsize); |
| 209 |
|
|
// |
| 210 |
mocchiut |
1.1 |
TString S4version; |
| 211 |
|
|
ItoRunInfo *runinfo = 0; |
| 212 |
mocchiut |
1.4 |
TArrayI *runlist = 0; |
| 213 |
mocchiut |
1.1 |
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 |
mocchiut |
1.4 |
UInt_t totfileentries = 0; |
| 222 |
|
|
UInt_t idRun = 0; |
| 223 |
|
|
Double_t ParamFit0 = 0.; |
| 224 |
|
|
Double_t ParamFit1 = 0.; |
| 225 |
mocchiut |
1.1 |
// |
| 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 |
mocchiut |
1.4 |
pamela::PscuHeader *ph = 0; |
| 245 |
mocchiut |
1.1 |
// |
| 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 |
mocchiut |
1.4 |
UInt_t atime = 0; |
| 254 |
|
|
// Int_t ei = 0; |
| 255 |
|
|
UInt_t re = 0; |
| 256 |
mocchiut |
1.1 |
// |
| 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 |
mocchiut |
1.17 |
Bool_t myfold = false; |
| 271 |
mocchiut |
1.1 |
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 |
mocchiut |
1.24 |
UInt_t totnorun = 0; |
| 279 |
mocchiut |
1.1 |
// |
| 280 |
|
|
// DB classes |
| 281 |
|
|
// |
| 282 |
|
|
GL_ROOT *glroot = new GL_ROOT(); |
| 283 |
mocchiut |
1.4 |
GL_TIMESYNC *dbtime = 0; |
| 284 |
mocchiut |
1.1 |
// |
| 285 |
|
|
// Let's start! |
| 286 |
|
|
// |
| 287 |
|
|
// 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 |
| 288 |
|
|
// if run != 0 we must process only that run but first we have to check if the tree MyDetector2 already exist in the file |
| 289 |
|
|
// if it exists we are reprocessing data and we must delete that entries, if not we must create it. |
| 290 |
|
|
// |
| 291 |
mocchiut |
1.4 |
if ( run == 0 ) reproc = true; |
| 292 |
mocchiut |
1.1 |
// |
| 293 |
|
|
// |
| 294 |
|
|
// Output file is "outputfile" |
| 295 |
|
|
// |
| 296 |
|
|
if ( !file->IsOpen() ){ |
| 297 |
|
|
//printf(" S4 - ERROR: cannot open file for writing\n"); |
| 298 |
|
|
throw -501; |
| 299 |
|
|
}; |
| 300 |
|
|
// |
| 301 |
|
|
// Retrieve GL_RUN variables from the level2 file |
| 302 |
|
|
// |
| 303 |
|
|
S4version = S4Info(false); // we should decide how to handle versioning system |
| 304 |
|
|
// |
| 305 |
|
|
// create an interface to RunInfo called "runinfo" |
| 306 |
|
|
// |
| 307 |
|
|
runinfo = new ItoRunInfo(file); |
| 308 |
|
|
// |
| 309 |
|
|
// open "Run" tree in level2 file, if not existing return an error (sngl != 0) |
| 310 |
|
|
// |
| 311 |
|
|
sgnl = 0; |
| 312 |
|
|
sgnl = runinfo->Update(run,"S4",S4version); |
| 313 |
|
|
if ( sgnl ){ |
| 314 |
|
|
//printf(" S4 - ERROR: RunInfo exited with non-zero status\n"); |
| 315 |
|
|
code = sgnl; |
| 316 |
|
|
goto closeandexit; |
| 317 |
|
|
} else { |
| 318 |
|
|
sgnl = 0; |
| 319 |
|
|
}; |
| 320 |
|
|
// |
| 321 |
|
|
// number of events in the file BEFORE the first event of our run |
| 322 |
|
|
// |
| 323 |
|
|
nobefrun = runinfo->GetFirstEntry(); |
| 324 |
|
|
// |
| 325 |
|
|
// total number of events in the file |
| 326 |
|
|
// |
| 327 |
|
|
totfileentries = runinfo->GetFileEntries(); |
| 328 |
|
|
// |
| 329 |
|
|
// first file entry AFTER the last event of our run |
| 330 |
|
|
// |
| 331 |
|
|
noaftrun = runinfo->GetLastEntry() + 1; |
| 332 |
|
|
// |
| 333 |
|
|
// number of run to be processed |
| 334 |
|
|
// |
| 335 |
mocchiut |
1.24 |
numbofrun = runinfo->GetNoRun(); |
| 336 |
|
|
totnorun = runinfo->GetRunEntries(); |
| 337 |
mocchiut |
1.1 |
// |
| 338 |
|
|
// Try to access the S4 tree in the file, if it exists we are reprocessing data if not we are processing a new run |
| 339 |
|
|
// |
| 340 |
|
|
S4trclone = (TTree*)file->Get("S4"); |
| 341 |
|
|
// |
| 342 |
|
|
if ( !S4trclone ){ |
| 343 |
|
|
// |
| 344 |
|
|
// tree does not exist, we are not reprocessing |
| 345 |
|
|
// |
| 346 |
|
|
reproc = false; |
| 347 |
mocchiut |
1.4 |
if ( run == 0 ){ |
| 348 |
mocchiut |
1.1 |
if (verbose) printf(" S4 - WARNING: you are reprocessing data but S4 tree does not exist!\n"); |
| 349 |
|
|
} |
| 350 |
mocchiut |
1.4 |
if ( runinfo->IsReprocessing() && run != 0 ) { |
| 351 |
mocchiut |
1.1 |
if (verbose) printf(" S4 - WARNING: it seems you are not reprocessing data but S4\n versioning information already exists in RunInfo.\n"); |
| 352 |
|
|
} |
| 353 |
|
|
} else { |
| 354 |
|
|
// |
| 355 |
|
|
// tree exists, we are reprocessing data. Are we reprocessing a single run or all the file? |
| 356 |
|
|
// |
| 357 |
mocchiut |
1.9 |
S4trclone->SetAutoSave(900000000000000LL); |
| 358 |
mocchiut |
1.1 |
reproc = true; |
| 359 |
|
|
// |
| 360 |
|
|
// update versioning information |
| 361 |
|
|
// |
| 362 |
|
|
if (verbose) printf("\n Preparing the pre-processing...\n"); |
| 363 |
|
|
// |
| 364 |
mocchiut |
1.15 |
if ( run == 0 || totnorun == 1 ){ |
| 365 |
mocchiut |
1.1 |
// |
| 366 |
|
|
// we are reprocessing all the file |
| 367 |
|
|
// 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 |
| 368 |
|
|
// |
| 369 |
|
|
reprocall = true; |
| 370 |
|
|
// |
| 371 |
|
|
if (verbose) printf("\n S4 - WARNING: Reprocessing all runs\n"); |
| 372 |
|
|
// |
| 373 |
|
|
} else { |
| 374 |
|
|
// |
| 375 |
|
|
// 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 |
| 376 |
|
|
// |
| 377 |
|
|
reprocall = false; |
| 378 |
|
|
// |
| 379 |
mocchiut |
1.4 |
if (verbose) printf("\n S4 - WARNING: Reprocessing run number %u \n",run); |
| 380 |
mocchiut |
1.1 |
// |
| 381 |
|
|
// copying old tree to a new file |
| 382 |
|
|
// |
| 383 |
mocchiut |
1.17 |
gSystem->MakeDirectory(S4folder.str().c_str()); |
| 384 |
|
|
myfold = true; |
| 385 |
mocchiut |
1.1 |
tempfile = new TFile(tempname.str().c_str(),"RECREATE"); |
| 386 |
|
|
tempS4 = S4trclone->CloneTree(-1,"fast"); |
| 387 |
|
|
tempS4->SetName("S4-old"); |
| 388 |
|
|
tempfile->Write(); |
| 389 |
|
|
tempfile->Close(); |
| 390 |
|
|
} |
| 391 |
|
|
// |
| 392 |
|
|
// Delete the old tree from old file and memory |
| 393 |
|
|
// |
| 394 |
|
|
S4trclone->Delete("all"); |
| 395 |
|
|
// |
| 396 |
|
|
if (verbose) printf(" ...done!\n"); |
| 397 |
|
|
// |
| 398 |
|
|
}; |
| 399 |
|
|
// |
| 400 |
|
|
// create mydetector tree mydect |
| 401 |
|
|
// |
| 402 |
|
|
file->cd(); |
| 403 |
|
|
S4tr = new TTree("S4-new","PAMELA Level2 S4 data"); |
| 404 |
mocchiut |
1.9 |
S4tr->SetAutoSave(900000000000000LL); |
| 405 |
mocchiut |
1.1 |
S4tr->Branch("S4Level2","S4Level2",&s4); |
| 406 |
|
|
// |
| 407 |
|
|
if ( reproc && !reprocall ){ |
| 408 |
|
|
// |
| 409 |
|
|
// open new file and retrieve also tree informations |
| 410 |
|
|
// |
| 411 |
|
|
tempfile = new TFile(tempname.str().c_str(),"READ"); |
| 412 |
|
|
S4trclone = (TTree*)tempfile->Get("S4-old"); |
| 413 |
mocchiut |
1.9 |
S4trclone->SetAutoSave(900000000000000LL); |
| 414 |
mocchiut |
1.1 |
S4trclone->SetBranchAddress("S4Level2",&s4clone); |
| 415 |
|
|
// |
| 416 |
|
|
if ( nobefrun > 0 ){ |
| 417 |
|
|
if (verbose){ |
| 418 |
|
|
printf("\n Pre-processing: copying events from the old tree before the processed run\n"); |
| 419 |
mocchiut |
1.4 |
printf(" Copying %u events in the file which are before the beginning of the run %u \n",nobefrun,run); |
| 420 |
mocchiut |
1.1 |
printf(" Start copying at event number 0, end copying at event number %u \n",nobefrun); |
| 421 |
|
|
} |
| 422 |
|
|
for (UInt_t j = 0; j < nobefrun; j++){ |
| 423 |
|
|
// |
| 424 |
mocchiut |
1.25 |
if ( S4trclone->GetEntry(j) <= 0 ) throw -36; |
| 425 |
mocchiut |
1.1 |
// |
| 426 |
|
|
// copy s4clone to mydec |
| 427 |
|
|
// |
| 428 |
mocchiut |
1.2 |
s4->Clear(); |
| 429 |
mocchiut |
1.4 |
// |
| 430 |
mocchiut |
1.1 |
memcpy(&s4,&s4clone,sizeof(s4clone)); |
| 431 |
|
|
// |
| 432 |
|
|
// Fill entry in the new tree |
| 433 |
|
|
// |
| 434 |
|
|
S4tr->Fill(); |
| 435 |
|
|
// |
| 436 |
|
|
}; |
| 437 |
|
|
if (verbose) printf(" Finished successful copying!\n"); |
| 438 |
|
|
}; |
| 439 |
|
|
}; |
| 440 |
|
|
// |
| 441 |
|
|
// Get the list of run to be processed, if only one run has to be processed the list will contain one entry only. |
| 442 |
|
|
// |
| 443 |
|
|
runlist = runinfo->GetRunList(); |
| 444 |
|
|
// |
| 445 |
|
|
// Loop over the run to be processed |
| 446 |
|
|
// |
| 447 |
|
|
for (UInt_t irun=0; irun < numbofrun; irun++){ |
| 448 |
|
|
// |
| 449 |
|
|
// retrieve the first run ID to be processed using the RunInfo list |
| 450 |
|
|
// |
| 451 |
|
|
idRun = runlist->At(irun); |
| 452 |
|
|
if (verbose){ |
| 453 |
|
|
printf("\n\n\n ####################################################################### \n"); |
| 454 |
mocchiut |
1.4 |
printf(" PROCESSING RUN NUMBER %u \n",idRun); |
| 455 |
mocchiut |
1.1 |
printf(" ####################################################################### \n\n\n"); |
| 456 |
|
|
} |
| 457 |
|
|
// |
| 458 |
mocchiut |
1.4 |
runinfo->ID_ROOT_L0 = 0; |
| 459 |
mocchiut |
1.1 |
// |
| 460 |
|
|
// store in the runinfo class the GL_RUN variables for our run |
| 461 |
|
|
// |
| 462 |
|
|
sgnl = 0; |
| 463 |
|
|
sgnl = runinfo->GetRunInfo(idRun); |
| 464 |
|
|
if ( sgnl ){ |
| 465 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: RunInfo exited with non-zero status\n"); |
| 466 |
mocchiut |
1.1 |
code = sgnl; |
| 467 |
|
|
goto closeandexit; |
| 468 |
|
|
} else { |
| 469 |
|
|
sgnl = 0; |
| 470 |
|
|
}; |
| 471 |
|
|
// |
| 472 |
mocchiut |
1.5 |
// now you can access that variables using the RunInfo class this way runinfo->ID_ROOT_L0 |
| 473 |
mocchiut |
1.1 |
// |
| 474 |
mocchiut |
1.4 |
if ( runinfo->ID_ROOT_L0 == 0 ){ |
| 475 |
|
|
if ( debug ) printf("\n S4 - ERROR: no run with ID_RUN = %u \n\n Exiting... \n\n",idRun); |
| 476 |
mocchiut |
1.1 |
code = -5; |
| 477 |
|
|
goto closeandexit; |
| 478 |
|
|
}; |
| 479 |
|
|
// |
| 480 |
mocchiut |
1.4 |
// prepare the timesync for the db |
| 481 |
|
|
// |
| 482 |
mocchiut |
1.19 |
TString host = glt->CGetHost(); |
| 483 |
|
|
TString user = glt->CGetUser(); |
| 484 |
|
|
TString psw = glt->CGetPsw(); |
| 485 |
|
|
TSQLServer *dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
| 486 |
|
|
if ( !dbc->IsConnected() ) throw -504; |
| 487 |
mocchiut |
1.20 |
stringstream myquery; |
| 488 |
|
|
myquery.str(""); |
| 489 |
|
|
myquery << "SET time_zone='+0:00'"; |
| 490 |
|
|
dbc->Query(myquery.str().c_str()); |
| 491 |
mocchiut |
1.4 |
dbtime = new GL_TIMESYNC(runinfo->ID_ROOT_L0,"ID",dbc); |
| 492 |
|
|
// |
| 493 |
mocchiut |
1.1 |
// Search in the DB the path and name of the LEVEL0 file to be processed. |
| 494 |
|
|
// |
| 495 |
mocchiut |
1.18 |
// if ( !dbc->IsConnected() ) throw -504; |
| 496 |
mocchiut |
1.4 |
glroot->Query_GL_ROOT(runinfo->ID_ROOT_L0,dbc); |
| 497 |
mocchiut |
1.1 |
// |
| 498 |
|
|
ftmpname.str(""); |
| 499 |
|
|
ftmpname << glroot->PATH.Data() << "/"; |
| 500 |
|
|
ftmpname << glroot->NAME.Data(); |
| 501 |
|
|
fname = ftmpname.str().c_str(); |
| 502 |
|
|
// |
| 503 |
|
|
// print out informations |
| 504 |
|
|
// |
| 505 |
mocchiut |
1.4 |
totevent = runinfo->NEVENTS; |
| 506 |
mocchiut |
1.1 |
if (verbose){ |
| 507 |
|
|
printf("\n LEVEL0 data file: %s \n",fname.Data()); |
| 508 |
mocchiut |
1.4 |
printf(" RUN HEADER absolute time is: %u \n",runinfo->RUNHEADER_TIME); |
| 509 |
|
|
printf(" RUN TRAILER absolute time is: %u \n",runinfo->RUNTRAILER_TIME); |
| 510 |
|
|
printf(" %i events to be processed for run %u: from %u to %u \n\n",totevent,idRun,runinfo->EV_FROM,(runinfo->EV_FROM+totevent)); |
| 511 |
mocchiut |
1.1 |
} |
| 512 |
|
|
// |
| 513 |
mocchiut |
1.22 |
// if ( !totevent ) goto closeandexit; |
| 514 |
mocchiut |
1.21 |
// |
| 515 |
mocchiut |
1.1 |
// Open Level0 file |
| 516 |
|
|
// |
| 517 |
|
|
l0File = new TFile(fname.Data()); |
| 518 |
|
|
if ( !l0File ) { |
| 519 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: problems opening Level0 file\n"); |
| 520 |
mocchiut |
1.1 |
code = -6; |
| 521 |
|
|
goto closeandexit; |
| 522 |
|
|
}; |
| 523 |
mocchiut |
1.4 |
// |
| 524 |
mocchiut |
1.1 |
l0tr = (TTree*)l0File->Get("Physics"); |
| 525 |
|
|
if ( !l0tr ) { |
| 526 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: no Physics tree in Level0 file\n"); |
| 527 |
mocchiut |
1.1 |
l0File->Close(); |
| 528 |
|
|
code = -7; |
| 529 |
|
|
goto closeandexit; |
| 530 |
|
|
}; |
| 531 |
|
|
l0head = l0tr->GetBranch("Header"); |
| 532 |
|
|
if ( !l0head ) { |
| 533 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: no Header branch in Level0 tree\n"); |
| 534 |
mocchiut |
1.1 |
l0File->Close(); |
| 535 |
|
|
code = -8; |
| 536 |
|
|
goto closeandexit; |
| 537 |
|
|
}; |
| 538 |
|
|
l0S4 = l0tr->GetBranch("S4"); |
| 539 |
|
|
if ( !l0S4 ) { |
| 540 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: no S4 branch in Level0 tree\n"); |
| 541 |
mocchiut |
1.1 |
l0File->Close(); |
| 542 |
|
|
code = -503; |
| 543 |
|
|
goto closeandexit; |
| 544 |
|
|
}; |
| 545 |
|
|
// |
| 546 |
|
|
l0tr->SetBranchAddress("S4", &l0s4e); |
| 547 |
|
|
l0tr->SetBranchAddress("Header", &eh); |
| 548 |
|
|
// |
| 549 |
mocchiut |
1.4 |
nevents = l0S4->GetEntries(); |
| 550 |
mocchiut |
1.1 |
// |
| 551 |
mocchiut |
1.22 |
if ( nevents < 1 && totevent ) { |
| 552 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: Level0 file is empty\n\n"); |
| 553 |
mocchiut |
1.1 |
l0File->Close(); |
| 554 |
|
|
code = -11; |
| 555 |
|
|
goto closeandexit; |
| 556 |
|
|
}; |
| 557 |
|
|
// |
| 558 |
mocchiut |
1.22 |
if ( runinfo->EV_TO > nevents-1 && totevent ) { |
| 559 |
mocchiut |
1.4 |
if ( debug ) printf(" S4 - ERROR: too few entries in the S4 tree\n"); |
| 560 |
mocchiut |
1.1 |
l0File->Close(); |
| 561 |
|
|
code = -12; |
| 562 |
|
|
goto closeandexit; |
| 563 |
|
|
}; |
| 564 |
|
|
// |
| 565 |
|
|
// Check if we have to load parameter files (or calibration associated to runs and not to events) |
| 566 |
|
|
// second query: which is the value of paramfit relative to the calibration? |
| 567 |
|
|
// |
| 568 |
mocchiut |
1.4 |
TArrayD *params = 0; |
| 569 |
|
|
params = S4_paramfit(runinfo->RUNHEADER_TIME, dbc); |
| 570 |
|
|
if ( !params ){ |
| 571 |
|
|
code = -13; |
| 572 |
|
|
goto closeandexit; |
| 573 |
|
|
}; |
| 574 |
|
|
// |
| 575 |
|
|
ParamFit0 = params->At(0); |
| 576 |
|
|
ParamFit1 = params->At(1); |
| 577 |
mocchiut |
1.1 |
// |
| 578 |
|
|
// run over all the events of the run |
| 579 |
|
|
// |
| 580 |
|
|
if (verbose) printf("\n Ready to start! \n\n Processed events: \n\n"); |
| 581 |
|
|
// |
| 582 |
mocchiut |
1.18 |
if ( dbc ){ |
| 583 |
|
|
dbc->Close(); |
| 584 |
pam-fi |
1.26 |
delete dbc; |
| 585 |
|
|
dbc = 0; |
| 586 |
mocchiut |
1.18 |
}; |
| 587 |
|
|
// |
| 588 |
mocchiut |
1.4 |
for ( re = runinfo->EV_FROM; re < (runinfo->EV_FROM+runinfo->NEVENTS); re++){ |
| 589 |
mocchiut |
1.1 |
// |
| 590 |
|
|
if ( procev%1000 == 0 && procev > 0 && verbose ) printf(" %iK \n",procev/1000); |
| 591 |
|
|
// |
| 592 |
mocchiut |
1.25 |
if ( l0head->GetEntry(re) <= 0 ) throw -36; |
| 593 |
mocchiut |
1.1 |
// |
| 594 |
|
|
// absolute time of this event |
| 595 |
|
|
// |
| 596 |
mocchiut |
1.4 |
ph = eh->GetPscuHeader(); |
| 597 |
|
|
atime = dbtime->DBabsTime(ph->GetOrbitalTime()); |
| 598 |
mocchiut |
1.1 |
// |
| 599 |
|
|
// paranoid check |
| 600 |
|
|
// |
| 601 |
mocchiut |
1.23 |
if ( (atime > (runinfo->RUNTRAILER_TIME+1)) || (atime < (runinfo->RUNHEADER_TIME-1)) ) { |
| 602 |
mocchiut |
1.1 |
if (verbose) printf(" S4 - WARNING: event at time outside the run time window, skipping it\n"); |
| 603 |
|
|
goto jumpev; |
| 604 |
|
|
}; |
| 605 |
|
|
// |
| 606 |
|
|
procev++; |
| 607 |
|
|
// |
| 608 |
|
|
// start processing |
| 609 |
|
|
// |
| 610 |
mocchiut |
1.2 |
s4->Clear(); |
| 611 |
mocchiut |
1.25 |
if ( l0S4->GetEntry(re) <= 0 ) throw -36; |
| 612 |
mocchiut |
1.6 |
if (l0s4e->unpackError == 0){ |
| 613 |
|
|
s4->S4adc = l0s4e->S4_DATA; |
| 614 |
|
|
// |
| 615 |
|
|
if ((l0s4e->S4_DATA) > 31 ){ |
| 616 |
|
|
s4->S4calibrated = ParamFit0*((l0s4e->S4_DATA)-32)+ParamFit1; |
| 617 |
|
|
}else{ |
| 618 |
|
|
s4->S4calibrated = 0; |
| 619 |
|
|
} |
| 620 |
|
|
}; |
| 621 |
mocchiut |
1.4 |
// |
| 622 |
mocchiut |
1.7 |
s4->unpackError = l0s4e->unpackError; |
| 623 |
|
|
// |
| 624 |
mocchiut |
1.1 |
S4tr->Fill(); |
| 625 |
|
|
// |
| 626 |
|
|
// |
| 627 |
|
|
jumpev: |
| 628 |
|
|
debug = false; |
| 629 |
|
|
// |
| 630 |
|
|
}; |
| 631 |
|
|
// |
| 632 |
|
|
// Here you may want to clear some variables before processing another run |
| 633 |
|
|
// |
| 634 |
mocchiut |
1.4 |
delete dbtime; |
| 635 |
|
|
if ( params ) delete params; |
| 636 |
|
|
// |
| 637 |
mocchiut |
1.1 |
}; // process all the runs |
| 638 |
|
|
// |
| 639 |
|
|
if (verbose) printf("\n Finished processing data \n"); |
| 640 |
|
|
// |
| 641 |
|
|
closeandexit: |
| 642 |
|
|
// |
| 643 |
|
|
// 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. |
| 644 |
|
|
// |
| 645 |
|
|
if ( !reprocall && reproc && code >= 0 ){ |
| 646 |
|
|
if ( totfileentries > noaftrun ){ |
| 647 |
|
|
if (verbose){ |
| 648 |
|
|
printf("\n Post-processing: copying events from the old tree after the processed run\n"); |
| 649 |
|
|
printf(" Copying %i events in the file which are after the end of the run %i \n",(int)(totfileentries-noaftrun),(int)run); |
| 650 |
|
|
printf(" Start copying at event number %i end copying at event number %i \n",(int)noaftrun,(int)totfileentries); |
| 651 |
|
|
} |
| 652 |
|
|
for (UInt_t j = noaftrun; j < totfileentries; j++ ){ |
| 653 |
|
|
// |
| 654 |
|
|
// Get entry from old tree |
| 655 |
|
|
// |
| 656 |
mocchiut |
1.25 |
if ( S4trclone->GetEntry(j) <= 0 ) throw -36; |
| 657 |
mocchiut |
1.1 |
// |
| 658 |
|
|
// copy s4clone to s4 |
| 659 |
|
|
// |
| 660 |
mocchiut |
1.2 |
// s4 = new S4Level2(); |
| 661 |
|
|
s4->Clear(); |
| 662 |
mocchiut |
1.1 |
memcpy(&s4,&s4clone,sizeof(s4clone)); |
| 663 |
|
|
// |
| 664 |
|
|
// Fill entry in the new tree |
| 665 |
|
|
// |
| 666 |
|
|
S4tr->Fill(); |
| 667 |
|
|
}; |
| 668 |
|
|
if (verbose) printf(" Finished successful copying!\n"); |
| 669 |
|
|
}; |
| 670 |
|
|
}; |
| 671 |
|
|
// |
| 672 |
|
|
// Close files, delete old tree(s), write and close level2 file |
| 673 |
|
|
// |
| 674 |
|
|
if ( l0File ) l0File->Close(); |
| 675 |
|
|
if ( tempfile ) tempfile->Close(); |
| 676 |
mocchiut |
1.17 |
if ( myfold) gSystem->Unlink(tempname.str().c_str()); |
| 677 |
mocchiut |
1.4 |
// |
| 678 |
mocchiut |
1.27 |
//runinfo->Write(); |
| 679 |
mocchiut |
1.1 |
if ( S4tr ) S4tr->SetName("S4"); |
| 680 |
|
|
if ( file ){ |
| 681 |
|
|
file->cd(); |
| 682 |
mocchiut |
1.27 |
S4tr->Write("S4", TObject::kOverwrite); |
| 683 |
|
|
//file->Write(); |
| 684 |
mocchiut |
1.1 |
}; |
| 685 |
|
|
// |
| 686 |
mocchiut |
1.17 |
if ( myfold ) gSystem->Unlink(S4folder.str().c_str()); |
| 687 |
mocchiut |
1.1 |
// |
| 688 |
|
|
// the end |
| 689 |
|
|
// |
| 690 |
|
|
if (verbose) printf("\n Exiting...\n"); |
| 691 |
mocchiut |
1.3 |
// |
| 692 |
|
|
if ( glroot ) delete glroot; |
| 693 |
mocchiut |
1.27 |
if ( runinfo ) runinfo->Close(); |
| 694 |
|
|
if ( runinfo ) delete runinfo; |
| 695 |
mocchiut |
1.3 |
// |
| 696 |
mocchiut |
1.1 |
if(code < 0) throw code; |
| 697 |
|
|
return(code); |
| 698 |
|
|
} |
| 699 |
|
|
|
| 700 |
|
|
|