/[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.1 - (hide annotations) (download)
Wed Dec 6 11:07:35 2006 UTC (17 years, 11 months ago) by pam-fi
Branch: MAIN
*** empty log message ***

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    
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();
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();
61     if(fillTree) event->CreateCloneTrees(tree);
62    
63     cout << "\nBegin() - done\n\n";
64     return 1;
65    
66     }
67     //==========================================
68     //000000000000000000000000000000000000000000
69     //==========================================
70    
71     bool Process(int iev){
72    
73     //------------------------------
74     // create output histos/trees
75     //------------------------------
76    
77     event->Clear();
78     tree->GetEntry(iev);
79    
80     if( !Select(event) )return false;
81     // cout <<event->TrkLevel2::ntrk()<<endl;
82    
83    
84     // if(fillTree)event->FillCloneTrees();
85     // if(fillHistos)FillHistos(event);
86    
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    
116     if(options.Contains("fillTree"))fillTree=true;
117     if(options.Contains("fillHisto"))fillHistos=true;
118    
119     // -------------------
120     // create output files
121     // -------------------
122     TString outfile_t =0;
123     TString outfile_h =0;
124     if( outfile.IsNull() ){
125     if(!list.IsNull())outfile = list(0,list.Last('.'));
126     else outfile = "output";
127     }else{
128     if(outfile.Contains(".root"))outfile = outfile(0,outfile.Last('.'));
129     }
130     if(fillTree){
131     outfile_t = outfile;
132     outfile_t.Append("-tree.root");
133     outft = (TFile*)gROOT->FindObject(outfile_t); if (outft) outft->Close();
134     outft = new TFile(outfile_t,"RECREATE");
135     if(outft->IsZombie()){
136     cout << "Output file could not be created\n";
137     return 1;
138     };
139     cout << "Created output file: "<<outft->GetName()<<endl;
140     }
141     if(fillHistos){
142     outfile_h = outfile;
143     outfile_h.Append("-histo.root");
144     outfh = (TFile*)gROOT->FindObject(outfile_h); if (outfh) outfh->Close();
145     outfh = new TFile(outfile_h,"RECREATE");
146     if(outfh->IsZombie()){
147     cout << "Output file could not be created\n";
148     return 1;
149     };
150     cout << "Created output file: "<<outfh->GetName()<<endl;
151     }
152    
153     // --------------------
154     // read input file/list
155     // --------------------
156     event = new PamLevel2();
157     if(list.Contains(".root")){
158     stringstream command;
159     command.str("");
160     command << " echo "<<list<<" > list-temp.txt";
161     cout << command.str().c_str() << endl;
162     gSystem->Exec(command.str().c_str());
163     tree = event->GetPamTree(event->GetListOfLevel2Files(ddir,"list-temp.txt"),options);
164     }else{
165     tree = event->GetPamTree(event->GetListOfLevel2Files(ddir,list),options);
166     };
167     tree->SetCacheSize(0);
168    
169     // ---------------
170     // initializations
171     // ---------------
172     if( !Begin() )return 0;;
173    
174     // -----------------
175     // loop over events
176     // -----------------
177     ULong64_t nevents = tree->GetEntriesFast();
178     if(!nmax)nmax = numeric_limits<ULong64_t>::max();
179     if(nevents < nmax)nmax=nevents;
180    
181     cout << endl<<" Start loop over events: "<< nmax<<endl;
182     Int_t ntrk=0;
183     Int_t sel=0;
184     TBenchmark *benchmark = new TBenchmark();
185     benchmark->Start("event-loop");
186    
187     TString current_file = "";
188     for(ULong64_t iev=60000; iev<nmax; iev++){
189    
190     // tree->GetEntry(iev);
191     if( Process(iev) )sel++;
192    
193     if(current_file.CompareTo(tree->GetFile()->GetName())){
194     current_file=tree->GetFile()->GetName();
195     cout << iev<< " -> "<< current_file << endl;
196     // cout << tree->GetFile()->GetName() << endl;
197     };
198    
199     if(event->TrkLevel2::ntrk() >0)ntrk++;
200     if(!(iev%100))cout << iev << endl;
201    
202     };
203     benchmark->Show("event-loop");
204     cout << ntrk <<" tracks over "<<nmax<<" events ";
205     if(nmax)cout<<"("<< 100*ntrk/nmax<<"%)"<<endl;
206     cout << "Selected events: "<<sel<<endl;
207    
208     // --------------
209     // close and exit
210     // --------------
211     Finish();
212    
213     return 0;
214    
215     }
216    
217     /////////////////////////////////////////////////////////////////
218    
219     #if !defined(__CINT__)
220    
221     // input parameters
222     Bool_t DEBUG;
223     TString DIR;
224     TString LIST;
225     ULong64_t MAXEV;
226     TString OUTFILE;
227    
228     void usage(){
229     cout << "USAGE:"<<endl;
230     cout << "-processDir [DIR] - Level2/Level1 data directory \n";
231     cout << "-processList [LIST] - list (txt) of names of files to be analysed \n";
232     cout << "-outputFile [PATH] - name of the output file \n";
233     cout << "-NumEvents [XXX] - number of events to be analysed \n";
234     cout << "--debug, -g - debug mode \n";
235     cout << "--help, -h - print this help \n";
236     }
237     //
238     int HandleInputPar(int argc, char **argv){
239    
240     if(argc>1){
241    
242     if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") ){
243     usage();
244     return(1);
245     };
246     // -----------------------
247     // Read input parameters
248     // -----------------------
249     /* DEBUG = false;
250     DIR = gSystem->WorkingDirectory();
251     LIST = "";
252     OUTFILE = "TrkLevel1Report_output.root";
253     MAXEV = numeric_limits<ULong64_t>::max();
254     */
255    
256    
257     for (int i = 1; i < argc; i++){
258     // -----------------------------------------------------//
259     if (!strcmp(argv[i], "-processDir")){
260     if (++i >= argc) throw -1;
261     DIR = argv[i];
262     cout << "processDir "<<DIR<<endl;
263     continue;
264     }
265     // -----------------------------------------------------//
266     else if (!strcmp(argv[i], "-processList")){
267     if (++i >= argc) throw -1;
268     LIST = argv[i];
269     cout << "processList "<<LIST<<endl;
270     continue;
271     }
272     // -----------------------------------------------------//
273     else if (!strcmp(argv[i], "-outputFile")){
274     if (++i >= argc) throw -1;
275     OUTFILE = argv[i];
276     cout << "outputFile "<<OUTFILE<<endl;
277     continue;
278     }
279     // -----------------------------------------------------//
280     else if (!strcmp(argv[i], "-NumEvents")){
281     if (++i >= argc) throw -1;
282     MAXEV = atoi(argv[i]);
283     cout << "NumEvents "<<MAXEV<<endl;
284     continue;
285     }
286     // -----------------------------------------------------//
287     else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){
288     DEBUG = true;
289     continue;
290     }
291     // -----------------------------------------------------//
292     else{
293     cout << "Unidentified input parameter. Ignored."<< endl;
294     };
295     };
296     }else{
297     usage();
298     return(1);
299     };
300     // -----------------------
301     // Check input parameters
302     // -----------------------
303    
304    
305     return(0);
306    
307     };
308     //
309    
310     int main(int argc, char **argv)
311     {
312    
313     if( HandleInputPar(argc,argv) )return(1);
314    
315     Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC fillHisto fillTree",OUTFILE);
316    
317     cout << "Back to main - end"<<endl;
318    
319     return 0;
320    
321     };
322    
323     #endif

  ViewVC Help
Powered by ViewVC 1.1.23