/[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.2 - (show annotations) (download)
Mon Dec 11 18:29:01 2006 UTC (18 years ago) by pam-fi
Branch: MAIN
Changes since 1.1: +73 -42 lines
*** empty log message ***

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

  ViewVC Help
Powered by ViewVC 1.1.23