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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by pam-fi, Wed Dec 6 11:07:35 2006 UTC revision 1.4 by pam-fi, Mon Jan 15 11:51:39 2007 UTC
# Line 10  Line 10 
10  #include <TString.h>  #include <TString.h>
11  #include <TBenchmark.h>  #include <TBenchmark.h>
12  #include <TFriendElement.h>  #include <TFriendElement.h>
13    #include <TObjectTable.h>
14    
15  #include <stdlib.h>  #include <stdlib.h>
16  #include <iostream>  #include <iostream>
# Line 20  using namespace std; Line 21  using namespace std;
21  //===================================  //===================================
22  // global variables  // global variables
23  //===================================  //===================================
24    Bool_t    DEBUG;
25    TString   DIR;
26    TString   LIST;
27    TString   OPTIONS;
28    ULong64_t MAXEV;
29    TString   OUTFILE;
30    
31  PamLevel2 *event = NULL;  PamLevel2 *event = NULL;
32  TChain    *tree  = NULL;  TChain    *tree  = NULL;
33  TFile     *outfh = NULL;  TFile     *outfh = NULL;
# Line 33  bool fillHistos = false; Line 41  bool fillHistos = false;
41  // Functions to be provided externally  // Functions to be provided externally
42  //====================================  //====================================
43  bool Select(PamLevel2*);  bool Select(PamLevel2*);
44  void CreateHistos();  void CreateHistos(TFile *);
45  void FillHistos(PamLevel2*);  void FillHistos(PamLevel2*);
46  void SaveHistos(TFile *);  void SaveHistos(TFile *);
47    
# Line 51  bool Begin(){ Line 59  bool Begin(){
59          return 0;          return 0;
60      }      }
61      fieldpath.Append("/trk-param/field_param-0/");      fieldpath.Append("/trk-param/field_param-0/");
62      event->LoadField(fieldpath.Data());      event->GetTrkLevel2()->LoadField(fieldpath.Data());
63    
64    
65      //------------------------------      //------------------------------
66      //  create output histos/trees      //  create output histos/trees
67      //------------------------------      //------------------------------
68      if(fillHistos) CreateHistos();      if(fillHistos) CreateHistos(outfh);
69      if(fillTree)   event->CreateCloneTrees(tree);      if(fillTree){
70    //      outft->cd();
71    //      event->CreateCloneTrees(tree,outft);
72            event->CreateCloneTrees(outft);
73        }
74    //    gDirectory->ls();
75    
76      cout << "\nBegin() - done\n\n";      cout << "\nBegin() - done\n\n";
77      return 1;      return 1;
# Line 70  bool Begin(){ Line 83  bool Begin(){
83    
84  bool Process(int iev){  bool Process(int iev){
85    
     //------------------------------  
     //  create output histos/trees  
     //------------------------------  
   
     event->Clear();  
     tree->GetEntry(iev);  
86            
87      if( !Select(event) )return false;      if( !Select(event) )return false;
 //    cout <<event->TrkLevel2::ntrk()<<endl;  
   
88    
89  //    if(fillTree)event->FillCloneTrees();      if(fillTree)event->FillCloneTrees();
90  //    if(fillHistos)FillHistos(event);      if(fillHistos)FillHistos(event);
91    
92      return true;      return true;
93    
# Line 112  bool Finish(){ Line 117  bool Finish(){
117  //000000000000000000000000000000000000000000  //000000000000000000000000000000000000000000
118  //==========================================  //==========================================
119  Int_t Loop(TString ddir,TString list, ULong64_t nmax, TString options, TString outfile){  Int_t Loop(TString ddir,TString list, ULong64_t nmax, TString options, TString outfile){
120    
121    //    gObjectTable->Print();
122                    
123      if(options.Contains("fillTree"))fillTree=true;      if(options.Contains("fillTree"))fillTree=true;
124      if(options.Contains("fillHisto"))fillHistos=true;      if(options.Contains("fillHisto"))fillHistos=true;
# Line 154  Int_t Loop(TString ddir,TString list, UL Line 161  Int_t Loop(TString ddir,TString list, UL
161  //  read input file/list  //  read input file/list
162  //  --------------------  //  --------------------
163      event = new PamLevel2();      event = new PamLevel2();
164        if(DEBUG)gObjectTable->Print();
165      if(list.Contains(".root")){      if(list.Contains(".root")){
166          stringstream command;          stringstream command;
167          command.str("");          command.str("");
168          command << " echo "<<list<<" > list-temp.txt";          command << " echo "<<list<<" > list-temp.txt";
169          cout << command.str().c_str() << endl;          cout << command.str().c_str() << endl;
170          gSystem->Exec(command.str().c_str());          gSystem->Exec(command.str().c_str());
171          tree  = event->GetPamTree(event->GetListOfLevel2Files(ddir,"list-temp.txt"),options);          tree  = event->GetPamTree(ddir,"list-temp.txt",options);
172      }else{            }else{      
173          tree  = event->GetPamTree(event->GetListOfLevel2Files(ddir,list),options);          tree  = event->GetPamTree(ddir,list,options);
174      };      };
175    //    gObjectTable->Print();
176      tree->SetCacheSize(0);      tree->SetCacheSize(0);
177    
178  //  ---------------  //  ---------------
# Line 174  Int_t Loop(TString ddir,TString list, UL Line 183  Int_t Loop(TString ddir,TString list, UL
183  //  -----------------  //  -----------------
184  //  loop over events  //  loop over events
185  //  -----------------  //  -----------------
186      ULong64_t nevents = tree->GetEntriesFast();      ULong64_t nevents = tree->GetEntries();    
187      if(!nmax)nmax = numeric_limits<ULong64_t>::max();        if(!nmax)nmax = numeric_limits<ULong64_t>::max();  
188      if(nevents < nmax)nmax=nevents;      if(nevents < nmax)nmax=nevents;
189            
190      cout << endl<<" Start loop over events:  "<< nmax<<endl;      cout << endl<<" Start loop over events:  "<< nmax<<endl;
191      Int_t ntrk=0;      Int_t ntrk = 0;
192      Int_t sel=0;      Int_t ncls = 0;
193        Int_t sel  = 0;
194        Int_t get  = 0;
195      TBenchmark *benchmark = new TBenchmark();      TBenchmark *benchmark = new TBenchmark();
196      benchmark->Start("event-loop");      benchmark->Start("event-loop");
197            
198      TString current_file = "";      TString current_file = "";
199      for(ULong64_t iev=60000; iev<nmax; iev++){      if(DEBUG)gObjectTable->Print();
200        for(ULong64_t iev=0; iev<nmax; iev++){
201                    
202  //      tree->GetEntry(iev);          event->Clear();
203          if( Process(iev) )sel++;          if( tree->GetEntry(iev) ){
204                get++;
205          if(current_file.CompareTo(tree->GetFile()->GetName())){              if( Process(iev) ){
206              current_file=tree->GetFile()->GetName();                  sel++;
207              cout << iev<< " -> "<< current_file << endl;              }
208  //          cout << tree->GetFile()->GetName() << endl;              if(current_file.CompareTo(tree->GetFile()->GetName())){
209                    current_file=tree->GetFile()->GetName();
210                    cout << iev<< " -> "<< current_file << endl;
211                };
212            }else{
213                cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
214          };          };
215            if( !(iev%5000) && DEBUG)gObjectTable->Print();
216    
         if(event->TrkLevel2::ntrk() >0)ntrk++;    
         if(!(iev%100))cout << iev << endl;  
           
217      };      };
218      benchmark->Show("event-loop");      benchmark->Show("event-loop");
219      cout << ntrk <<" tracks over "<<nmax<<" events ";      cout << sel <<" selected events over "<<get;
220      if(nmax)cout<<"("<< 100*ntrk/nmax<<"%)"<<endl;      if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
221      cout << "Selected events: "<<sel<<endl;      if(DEBUG)gObjectTable->Print();
222            event->Clear();
223  //  --------------  //  --------------
224  //  close and exit  //  close and exit
225  //  --------------  //  --------------
# Line 216  Int_t Loop(TString ddir,TString list, UL Line 231  Int_t Loop(TString ddir,TString list, UL
231    
232  /////////////////////////////////////////////////////////////////  /////////////////////////////////////////////////////////////////
233    
234  #if !defined(__CINT__)  #if !defined(__CINT__)
235    
236  // input parameters  // // input parameters
237  Bool_t    DEBUG;  // Bool_t    DEBUG;
238  TString   DIR;  // TString   DIR;
239  TString   LIST;  // TString   LIST;
240  ULong64_t MAXEV;  // TString   OPTIONS;
241  TString   OUTFILE;  // ULong64_t MAXEV;
242    // TString   OUTFILE;
243    
244  void usage(){  void usage(){
245    
246        cout << "------------------------------------------------------------"<<endl;
247        cout << "Loop over events (on one or more Level2-files), applying some selection cuts (defined in My-Selection.cpp), \n";
248        cout << "creating output histograms (defined in My-Histos.cpp) and/or trees with selected events. \n \n ";
249      cout << "USAGE:"<<endl;      cout << "USAGE:"<<endl;
250      cout << "-processDir  [DIR]   -  Level2/Level1 data directory \n";      cout << "-processDir  DIR     -  Level2 data directory \n";
251      cout << "-processList [LIST]  -  list (txt) of names of files to be analysed \n";      cout << "-processList LIST    -  list  of files (.txt) or single file (.root) to be analysed \n";
252      cout << "-outputFile  [PATH]  -  name of the output file \n";      cout << "-outputFile  PATH    -  name of the output file \n";
253      cout << "-NumEvents   [XXX]   -  number of events to be analysed \n";      cout << "-NumEvents   XXX     -  number of events to be analysed \n";
254      cout << "--debug, -g          -  debug mode \n";      cout << "--debug, -g          -  debug mode \n";
255      cout << "--help, -h           -  print this help \n";      cout << "--help, -h           -  print this help \n";
256        cout << "-options [ options ] -  options: \n";
257        cout << "                        fillHistos --> create an output file with histograms  \n";
258        cout << "                        fillTree   --> create an output file with trees storing the selected events \n ";
259        cout << "                        +(-)ALL    --> inlcude(exclude) all trees and branches \n "   ;
260        cout << "                        +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) trees and branches  \n"  ;
261        cout << "------------------------------------------------------------"<<endl;
262  }  }
263  //  //
264  int HandleInputPar(int argc, char **argv){  int HandleInputPar(int argc, char **argv){
# Line 246  int HandleInputPar(int argc, char **argv Line 272  int HandleInputPar(int argc, char **argv
272  // -----------------------  // -----------------------
273  // Read input parameters  // Read input parameters
274  // -----------------------    // -----------------------  
275  /*              DEBUG   = false;          DEBUG   = false;
276                  DIR     = gSystem->WorkingDirectory();          DIR     = gSystem->WorkingDirectory();
277                  LIST    = "";          LIST    = "";
278                  OUTFILE = "TrkLevel1Report_output.root";          OUTFILE = "";
279                  MAXEV = numeric_limits<ULong64_t>::max();          OPTIONS = "+ALL fillTree";
280  */          MAXEV   = 0;    
281                                    
282    
283          for (int i = 1; i < argc; i++){                  for (int i = 1; i < argc; i++){        
# Line 284  int HandleInputPar(int argc, char **argv Line 310  int HandleInputPar(int argc, char **argv
310                  continue;                        continue;      
311              }              }
312              // -----------------------------------------------------//              // -----------------------------------------------------//
313                else if (!strcmp(argv[i], "-options")){
314                    if (++i >= argc) throw -1;
315                    OPTIONS = argv[i];
316                    if( OPTIONS.Contains("[") ){
317                        do{
318                            if (++i >= argc) throw -1;
319                            OPTIONS.Append(argv[i]);
320                        }while(!OPTIONS.Contains("]"));
321                    }else cout << "wrong option format --> ignoring " << endl;
322                }
323                // -----------------------------------------------------//
324              else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){              else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){
325                  DEBUG = true;                  DEBUG = true;
326                  continue;                        continue;      
# Line 312  int main(int argc, char **argv) Line 349  int main(int argc, char **argv)
349                    
350      if( HandleInputPar(argc,argv) )return(1);      if( HandleInputPar(argc,argv) )return(1);
351                    
352      Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC fillHisto fillTree",OUTFILE);  //    Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE);
353        cout << "OPTIONS "<<OPTIONS<<endl;
354        Loop(DIR,LIST,MAXEV,OPTIONS,OUTFILE);
355            
356      cout << "Back to main - end"<<endl;      cout << "Back to main - end"<<endl;
357            

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.23