/[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.5 - (hide 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 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     TString OPTIONS;
28     ULong64_t MAXEV;
29     TString OUTFILE;
30    
31 pam-fi 1.1 PamLevel2 *event = NULL;
32     TChain *tree = NULL;
33 pam-fi 1.5 TChain *runt = NULL;
34 pam-fi 1.1 TFile *outfh = NULL;
35     TFile *outft = NULL;
36     TTree *otree = NULL;
37 pam-fi 1.5 GL_RUN *run = NULL;
38 pam-fi 1.1
39     bool fillTree = false;
40     bool fillHistos = false;
41    
42     //====================================
43     // Functions to be provided externally
44     //====================================
45     bool Select(PamLevel2*);
46 pam-fi 1.5 void CreateHistos(PamLevel2*, TFile *);
47 pam-fi 1.1 void FillHistos(PamLevel2*);
48     void SaveHistos(TFile *);
49    
50     //==========================================
51     //000000000000000000000000000000000000000000
52     //==========================================
53     bool Begin(){
54    
55 pam-fi 1.5 // //------------------------
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 pam-fi 1.1
67     //------------------------------
68     // create output histos/trees
69     //------------------------------
70 pam-fi 1.5 if(fillHistos) CreateHistos(event,outfh);
71     if(fillTree)event->CreateCloneTrees(outft);
72    
73 pam-fi 1.2 // gDirectory->ls();
74 pam-fi 1.1
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 pam-fi 1.5 if(fillHistos)FillHistos(event);
89 pam-fi 1.2 if(fillTree)event->FillCloneTrees();
90 pam-fi 1.1
91     return true;
92    
93     }
94     //==========================================
95     //000000000000000000000000000000000000000000
96     //==========================================
97    
98     bool Finish(){
99    
100 pam-fi 1.5 if(fillHistos){
101     SaveHistos(outfh);
102     outfh->Close();
103     }
104 pam-fi 1.1 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 pam-fi 1.3
120     // gObjectTable->Print();
121 pam-fi 1.1
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 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
164 pam-fi 1.1 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 pam-fi 1.2 tree = event->GetPamTree(ddir,"list-temp.txt",options);
171 pam-fi 1.5 runt = event->GetRunTree(ddir,"list-temp.txt");
172 pam-fi 1.1 }else{
173 pam-fi 1.2 tree = event->GetPamTree(ddir,list,options);
174 pam-fi 1.5 runt = event->GetRunTree(ddir,list);
175 pam-fi 1.1 };
176 pam-fi 1.4 // gObjectTable->Print();
177 pam-fi 1.1 tree->SetCacheSize(0);
178    
179     // ---------------
180     // initializations
181     // ---------------
182     if( !Begin() )return 0;;
183    
184     // -----------------
185     // loop over events
186     // -----------------
187 pam-fi 1.2 ULong64_t nevents = tree->GetEntries();
188 pam-fi 1.1 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 pam-fi 1.2 Int_t ntrk = 0;
193 pam-fi 1.4 Int_t ncls = 0;
194 pam-fi 1.2 Int_t sel = 0;
195     Int_t get = 0;
196 pam-fi 1.1 TBenchmark *benchmark = new TBenchmark();
197     benchmark->Start("event-loop");
198    
199     TString current_file = "";
200 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
201 pam-fi 1.2 for(ULong64_t iev=0; iev<nmax; iev++){
202 pam-fi 1.1
203 pam-fi 1.2 event->Clear();
204 pam-fi 1.5 // if( tree->GetEntry(iev) ){
205     if( event->GetEntry(iev) ){ //<<< new feature
206 pam-fi 1.2 get++;
207     if( Process(iev) ){
208 pam-fi 1.5 cout << "%%%% "<<iev << endl;
209 pam-fi 1.2 sel++;
210     }
211     if(current_file.CompareTo(tree->GetFile()->GetName())){
212     current_file=tree->GetFile()->GetName();
213 pam-fi 1.5 cout <<endl<< iev<< " -> "<< current_file << endl;
214 pam-fi 1.2 };
215     }else{
216     cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
217 pam-fi 1.1 };
218 pam-fi 1.4 if( !(iev%5000) && DEBUG)gObjectTable->Print();
219 pam-fi 1.5
220     if( !(iev%1000) )cout <<"|";
221 pam-fi 1.4
222 pam-fi 1.1 };
223 pam-fi 1.5 cout <<endl;
224 pam-fi 1.1 benchmark->Show("event-loop");
225 pam-fi 1.2 cout << sel <<" selected events over "<<get;
226     if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
227 pam-fi 1.4 if(DEBUG)gObjectTable->Print();
228     event->Clear();
229 pam-fi 1.1 // --------------
230     // close and exit
231     // --------------
232     Finish();
233    
234     return 0;
235    
236     }
237    
238     /////////////////////////////////////////////////////////////////
239    
240 pam-fi 1.2 #if !defined(__CINT__)
241 pam-fi 1.1
242 pam-fi 1.4 // // input parameters
243     // Bool_t DEBUG;
244     // TString DIR;
245     // TString LIST;
246     // TString OPTIONS;
247     // ULong64_t MAXEV;
248     // TString OUTFILE;
249 pam-fi 1.1
250     void usage(){
251 pam-fi 1.2
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 pam-fi 1.1 cout << "USAGE:"<<endl;
256 pam-fi 1.2 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 pam-fi 1.1 cout << "--debug, -g - debug mode \n";
261     cout << "--help, -h - print this help \n";
262 pam-fi 1.2 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 pam-fi 1.1 }
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 pam-fi 1.2 DEBUG = false;
282     DIR = gSystem->WorkingDirectory();
283     LIST = "";
284     OUTFILE = "";
285     OPTIONS = "+ALL fillTree";
286     MAXEV = 0;
287 pam-fi 1.1
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 pam-fi 1.2 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 pam-fi 1.1 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 pam-fi 1.2 // 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 pam-fi 1.1
362     cout << "Back to main - end"<<endl;
363    
364     return 0;
365    
366     };
367    
368     #endif

  ViewVC Help
Powered by ViewVC 1.1.23