/[PAMELA software]/PamelaLevel2/doc/examples-1/Loop.cpp
ViewVC logotype

Contents of /PamelaLevel2/doc/examples-1/Loop.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Mon Nov 5 13:10:12 2007 UTC (17 years, 1 month ago) by pam-fi
Branch: MAIN
CVS Tags: v10RED, v5r00, v6r00, v9r00, HEAD
implemente gpamela and new examples

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 I_OPTIONS;
28 TString O_OPTIONS;
29 ULong64_t MINEV;
30 ULong64_t MAXEV;
31 TString OUTFILE;
32 Bool_t SIMU;
33
34 PamLevel2 *event = NULL;
35 TChain *tree = NULL;
36 TChain *runt = NULL;
37 TFile *outfh = NULL;
38 TFile *outft = NULL;
39 TTree *otree = NULL;
40 GL_RUN *run = NULL;
41
42 bool fillTree = false;
43 bool fillHistos = false;
44
45 //====================================
46 // Functions to be provided externally
47 //====================================
48 bool Select(PamLevel2*);
49 //void CreateHistos(PamLevel2*, TFile *);
50 void CreateHistos(PamLevel2*, TFile *);
51 void FillHistos(PamLevel2*);
52 void SaveHistos(TFile *);
53
54 //==========================================
55 //000000000000000000000000000000000000000000
56 //==========================================
57 bool Begin(){
58
59 //------------------------------
60 // create output histos/trees
61 //------------------------------
62 //if(fillHistos) CreateHistos(event,outfh);
63 if(fillHistos || fillTree) CreateHistos(event, outfh);
64 if(fillTree){
65 event->CreateCloneTrees(outft);
66 // CreateHistos(event, outft);
67 }
68 cout << "\nBegin() - done\n\n";
69 return 1;
70
71 }
72 //==========================================
73 //000000000000000000000000000000000000000000
74 //==========================================
75
76 bool Process(int iev){
77
78
79 if( !Select(event) )return false;
80 // if(fillHistos)FillHistos(event);
81 if(fillHistos || fillTree) FillHistos(event);
82 if(fillTree)event->FillCloneTrees();//must be after fillhisto
83
84 return true;
85
86 }
87 //==========================================
88 //000000000000000000000000000000000000000000
89 //==========================================
90
91 bool Finish(){
92
93 if(fillHistos || fillTree){
94 SaveHistos(outfh);
95 outfh->Close();
96 }
97 if(fillTree){
98 outft->cd();
99 event->WriteCloneTrees();
100 // SaveHistos(outft);
101 outft->Close();
102 }
103
104 cout << "Finish() - done\n";
105
106 return 1;
107 }
108
109 //==========================================
110 //000000000000000000000000000000000000000000
111 //==========================================
112 Int_t Loop(TString ddir,TString list, ULong64_t nmax, ULong64_t nmin, TString ioptions, TString ooptions, TString outfile){
113
114 // gObjectTable->Print();
115
116 // ------------------------
117 // check output options (I)
118 // ------------------------
119 if(ooptions.Contains("fillTree"))fillTree=true;
120 if(ooptions.Contains("fillHisto"))fillHistos=true;
121
122 // -------------------
123 // create output files
124 // -------------------
125 TTree::SetMaxTreeSize(1000*Long64_t(2000000000)); //m 070308 + dimensione oltre la quale divide il file di uscita: 2TB
126
127 TString outfile_t =0;
128 TString outfile_h =0;
129 if( outfile.IsNull() ){
130 if(!list.IsNull())outfile = list(0,list.Last('.'));
131 else outfile = "output";
132 }else{
133 if(outfile.Contains(".root"))outfile = outfile(0,outfile.Last('.'));
134 }
135 if(fillTree){
136 outfile_t = outfile;
137 outfile_t.Append("-tree.root");
138 outft = (TFile*)gROOT->FindObject(outfile_t); if (outft) outft->Close();
139 outft = new TFile(outfile_t,"RECREATE");
140 if(outft->IsZombie()){
141 cout << "Output file could not be created\n";
142 return 1;
143 };
144 cout << "Created output file: "<<outft->GetName()<<endl;
145 }
146 if(fillHistos){
147 outfile_h = outfile;
148 outfile_h.Append("-histo.root");
149 outfh = (TFile*)gROOT->FindObject(outfile_h); if (outfh) outfh->Close();
150 outfh = new TFile(outfile_h,"RECREATE");
151 if(outfh->IsZombie()){
152 cout << "Output file could not be created\n";
153 return 1;
154 };
155 cout << "Created output file: "<<outfh->GetName()<<endl;
156 }
157
158 // --------------------
159 // read input file/list
160 // --------------------
161 event = new PamLevel2();
162 event->SetGP(SIMU);//new
163 event->SetDBConnection();
164 if(DEBUG)gObjectTable->Print();
165 if(list.Contains(".root")){
166 TString tempfile = gSystem->BaseName(list.Data());
167 // tempfile.Replace(tempfile.Index(".root",5),5,".txt",4);
168 tempfile.Append(".txt");
169 cout << "Creating temporary file: "<<tempfile<<endl;
170 stringstream command;
171 command.str("");
172 command << " echo "<<list<<" > "<<tempfile;
173 cout << command.str().c_str() << endl;
174 gSystem->Exec(command.str().c_str());
175 tree = event->GetPamTree(ddir,tempfile.Data(),ioptions);
176 runt = event->GetRunTree(ddir,tempfile.Data());
177 command.str("");
178 command << "rm -f "<<tempfile;
179 gSystem->Exec(command.str().c_str());
180 }else{
181 tree = event->GetPamTree(ddir,list,ioptions);
182 runt = event->GetRunTree(ddir,list);
183 };
184
185
186 // gObjectTable->Print();
187 tree->SetCacheSize(0);
188
189 // -------------------------
190 // check output options (II)
191 // -------------------------
192 if(fillTree)event->SetWhichTrees(ooptions);
193
194 // ---------------
195 // initializations
196 // ---------------
197 if( !Begin() )return 0;;
198
199 // -----------------
200 // loop over events
201 // -----------------
202 ULong64_t nevents = tree->GetEntries();
203 if(!nmax)nmax = numeric_limits<ULong64_t>::max();
204 if(nevents < nmax)nmax=nevents;
205 nmax=nmax-nmin;
206
207 cout << endl<<" Start loop over events: "<< nmax <<endl;
208 Int_t ntrk = 0;
209 Int_t ncls = 0;
210 Int_t sel = 0;
211 Int_t get = 0;
212 TBenchmark *benchmark = new TBenchmark();
213 benchmark->Start("event-loop");
214
215 TString current_file = "";
216 if(DEBUG)gObjectTable->Print();
217 for(ULong64_t iev=nmin; iev<nmin+nmax; iev++){
218
219 event->Clear();
220 // if( tree->GetEntry(iev) ){
221 if( event->GetEntry(iev) ){ //<<< new feature
222 get++;
223 if( Process(iev) ){
224 // cout << "%%%% "<<iev << endl;
225 sel++;
226 }
227 if(current_file.CompareTo(tree->GetFile()->GetName())){
228 current_file=tree->GetFile()->GetName();
229 cout <<endl<<"@ entry "<< iev<< " -> "<< current_file << endl;
230 };
231 }else{
232 cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
233 };
234 if( !(iev%5000) && DEBUG)gObjectTable->Print();
235
236 if( !(iev%1000) )cout <<"|";
237
238 };
239 cout <<endl;
240 benchmark->Show("event-loop");
241 cout << sel <<" selected events over "<<get;
242 if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
243 if(DEBUG)gObjectTable->Print();
244 event->Clear();
245 // --------------
246 // close and exit
247 // --------------
248 Finish();
249
250 return 0;
251
252 }
253
254 /////////////////////////////////////////////////////////////////
255
256 #if !defined(__CINT__)
257
258 // // input parameters
259 // Bool_t DEBUG;
260 // TString DIR;
261 // TString LIST;
262 // TString OPTIONS;
263 // ULong64_t MAXEV;
264 // TString OUTFILE;
265
266 void usage(){
267
268 cout << "------------------------------------------------------------"<<endl;
269 cout << "Loop over events (on one or more Level2-files), applying some selection cuts, \n";
270 cout << "creating output histograms and/or trees with selected events. \n \n ";
271 cout << "USAGE:"<<endl;
272 cout << "-processDir DIR - Level2 data directory \n";
273 cout << "-processList LIST - list of files (.txt) or single file (.root) to be analysed \n";
274 cout << "-outputFile PATH - name of the output file \n";
275 cout << "-NumEvents XXX - number of events to be analysed \n";
276 cout << "-FirstEvent XXX - first event to be analysed \n";
277 cout << "--debug, -g - debug mode \n";
278 cout << "--help, -h - print this help \n";
279 cout << "-input-options [ options ] - options: \n";
280 cout << " +AUTO --> inlcude all trees and branches found in the input file \n " ;
281 cout << " +(-)ALL --> inlcude(exclude) all level0/level1/level2 trees and branches \n " ;
282 cout << " +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB --> inlcude(exclude) level1/level2 trees and branches \n" ;
283 cout << " +TRK0 +CAL0 +TOF0 --> include level0 branches \n" ;
284 cout << " +(-)GP --> include(exclude) GP tree \n" ;
285 cout << " == NB == "<<endl;
286 cout << " FILE IS DISCARDED IF REQUIRED BRANCH/TREE IS MISSING \n" ;
287 cout << "-output-options [ options ] - options: \n";
288 cout << " fillHistos --> create an output file with histograms \n";
289 cout << " fillTree --> create an output file with trees storing the selected events \n ";
290 cout << " == NB == "<<endl;
291 cout << " output level1/level2 tree is created with the same trees/branches as read from input file."<<endl;
292 cout << " in order to modify the output structure use the following options:"<<endl;
293 cout << " +(-)ALL --> inlcude(exclude) all trees and branches \n " ;
294 cout << " +(-)TRK1+(-)TRK2+(-)CAL1+(-)CAL2+(-)TOF+(-)TRG+(-)ND+(-)S4+(-)ORB --> inlcude(exclude) trees and branches \n" ;
295 cout << " +(-)GP --> include(exclude) GP tree \n" ;
296 // cout << "--simulation, -s - simulation file \n";
297 cout << "------------------------------------------------------------"<<endl;
298 }
299 //
300 int HandleInputPar(int argc, char **argv){
301
302 if(argc>1){
303
304 if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") ){
305 usage();
306 return(1);
307 };
308 // -----------------------
309 // Read input parameters
310 // -----------------------
311 DEBUG = false;
312 DIR = gSystem->WorkingDirectory();
313 LIST = "";
314 OUTFILE = "";
315 I_OPTIONS = "+ALL ";
316 O_OPTIONS = "fillTree";
317 MAXEV = 0;
318 MINEV = 0;
319 SIMU = false;
320
321
322 for (int i = 1; i < argc; i++){
323 // -----------------------------------------------------//
324 if (!strcmp(argv[i], "-processDir")){
325 if (++i >= argc) throw -1;
326 DIR = argv[i];
327 cout << "processDir "<<DIR<<endl;
328 continue;
329 }
330 // -----------------------------------------------------//
331 else if (!strcmp(argv[i], "-processList")){
332 if (++i >= argc) throw -1;
333 LIST = argv[i];
334 cout << "processList "<<LIST<<endl;
335 continue;
336 }
337 // -----------------------------------------------------//
338 else if (!strcmp(argv[i], "-outputFile")){
339 if (++i >= argc) throw -1;
340 OUTFILE = argv[i];
341 cout << "outputFile "<<OUTFILE<<endl;
342 continue;
343 }
344 // -----------------------------------------------------//
345 else if (!strcmp(argv[i], "-FirstEvent")){
346 if (++i >= argc) throw -1;
347 MINEV = atoi(argv[i]);
348 cout << "FirstEvent "<<MINEV<<endl;
349 continue;
350 }
351 // -----------------------------------------------------//
352 else if (!strcmp(argv[i], "-NumEvents")){
353 if (++i >= argc) throw -1;
354 MAXEV = atoi(argv[i]);
355 cout << "NumEvents "<<MAXEV<<endl;
356 continue;
357 }
358 // -----------------------------------------------------//
359 else if (!strcmp(argv[i], "-input-options")){
360 if (++i >= argc) throw -1;
361 I_OPTIONS = argv[i];
362 if( I_OPTIONS.Contains("[") ){
363 do{
364 if (++i >= argc) throw -1;
365 I_OPTIONS.Append(argv[i]);
366 }while(!I_OPTIONS.Contains("]"));
367 }else cout << "wrong option format --> ignoring " << endl;
368 }
369 // -----------------------------------------------------//
370 else if (!strcmp(argv[i], "-output-options")){
371 if (++i >= argc) throw -1;
372 O_OPTIONS = argv[i];
373 if( O_OPTIONS.Contains("[") ){
374 do{
375 if (++i >= argc) throw -1;
376 O_OPTIONS.Append(argv[i]);
377 }while(!O_OPTIONS.Contains("]"));
378 }else cout << "wrong option format --> ignoring " << endl;
379 }
380 // -----------------------------------------------------//
381 else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){
382 DEBUG = true;
383 continue;
384 }
385 // -----------------------------------------------------//
386 else if (!strcmp(argv[i], "--simulation") || !strcmp(argv[i], "-s")){
387 SIMU = true;
388 continue;
389 }
390 // -----------------------------------------------------//
391 else{
392 cout << "Unidentified input parameter. Ignored."<< endl;
393 };
394 };
395 }else{
396 usage();
397 return(1);
398 };
399 // -----------------------
400 // Check input parameters
401 // -----------------------
402
403
404 return(0);
405
406 };
407 //
408
409 int main(int argc, char **argv)
410 {
411
412 if( HandleInputPar(argc,argv) )return(1);
413
414 // Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE);
415 cout << "INPUT OPTIONS "<<I_OPTIONS<<endl;
416 cout << "OUTPUT OPTIONS "<<O_OPTIONS<<endl;
417 Loop(DIR,LIST,MAXEV,MINEV,I_OPTIONS,O_OPTIONS,OUTFILE);
418
419 cout << "Back to main - end"<<endl;
420
421 return 0;
422
423 };
424
425 #endif

  ViewVC Help
Powered by ViewVC 1.1.23