/[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.3 - (hide annotations) (download)
Wed Jan 3 13:28:49 2007 UTC (17 years, 11 months ago) by pam-fi
Branch: MAIN
CVS Tags: v0r02, v2r02
Changes since 1.2: +6 -2 lines
new example with selection cuts

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

  ViewVC Help
Powered by ViewVC 1.1.23