/[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.5 by pam-fi, Fri Feb 16 18:38:13 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    TChain    *runt  = NULL;
34  TFile     *outfh = NULL;  TFile     *outfh = NULL;
35  TFile     *outft = NULL;  TFile     *outft = NULL;
36  TTree     *otree = NULL;  TTree     *otree = NULL;
37    GL_RUN    *run   = NULL;
38    
39  bool fillTree   = false;  bool fillTree   = false;
40  bool fillHistos = false;  bool fillHistos = false;
# Line 33  bool fillHistos = false; Line 43  bool fillHistos = false;
43  // Functions to be provided externally  // Functions to be provided externally
44  //====================================  //====================================
45  bool Select(PamLevel2*);  bool Select(PamLevel2*);
46  void CreateHistos();  void CreateHistos(PamLevel2*, TFile *);
47  void FillHistos(PamLevel2*);  void FillHistos(PamLevel2*);
48  void SaveHistos(TFile *);  void SaveHistos(TFile *);
49    
# Line 42  void SaveHistos(TFile *); Line 52  void SaveHistos(TFile *);
52  //==========================================  //==========================================
53  bool Begin(){  bool Begin(){
54                    
55      //------------------------  //     //------------------------
56      //  load magnetic field  //     //  load magnetic field
57      //------------------------  //     //------------------------
58      TString fieldpath = gSystem->Getenv("PAM_CALIB");  //     TString fieldpath = gSystem->Getenv("PAM_CALIB");
59      if(fieldpath.IsNull()){  //     if(fieldpath.IsNull()){
60          cout << " **ERROR** : No PAMELA environment variables defined "<<endl;  //      cout << " **ERROR** : No PAMELA environment variables defined "<<endl;
61          return 0;  //      return 0;
62      }  //     }
63      fieldpath.Append("/trk-param/field_param-0/");  //     fieldpath.Append("/trk-param/field_param-0/");
64      event->LoadField(fieldpath.Data());  //     event->GetTrkLevel2()->LoadField(fieldpath.Data());
65    // === non c'e` piu` bisogno
66    
67      //------------------------------      //------------------------------
68      //  create output histos/trees      //  create output histos/trees
69      //------------------------------      //------------------------------
70      if(fillHistos) CreateHistos();      if(fillHistos) CreateHistos(event,outfh);
71      if(fillTree)   event->CreateCloneTrees(tree);      if(fillTree)event->CreateCloneTrees(outft);
72    
73    //    gDirectory->ls();
74    
75      cout << "\nBegin() - done\n\n";      cout << "\nBegin() - done\n\n";
76      return 1;      return 1;
# Line 70  bool Begin(){ Line 82  bool Begin(){
82    
83  bool Process(int iev){  bool Process(int iev){
84    
     //------------------------------  
     //  create output histos/trees  
     //------------------------------  
   
     event->Clear();  
     tree->GetEntry(iev);  
85            
86      if( !Select(event) )return false;      if( !Select(event) )return false;
 //    cout <<event->TrkLevel2::ntrk()<<endl;  
   
87    
88  //    if(fillTree)event->FillCloneTrees();      if(fillHistos)FillHistos(event);
89  //    if(fillHistos)FillHistos(event);      if(fillTree)event->FillCloneTrees();
90    
91      return true;      return true;
92    
# Line 93  bool Process(int iev){ Line 97  bool Process(int iev){
97    
98  bool Finish(){  bool Finish(){
99            
100        if(fillHistos){
101            SaveHistos(outfh);
102            outfh->Close();
103        }
104      if(fillTree){      if(fillTree){
105          outft->cd();          outft->cd();
106          event->WriteCloneTrees();          event->WriteCloneTrees();
107          outft->Close();          outft->Close();
108      }      }
     if(fillHistos){  
         SaveHistos(outfh);  
         outfh->Close();  
     }  
109            
110      cout << "Finish() - done\n";      cout << "Finish() - done\n";
111            
# Line 112  bool Finish(){ Line 116  bool Finish(){
116  //000000000000000000000000000000000000000000  //000000000000000000000000000000000000000000
117  //==========================================  //==========================================
118  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){
119    
120    //    gObjectTable->Print();
121                    
122      if(options.Contains("fillTree"))fillTree=true;      if(options.Contains("fillTree"))fillTree=true;
123      if(options.Contains("fillHisto"))fillHistos=true;      if(options.Contains("fillHisto"))fillHistos=true;
# Line 154  Int_t Loop(TString ddir,TString list, UL Line 160  Int_t Loop(TString ddir,TString list, UL
160  //  read input file/list  //  read input file/list
161  //  --------------------  //  --------------------
162      event = new PamLevel2();      event = new PamLevel2();
163        if(DEBUG)gObjectTable->Print();
164      if(list.Contains(".root")){      if(list.Contains(".root")){
165          stringstream command;          stringstream command;
166          command.str("");          command.str("");
167          command << " echo "<<list<<" > list-temp.txt";          command << " echo "<<list<<" > list-temp.txt";
168          cout << command.str().c_str() << endl;          cout << command.str().c_str() << endl;
169          gSystem->Exec(command.str().c_str());          gSystem->Exec(command.str().c_str());
170          tree  = event->GetPamTree(event->GetListOfLevel2Files(ddir,"list-temp.txt"),options);          tree  = event->GetPamTree(ddir,"list-temp.txt",options);
171            runt  = event->GetRunTree(ddir,"list-temp.txt");
172      }else{            }else{      
173          tree  = event->GetPamTree(event->GetListOfLevel2Files(ddir,list),options);          tree  = event->GetPamTree(ddir,list,options);
174            runt  = event->GetRunTree(ddir,list);
175      };      };
176    //    gObjectTable->Print();
177      tree->SetCacheSize(0);      tree->SetCacheSize(0);
178    
179  //  ---------------  //  ---------------
# Line 174  Int_t Loop(TString ddir,TString list, UL Line 184  Int_t Loop(TString ddir,TString list, UL
184  //  -----------------  //  -----------------
185  //  loop over events  //  loop over events
186  //  -----------------  //  -----------------
187      ULong64_t nevents = tree->GetEntriesFast();      ULong64_t nevents = tree->GetEntries();    
188      if(!nmax)nmax = numeric_limits<ULong64_t>::max();        if(!nmax)nmax = numeric_limits<ULong64_t>::max();  
189      if(nevents < nmax)nmax=nevents;      if(nevents < nmax)nmax=nevents;
190            
191      cout << endl<<" Start loop over events:  "<< nmax<<endl;      cout << endl<<" Start loop over events:  "<< nmax<<endl;
192      Int_t ntrk=0;      Int_t ntrk = 0;
193      Int_t sel=0;      Int_t ncls = 0;
194        Int_t sel  = 0;
195        Int_t get  = 0;
196      TBenchmark *benchmark = new TBenchmark();      TBenchmark *benchmark = new TBenchmark();
197      benchmark->Start("event-loop");      benchmark->Start("event-loop");
198            
199      TString current_file = "";      TString current_file = "";
200      for(ULong64_t iev=60000; iev<nmax; iev++){      if(DEBUG)gObjectTable->Print();
201        for(ULong64_t iev=0; iev<nmax; iev++){
202                    
203  //      tree->GetEntry(iev);          event->Clear();
204          if( Process(iev) )sel++;  //      if( tree->GetEntry(iev) ){
205            if( event->GetEntry(iev) ){ //<<< new feature
206          if(current_file.CompareTo(tree->GetFile()->GetName())){              get++;
207              current_file=tree->GetFile()->GetName();              if( Process(iev) ){
208              cout << iev<< " -> "<< current_file << endl;                  cout << "%%%% "<<iev << endl;
209  //          cout << tree->GetFile()->GetName() << endl;                  sel++;
210                }
211                if(current_file.CompareTo(tree->GetFile()->GetName())){
212                    current_file=tree->GetFile()->GetName();
213                    cout <<endl<< iev<< " -> "<< current_file << endl;
214                };
215            }else{
216                cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
217          };          };
218            if( !(iev%5000) && DEBUG)gObjectTable->Print();
         if(event->TrkLevel2::ntrk() >0)ntrk++;    
         if(!(iev%100))cout << iev << endl;  
219                    
220            if( !(iev%1000) )cout <<"|";
221    
222      };      };
223        cout <<endl;
224      benchmark->Show("event-loop");      benchmark->Show("event-loop");
225      cout << ntrk <<" tracks over "<<nmax<<" events ";      cout << sel <<" selected events over "<<get;
226      if(nmax)cout<<"("<< 100*ntrk/nmax<<"%)"<<endl;      if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
227      cout << "Selected events: "<<sel<<endl;      if(DEBUG)gObjectTable->Print();
228            event->Clear();
229  //  --------------  //  --------------
230  //  close and exit  //  close and exit
231  //  --------------  //  --------------
# Line 216  Int_t Loop(TString ddir,TString list, UL Line 237  Int_t Loop(TString ddir,TString list, UL
237    
238  /////////////////////////////////////////////////////////////////  /////////////////////////////////////////////////////////////////
239    
240  #if !defined(__CINT__)  #if !defined(__CINT__)
241    
242  // input parameters  // // input parameters
243  Bool_t    DEBUG;  // Bool_t    DEBUG;
244  TString   DIR;  // TString   DIR;
245  TString   LIST;  // TString   LIST;
246  ULong64_t MAXEV;  // TString   OPTIONS;
247  TString   OUTFILE;  // ULong64_t MAXEV;
248    // TString   OUTFILE;
249    
250  void usage(){  void usage(){
251    
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      cout << "USAGE:"<<endl;      cout << "USAGE:"<<endl;
256      cout << "-processDir  [DIR]   -  Level2/Level1 data directory \n";      cout << "-processDir  DIR     -  Level2 data directory \n";
257      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";
258      cout << "-outputFile  [PATH]  -  name of the output file \n";      cout << "-outputFile  PATH    -  name of the output file \n";
259      cout << "-NumEvents   [XXX]   -  number of events to be analysed \n";      cout << "-NumEvents   XXX     -  number of events to be analysed \n";
260      cout << "--debug, -g          -  debug mode \n";      cout << "--debug, -g          -  debug mode \n";
261      cout << "--help, -h           -  print this help \n";      cout << "--help, -h           -  print this help \n";
262        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  }  }
269  //  //
270  int HandleInputPar(int argc, char **argv){  int HandleInputPar(int argc, char **argv){
# Line 246  int HandleInputPar(int argc, char **argv Line 278  int HandleInputPar(int argc, char **argv
278  // -----------------------  // -----------------------
279  // Read input parameters  // Read input parameters
280  // -----------------------    // -----------------------  
281  /*              DEBUG   = false;          DEBUG   = false;
282                  DIR     = gSystem->WorkingDirectory();          DIR     = gSystem->WorkingDirectory();
283                  LIST    = "";          LIST    = "";
284                  OUTFILE = "TrkLevel1Report_output.root";          OUTFILE = "";
285                  MAXEV = numeric_limits<ULong64_t>::max();          OPTIONS = "+ALL fillTree";
286  */          MAXEV   = 0;    
287                                    
288    
289          for (int i = 1; i < argc; i++){                  for (int i = 1; i < argc; i++){        
# Line 284  int HandleInputPar(int argc, char **argv Line 316  int HandleInputPar(int argc, char **argv
316                  continue;                        continue;      
317              }              }
318              // -----------------------------------------------------//              // -----------------------------------------------------//
319                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              else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){              else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-g")){
331                  DEBUG = true;                  DEBUG = true;
332                  continue;                        continue;      
# Line 312  int main(int argc, char **argv) Line 355  int main(int argc, char **argv)
355                    
356      if( HandleInputPar(argc,argv) )return(1);      if( HandleInputPar(argc,argv) )return(1);
357                    
358      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);
359        cout << "OPTIONS "<<OPTIONS<<endl;
360        Loop(DIR,LIST,MAXEV,OPTIONS,OUTFILE);
361            
362      cout << "Back to main - end"<<endl;      cout << "Back to main - end"<<endl;
363            

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

  ViewVC Help
Powered by ViewVC 1.1.23