1 |
#if !defined(__CINT__) || defined(__MAKECINT__) |
2 |
|
3 |
#include <PamLevel2.h> |
4 |
|
5 |
#include <TTree.h> |
6 |
#include <TChain.h> |
7 |
#include <TFile.h> |
8 |
#include <TList.h> |
9 |
#include <TROOT.h> |
10 |
#include <TString.h> |
11 |
#include <TBenchmark.h> |
12 |
#include <TFriendElement.h> |
13 |
#include <TObjectTable.h> |
14 |
|
15 |
#include <stdlib.h> |
16 |
#include <iostream> |
17 |
using namespace std; |
18 |
|
19 |
#endif |
20 |
|
21 |
//=================================== |
22 |
// global variables |
23 |
//=================================== |
24 |
Bool_t DEBUG; |
25 |
TString DIR; |
26 |
TString LIST; |
27 |
TString OPTIONS; |
28 |
ULong64_t MAXEV; |
29 |
TString OUTFILE; |
30 |
|
31 |
PamLevel2 *event = NULL; |
32 |
TChain *tree = NULL; |
33 |
TFile *outfh = NULL; |
34 |
TFile *outft = NULL; |
35 |
TTree *otree = NULL; |
36 |
|
37 |
bool fillTree = false; |
38 |
bool fillHistos = false; |
39 |
|
40 |
//==================================== |
41 |
// Functions to be provided externally |
42 |
//==================================== |
43 |
bool Select(PamLevel2*); |
44 |
void CreateHistos(TFile *); |
45 |
void FillHistos(PamLevel2*); |
46 |
void SaveHistos(TFile *); |
47 |
|
48 |
//========================================== |
49 |
//000000000000000000000000000000000000000000 |
50 |
//========================================== |
51 |
bool Begin(){ |
52 |
|
53 |
//------------------------ |
54 |
// load magnetic field |
55 |
//------------------------ |
56 |
TString fieldpath = gSystem->Getenv("PAM_CALIB"); |
57 |
if(fieldpath.IsNull()){ |
58 |
cout << " **ERROR** : No PAMELA environment variables defined "<<endl; |
59 |
return 0; |
60 |
} |
61 |
fieldpath.Append("/trk-param/field_param-0/"); |
62 |
event->GetTrkLevel2()->LoadField(fieldpath.Data()); |
63 |
|
64 |
|
65 |
//------------------------------ |
66 |
// create output histos/trees |
67 |
//------------------------------ |
68 |
if(fillHistos) CreateHistos(outfh); |
69 |
if(fillTree){ |
70 |
// outft->cd(); |
71 |
// event->CreateCloneTrees(tree,outft); |
72 |
event->CreateCloneTrees(outft); |
73 |
} |
74 |
// gDirectory->ls(); |
75 |
|
76 |
cout << "\nBegin() - done\n\n"; |
77 |
return 1; |
78 |
|
79 |
} |
80 |
//========================================== |
81 |
//000000000000000000000000000000000000000000 |
82 |
//========================================== |
83 |
|
84 |
bool Process(int iev){ |
85 |
|
86 |
|
87 |
if( !Select(event) )return false; |
88 |
|
89 |
if(fillTree)event->FillCloneTrees(); |
90 |
if(fillHistos)FillHistos(event); |
91 |
|
92 |
return true; |
93 |
|
94 |
} |
95 |
//========================================== |
96 |
//000000000000000000000000000000000000000000 |
97 |
//========================================== |
98 |
|
99 |
bool Finish(){ |
100 |
|
101 |
if(fillTree){ |
102 |
outft->cd(); |
103 |
event->WriteCloneTrees(); |
104 |
outft->Close(); |
105 |
} |
106 |
if(fillHistos){ |
107 |
SaveHistos(outfh); |
108 |
outfh->Close(); |
109 |
} |
110 |
|
111 |
cout << "Finish() - done\n"; |
112 |
|
113 |
return 1; |
114 |
} |
115 |
|
116 |
//========================================== |
117 |
//000000000000000000000000000000000000000000 |
118 |
//========================================== |
119 |
Int_t Loop(TString ddir,TString list, ULong64_t nmax, TString options, TString outfile){ |
120 |
|
121 |
// gObjectTable->Print(); |
122 |
|
123 |
if(options.Contains("fillTree"))fillTree=true; |
124 |
if(options.Contains("fillHisto"))fillHistos=true; |
125 |
|
126 |
// ------------------- |
127 |
// create output files |
128 |
// ------------------- |
129 |
TString outfile_t =0; |
130 |
TString outfile_h =0; |
131 |
if( outfile.IsNull() ){ |
132 |
if(!list.IsNull())outfile = list(0,list.Last('.')); |
133 |
else outfile = "output"; |
134 |
}else{ |
135 |
if(outfile.Contains(".root"))outfile = outfile(0,outfile.Last('.')); |
136 |
} |
137 |
if(fillTree){ |
138 |
outfile_t = outfile; |
139 |
outfile_t.Append("-tree.root"); |
140 |
outft = (TFile*)gROOT->FindObject(outfile_t); if (outft) outft->Close(); |
141 |
outft = new TFile(outfile_t,"RECREATE"); |
142 |
if(outft->IsZombie()){ |
143 |
cout << "Output file could not be created\n"; |
144 |
return 1; |
145 |
}; |
146 |
cout << "Created output file: "<<outft->GetName()<<endl; |
147 |
} |
148 |
if(fillHistos){ |
149 |
outfile_h = outfile; |
150 |
outfile_h.Append("-histo.root"); |
151 |
outfh = (TFile*)gROOT->FindObject(outfile_h); if (outfh) outfh->Close(); |
152 |
outfh = new TFile(outfile_h,"RECREATE"); |
153 |
if(outfh->IsZombie()){ |
154 |
cout << "Output file could not be created\n"; |
155 |
return 1; |
156 |
}; |
157 |
cout << "Created output file: "<<outfh->GetName()<<endl; |
158 |
} |
159 |
|
160 |
// -------------------- |
161 |
// read input file/list |
162 |
// -------------------- |
163 |
event = new PamLevel2(); |
164 |
if(DEBUG)gObjectTable->Print(); |
165 |
if(list.Contains(".root")){ |
166 |
stringstream command; |
167 |
command.str(""); |
168 |
command << " echo "<<list<<" > list-temp.txt"; |
169 |
cout << command.str().c_str() << endl; |
170 |
gSystem->Exec(command.str().c_str()); |
171 |
tree = event->GetPamTree(ddir,"list-temp.txt",options); |
172 |
}else{ |
173 |
tree = event->GetPamTree(ddir,list,options); |
174 |
}; |
175 |
// gObjectTable->Print(); |
176 |
tree->SetCacheSize(0); |
177 |
|
178 |
// --------------- |
179 |
// initializations |
180 |
// --------------- |
181 |
if( !Begin() )return 0;; |
182 |
|
183 |
// ----------------- |
184 |
// loop over events |
185 |
// ----------------- |
186 |
ULong64_t nevents = tree->GetEntries(); |
187 |
if(!nmax)nmax = numeric_limits<ULong64_t>::max(); |
188 |
if(nevents < nmax)nmax=nevents; |
189 |
|
190 |
cout << endl<<" Start loop over events: "<< nmax<<endl; |
191 |
Int_t ntrk = 0; |
192 |
Int_t ncls = 0; |
193 |
Int_t sel = 0; |
194 |
Int_t get = 0; |
195 |
TBenchmark *benchmark = new TBenchmark(); |
196 |
benchmark->Start("event-loop"); |
197 |
|
198 |
TString current_file = ""; |
199 |
if(DEBUG)gObjectTable->Print(); |
200 |
for(ULong64_t iev=0; iev<nmax; iev++){ |
201 |
|
202 |
event->Clear(); |
203 |
if( tree->GetEntry(iev) ){ |
204 |
get++; |
205 |
if( Process(iev) ){ |
206 |
sel++; |
207 |
} |
208 |
if(current_file.CompareTo(tree->GetFile()->GetName())){ |
209 |
current_file=tree->GetFile()->GetName(); |
210 |
cout << iev<< " -> "<< current_file << endl; |
211 |
}; |
212 |
}else{ |
213 |
cout << "Chain entry "<<iev<<" -- ERROR --"<<endl; |
214 |
}; |
215 |
if( !(iev%5000) && DEBUG)gObjectTable->Print(); |
216 |
|
217 |
}; |
218 |
benchmark->Show("event-loop"); |
219 |
cout << sel <<" selected events over "<<get; |
220 |
if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl; |
221 |
if(DEBUG)gObjectTable->Print(); |
222 |
event->Clear(); |
223 |
// -------------- |
224 |
// close and exit |
225 |
// -------------- |
226 |
Finish(); |
227 |
|
228 |
return 0; |
229 |
|
230 |
} |
231 |
|
232 |
///////////////////////////////////////////////////////////////// |
233 |
|
234 |
#if !defined(__CINT__) |
235 |
|
236 |
// // input parameters |
237 |
// Bool_t DEBUG; |
238 |
// TString DIR; |
239 |
// TString LIST; |
240 |
// TString OPTIONS; |
241 |
// ULong64_t MAXEV; |
242 |
// TString OUTFILE; |
243 |
|
244 |
void usage(){ |
245 |
|
246 |
cout << "------------------------------------------------------------"<<endl; |
247 |
cout << "Loop over events (on one or more Level2-files), applying some selection cuts (defined in My-Selection.cpp), \n"; |
248 |
cout << "creating output histograms (defined in My-Histos.cpp) and/or trees with selected events. \n \n "; |
249 |
cout << "USAGE:"<<endl; |
250 |
cout << "-processDir DIR - Level2 data directory \n"; |
251 |
cout << "-processList LIST - list of files (.txt) or single file (.root) to be analysed \n"; |
252 |
cout << "-outputFile PATH - name of the output file \n"; |
253 |
cout << "-NumEvents XXX - number of events to be analysed \n"; |
254 |
cout << "--debug, -g - debug mode \n"; |
255 |
cout << "--help, -h - print this help \n"; |
256 |
cout << "-options [ options ] - options: \n"; |
257 |
cout << " fillHistos --> create an output file with histograms \n"; |
258 |
cout << " fillTree --> create an output file with trees storing the selected events \n "; |
259 |
cout << " +(-)ALL --> inlcude(exclude) all trees and branches \n " ; |
260 |
cout << " +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) trees and branches \n" ; |
261 |
cout << "------------------------------------------------------------"<<endl; |
262 |
} |
263 |
// |
264 |
int HandleInputPar(int argc, char **argv){ |
265 |
|
266 |
if(argc>1){ |
267 |
|
268 |
if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") ){ |
269 |
usage(); |
270 |
return(1); |
271 |
}; |
272 |
// ----------------------- |
273 |
// Read input parameters |
274 |
// ----------------------- |
275 |
DEBUG = false; |
276 |
DIR = gSystem->WorkingDirectory(); |
277 |
LIST = ""; |
278 |
OUTFILE = ""; |
279 |
OPTIONS = "+ALL fillTree"; |
280 |
MAXEV = 0; |
281 |
|
282 |
|
283 |
for (int i = 1; i < argc; i++){ |
284 |
// -----------------------------------------------------// |
285 |
if (!strcmp(argv[i], "-processDir")){ |
286 |
if (++i >= argc) throw -1; |
287 |
DIR = argv[i]; |
288 |
cout << "processDir "<<DIR<<endl; |
289 |
continue; |
290 |
} |
291 |
// -----------------------------------------------------// |
292 |
else if (!strcmp(argv[i], "-processList")){ |
293 |
if (++i >= argc) throw -1; |
294 |
LIST = argv[i]; |
295 |
cout << "processList "<<LIST<<endl; |
296 |
continue; |
297 |
} |
298 |
// -----------------------------------------------------// |
299 |
else if (!strcmp(argv[i], "-outputFile")){ |
300 |
if (++i >= argc) throw -1; |
301 |
OUTFILE = argv[i]; |
302 |
cout << "outputFile "<<OUTFILE<<endl; |
303 |
continue; |
304 |
} |
305 |
// -----------------------------------------------------// |
306 |
else if (!strcmp(argv[i], "-NumEvents")){ |
307 |
if (++i >= argc) throw -1; |
308 |
MAXEV = atoi(argv[i]); |
309 |
cout << "NumEvents "<<MAXEV<<endl; |
310 |
continue; |
311 |
} |
312 |
// -----------------------------------------------------// |
313 |
else if (!strcmp(argv[i], "-options")){ |
314 |
if (++i >= argc) throw -1; |
315 |
OPTIONS = argv[i]; |
316 |
if( OPTIONS.Contains("[") ){ |
317 |
do{ |
318 |
if (++i >= argc) throw -1; |
319 |
OPTIONS.Append(argv[i]); |
320 |
}while(!OPTIONS.Contains("]")); |
321 |
}else cout << "wrong option format --> ignoring " << endl; |
322 |
} |
323 |
// -----------------------------------------------------// |
324 |
else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){ |
325 |
DEBUG = true; |
326 |
continue; |
327 |
} |
328 |
// -----------------------------------------------------// |
329 |
else{ |
330 |
cout << "Unidentified input parameter. Ignored."<< endl; |
331 |
}; |
332 |
}; |
333 |
}else{ |
334 |
usage(); |
335 |
return(1); |
336 |
}; |
337 |
// ----------------------- |
338 |
// Check input parameters |
339 |
// ----------------------- |
340 |
|
341 |
|
342 |
return(0); |
343 |
|
344 |
}; |
345 |
// |
346 |
|
347 |
int main(int argc, char **argv) |
348 |
{ |
349 |
|
350 |
if( HandleInputPar(argc,argv) )return(1); |
351 |
|
352 |
// Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE); |
353 |
cout << "OPTIONS "<<OPTIONS<<endl; |
354 |
Loop(DIR,LIST,MAXEV,OPTIONS,OUTFILE); |
355 |
|
356 |
cout << "Back to main - end"<<endl; |
357 |
|
358 |
return 0; |
359 |
|
360 |
}; |
361 |
|
362 |
#endif |