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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Fri Aug 17 16:57:11 2007 UTC (17 years, 4 months ago) by pam-fi
Branch: MAIN
CVS Tags: v5r00, v4r00, v10RED, v6r00, v9r00, HEAD
Changes since 1.5: +91 -42 lines
modified to work also with simulation

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

  ViewVC Help
Powered by ViewVC 1.1.23