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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide 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 pam-fi 1.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 pam-fi 1.3 #include <TObjectTable.h>
14 pam-fi 1.1
15     #include <stdlib.h>
16     #include <iostream>
17     using namespace std;
18    
19     #endif
20    
21     //===================================
22     // global variables
23     //===================================
24 pam-fi 1.4 Bool_t DEBUG;
25     TString DIR;
26     TString LIST;
27 pam-fi 1.6 TString I_OPTIONS;
28     TString O_OPTIONS;
29     ULong64_t MINEV;
30 pam-fi 1.4 ULong64_t MAXEV;
31     TString OUTFILE;
32 pam-fi 1.6 Bool_t SIMU;
33 pam-fi 1.4
34 pam-fi 1.1 PamLevel2 *event = NULL;
35     TChain *tree = NULL;
36 pam-fi 1.5 TChain *runt = NULL;
37 pam-fi 1.1 TFile *outfh = NULL;
38     TFile *outft = NULL;
39     TTree *otree = NULL;
40 pam-fi 1.5 GL_RUN *run = NULL;
41 pam-fi 1.1
42     bool fillTree = false;
43     bool fillHistos = false;
44    
45     //====================================
46     // Functions to be provided externally
47     //====================================
48     bool Select(PamLevel2*);
49 pam-fi 1.6 //void CreateHistos(PamLevel2*, TFile *);
50 pam-fi 1.5 void CreateHistos(PamLevel2*, TFile *);
51 pam-fi 1.1 void FillHistos(PamLevel2*);
52     void SaveHistos(TFile *);
53    
54     //==========================================
55     //000000000000000000000000000000000000000000
56     //==========================================
57     bool Begin(){
58    
59     //------------------------------
60     // create output histos/trees
61     //------------------------------
62 pam-fi 1.6 //if(fillHistos) CreateHistos(event,outfh);
63     if(fillHistos) CreateHistos(event, outfh);
64 pam-fi 1.5 if(fillTree)event->CreateCloneTrees(outft);
65    
66 pam-fi 1.1 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 pam-fi 1.5 if(fillHistos)FillHistos(event);
79 pam-fi 1.6 if(fillTree)event->FillCloneTrees();//must be after fillhisto
80 pam-fi 1.1
81     return true;
82    
83     }
84     //==========================================
85     //000000000000000000000000000000000000000000
86     //==========================================
87    
88     bool Finish(){
89    
90 pam-fi 1.5 if(fillHistos){
91     SaveHistos(outfh);
92     outfh->Close();
93     }
94 pam-fi 1.1 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 pam-fi 1.6 Int_t Loop(TString ddir,TString list, ULong64_t nmax, ULong64_t nmin, TString ioptions, TString ooptions, TString outfile){
109 pam-fi 1.3
110     // gObjectTable->Print();
111 pam-fi 1.1
112 pam-fi 1.6 // ------------------------
113     // check output options (I)
114     // ------------------------
115     if(ooptions.Contains("fillTree"))fillTree=true;
116     if(ooptions.Contains("fillHisto"))fillHistos=true;
117    
118 pam-fi 1.1 // -------------------
119     // create output files
120     // -------------------
121 pam-fi 1.6 TTree::SetMaxTreeSize(1000*Long64_t(2000000000)); //m 070308 + dimensione oltre la quale divide il file di uscita: 2TB
122    
123 pam-fi 1.1 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 pam-fi 1.6 event->SetGP(SIMU);//new
159     if(SIMU)cout << " >> SIMULATION << "<<event->IsGP()<<endl;
160     event->SetDBConnection();
161 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
162 pam-fi 1.1 if(list.Contains(".root")){
163 pam-fi 1.6 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 pam-fi 1.1 stringstream command;
168     command.str("");
169 pam-fi 1.6 command << " echo "<<list<<" > "<<tempfile;
170 pam-fi 1.1 cout << command.str().c_str() << endl;
171     gSystem->Exec(command.str().c_str());
172 pam-fi 1.6 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 pam-fi 1.1 }else{
178 pam-fi 1.6 tree = event->GetPamTree(ddir,list,ioptions);
179 pam-fi 1.5 runt = event->GetRunTree(ddir,list);
180 pam-fi 1.1 };
181 pam-fi 1.6
182    
183 pam-fi 1.4 // gObjectTable->Print();
184 pam-fi 1.1 tree->SetCacheSize(0);
185    
186 pam-fi 1.6 // -------------------------
187     // check output options (II)
188     // -------------------------
189     if(fillTree)event->SetWhichTrees(ooptions);
190    
191 pam-fi 1.1 // ---------------
192     // initializations
193     // ---------------
194     if( !Begin() )return 0;;
195    
196     // -----------------
197     // loop over events
198     // -----------------
199 pam-fi 1.2 ULong64_t nevents = tree->GetEntries();
200 pam-fi 1.1 if(!nmax)nmax = numeric_limits<ULong64_t>::max();
201     if(nevents < nmax)nmax=nevents;
202 pam-fi 1.6 nmax=nmax-nmin;
203 pam-fi 1.1
204 pam-fi 1.6 cout << endl<<" Start loop over events: "<< nmax <<endl;
205 pam-fi 1.2 Int_t ntrk = 0;
206 pam-fi 1.4 Int_t ncls = 0;
207 pam-fi 1.2 Int_t sel = 0;
208     Int_t get = 0;
209 pam-fi 1.1 TBenchmark *benchmark = new TBenchmark();
210     benchmark->Start("event-loop");
211    
212     TString current_file = "";
213 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
214 pam-fi 1.6 for(ULong64_t iev=nmin; iev<nmin+nmax; iev++){
215 pam-fi 1.1
216 pam-fi 1.2 event->Clear();
217 pam-fi 1.5 // if( tree->GetEntry(iev) ){
218     if( event->GetEntry(iev) ){ //<<< new feature
219 pam-fi 1.2 get++;
220     if( Process(iev) ){
221 pam-fi 1.6 // cout << "%%%% "<<iev << endl;
222 pam-fi 1.2 sel++;
223     }
224     if(current_file.CompareTo(tree->GetFile()->GetName())){
225     current_file=tree->GetFile()->GetName();
226 pam-fi 1.6 cout <<endl<<"@ entry "<< iev<< " -> "<< current_file << endl;
227 pam-fi 1.2 };
228     }else{
229     cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
230 pam-fi 1.1 };
231 pam-fi 1.4 if( !(iev%5000) && DEBUG)gObjectTable->Print();
232 pam-fi 1.5
233     if( !(iev%1000) )cout <<"|";
234 pam-fi 1.4
235 pam-fi 1.1 };
236 pam-fi 1.5 cout <<endl;
237 pam-fi 1.1 benchmark->Show("event-loop");
238 pam-fi 1.2 cout << sel <<" selected events over "<<get;
239     if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
240 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
241     event->Clear();
242 pam-fi 1.1 // --------------
243     // close and exit
244     // --------------
245     Finish();
246    
247     return 0;
248    
249     }
250    
251     /////////////////////////////////////////////////////////////////
252    
253 pam-fi 1.2 #if !defined(__CINT__)
254 pam-fi 1.1
255 pam-fi 1.4 // // input parameters
256     // Bool_t DEBUG;
257     // TString DIR;
258     // TString LIST;
259     // TString OPTIONS;
260     // ULong64_t MAXEV;
261     // TString OUTFILE;
262 pam-fi 1.1
263     void usage(){
264 pam-fi 1.2
265     cout << "------------------------------------------------------------"<<endl;
266 pam-fi 1.6 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 pam-fi 1.1 cout << "USAGE:"<<endl;
269 pam-fi 1.2 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 pam-fi 1.6 cout << "-FirstEvent XXX - first event to be analysed \n";
274 pam-fi 1.1 cout << "--debug, -g - debug mode \n";
275     cout << "--help, -h - print this help \n";
276 pam-fi 1.6 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 pam-fi 1.2 cout << " fillHistos --> create an output file with histograms \n";
282     cout << " fillTree --> create an output file with trees storing the selected events \n ";
283 pam-fi 1.6 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 pam-fi 1.2 cout << " +(-)ALL --> inlcude(exclude) all trees and branches \n " ;
287 pam-fi 1.6 cout << " +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) trees and branches \n" ;
288     cout << "--simulation, -s - simulation file \n";
289 pam-fi 1.2 cout << "------------------------------------------------------------"<<endl;
290 pam-fi 1.1 }
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 pam-fi 1.2 DEBUG = false;
304     DIR = gSystem->WorkingDirectory();
305     LIST = "";
306     OUTFILE = "";
307 pam-fi 1.6 I_OPTIONS = "+ALL ";
308     O_OPTIONS = "fillTree";
309 pam-fi 1.2 MAXEV = 0;
310 pam-fi 1.6 MINEV = 0;
311     SIMU = false;
312 pam-fi 1.1
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 pam-fi 1.6 // -----------------------------------------------------//
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 pam-fi 1.1 // -----------------------------------------------------//
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 pam-fi 1.6 else if (!strcmp(argv[i], "-input-options")){
352 pam-fi 1.2 if (++i >= argc) throw -1;
353 pam-fi 1.6 I_OPTIONS = argv[i];
354     if( I_OPTIONS.Contains("[") ){
355 pam-fi 1.2 do{
356     if (++i >= argc) throw -1;
357 pam-fi 1.6 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 pam-fi 1.2 }else cout << "wrong option format --> ignoring " << endl;
371     }
372     // -----------------------------------------------------//
373 pam-fi 1.1 else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){
374     DEBUG = true;
375     continue;
376     }
377     // -----------------------------------------------------//
378 pam-fi 1.6 else if (!strcmp(argv[i], "--simulation") || !strcmp(argv[i], "-s")){
379     SIMU = true;
380     continue;
381     }
382     // -----------------------------------------------------//
383 pam-fi 1.1 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 pam-fi 1.2 // Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE);
407 pam-fi 1.6 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 pam-fi 1.1
411     cout << "Back to main - end"<<endl;
412    
413     return 0;
414    
415     };
416    
417     #endif

  ViewVC Help
Powered by ViewVC 1.1.23