/[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.5 - (show annotations) (download)
Fri Feb 16 18:38:13 2007 UTC (17 years, 9 months ago) by pam-fi
Branch: MAIN
CVS Tags: v3r03
Changes since 1.4: +31 -25 lines
new features in Loop.cpp

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

  ViewVC Help
Powered by ViewVC 1.1.23