/[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.4 by pam-fi, Mon Jan 15 11:51:39 2007 UTC revision 1.6 by pam-fi, Fri Aug 17 16:57:11 2007 UTC
# Line 24  using namespace std; Line 24  using namespace std;
24  Bool_t    DEBUG;  Bool_t    DEBUG;
25  TString   DIR;  TString   DIR;
26  TString   LIST;  TString   LIST;
27  TString   OPTIONS;  TString   I_OPTIONS;
28    TString   O_OPTIONS;
29    ULong64_t MINEV;
30  ULong64_t MAXEV;  ULong64_t MAXEV;
31  TString   OUTFILE;  TString   OUTFILE;
32    Bool_t    SIMU;
33    
34  PamLevel2 *event = NULL;  PamLevel2 *event = NULL;
35  TChain    *tree  = NULL;  TChain    *tree  = NULL;
36    TChain    *runt  = NULL;
37  TFile     *outfh = NULL;  TFile     *outfh = NULL;
38  TFile     *outft = NULL;  TFile     *outft = NULL;
39  TTree     *otree = NULL;  TTree     *otree = NULL;
40    GL_RUN    *run   = NULL;
41    
42  bool fillTree   = false;  bool fillTree   = false;
43  bool fillHistos = false;  bool fillHistos = false;
# Line 41  bool fillHistos = false; Line 46  bool fillHistos = false;
46  // Functions to be provided externally  // Functions to be provided externally
47  //====================================  //====================================
48  bool Select(PamLevel2*);  bool Select(PamLevel2*);
49  void CreateHistos(TFile *);  //void CreateHistos(PamLevel2*, TFile *);
50    void CreateHistos(PamLevel2*, TFile *);
51  void FillHistos(PamLevel2*);  void FillHistos(PamLevel2*);
52  void SaveHistos(TFile *);  void SaveHistos(TFile *);
53    
# Line 50  void SaveHistos(TFile *); Line 56  void SaveHistos(TFile *);
56  //==========================================  //==========================================
57  bool Begin(){  bool Begin(){
58                    
     //------------------------  
     //  load magnetic field  
     //------------------------  
     TString fieldpath = gSystem->Getenv("PAM_CALIB");  
     if(fieldpath.IsNull()){  
         cout << " **ERROR** : No PAMELA environment variables defined "<<endl;  
         return 0;  
     }  
     fieldpath.Append("/trk-param/field_param-0/");  
     event->GetTrkLevel2()->LoadField(fieldpath.Data());  
   
   
59      //------------------------------      //------------------------------
60      //  create output histos/trees      //  create output histos/trees
61      //------------------------------      //------------------------------
62      if(fillHistos) CreateHistos(outfh);      //if(fillHistos) CreateHistos(event,outfh);
63      if(fillTree){      if(fillHistos) CreateHistos(event, outfh);
64  //      outft->cd();      if(fillTree)event->CreateCloneTrees(outft);
 //      event->CreateCloneTrees(tree,outft);  
         event->CreateCloneTrees(outft);  
     }  
 //    gDirectory->ls();  
65    
66      cout << "\nBegin() - done\n\n";      cout << "\nBegin() - done\n\n";
67      return 1;      return 1;
# Line 85  bool Process(int iev){ Line 75  bool Process(int iev){
75    
76            
77      if( !Select(event) )return false;      if( !Select(event) )return false;
   
     if(fillTree)event->FillCloneTrees();  
78      if(fillHistos)FillHistos(event);      if(fillHistos)FillHistos(event);
79        if(fillTree)event->FillCloneTrees();//must be after fillhisto
80    
81      return true;      return true;
82    
# Line 98  bool Process(int iev){ Line 87  bool Process(int iev){
87    
88  bool Finish(){  bool Finish(){
89            
90        if(fillHistos){
91            SaveHistos(outfh);
92            outfh->Close();
93        }
94      if(fillTree){      if(fillTree){
95          outft->cd();          outft->cd();
96          event->WriteCloneTrees();          event->WriteCloneTrees();
97          outft->Close();          outft->Close();
98      }      }
     if(fillHistos){  
         SaveHistos(outfh);  
         outfh->Close();  
     }  
99            
100      cout << "Finish() - done\n";      cout << "Finish() - done\n";
101            
# Line 116  bool Finish(){ Line 105  bool Finish(){
105  //==========================================  //==========================================
106  //000000000000000000000000000000000000000000  //000000000000000000000000000000000000000000
107  //==========================================  //==========================================
108  Int_t Loop(TString ddir,TString list, ULong64_t nmax, TString options, TString outfile){  Int_t Loop(TString ddir,TString list, ULong64_t nmax,  ULong64_t nmin, TString ioptions, TString ooptions, TString outfile){
109    
110  //    gObjectTable->Print();  //    gObjectTable->Print();
111                    
112      if(options.Contains("fillTree"))fillTree=true;  //  ------------------------
113      if(options.Contains("fillHisto"))fillHistos=true;  //  check output options (I)
114    //  ------------------------
115        if(ooptions.Contains("fillTree"))fillTree=true;
116        if(ooptions.Contains("fillHisto"))fillHistos=true;
117        
118  //  -------------------  //  -------------------
119  //  create output files  //  create output files
120  //  -------------------  //  -------------------
121        TTree::SetMaxTreeSize(1000*Long64_t(2000000000)); //m 070308 + dimensione oltre la quale divide il file di uscita: 2TB
122    
123      TString outfile_t =0;      TString outfile_t =0;
124      TString outfile_h =0;      TString outfile_h =0;
125      if( outfile.IsNull() ){      if( outfile.IsNull() ){
# Line 161  Int_t Loop(TString ddir,TString list, UL Line 155  Int_t Loop(TString ddir,TString list, UL
155  //  read input file/list  //  read input file/list
156  //  --------------------  //  --------------------
157      event = new PamLevel2();      event = new PamLevel2();
158        event->SetGP(SIMU);//new
159        if(SIMU)cout << " >> SIMULATION << "<<event->IsGP()<<endl;
160        event->SetDBConnection();
161      if(DEBUG)gObjectTable->Print();      if(DEBUG)gObjectTable->Print();
162      if(list.Contains(".root")){      if(list.Contains(".root")){
163            TString tempfile = gSystem->BaseName(list.Data());
164    //      tempfile.Replace(tempfile.Index(".root",5),5,".txt",4);
165            tempfile.Append(".txt");
166            cout << "Creating temporary file: "<<tempfile<<endl;
167          stringstream command;          stringstream command;
168          command.str("");          command.str("");
169          command << " echo "<<list<<" > list-temp.txt";          command << " echo "<<list<<" > "<<tempfile;
170          cout << command.str().c_str() << endl;          cout << command.str().c_str() << endl;
171          gSystem->Exec(command.str().c_str());          gSystem->Exec(command.str().c_str());
172          tree  = event->GetPamTree(ddir,"list-temp.txt",options);          tree  = event->GetPamTree(ddir,tempfile.Data(),ioptions);
173            runt  = event->GetRunTree(ddir,tempfile.Data());
174            command.str("");
175            command << "rm -f "<<tempfile;
176            gSystem->Exec(command.str().c_str());
177      }else{            }else{      
178          tree  = event->GetPamTree(ddir,list,options);          tree  = event->GetPamTree(ddir,list,ioptions);
179            runt  = event->GetRunTree(ddir,list);
180      };      };
181    
182      
183  //    gObjectTable->Print();  //    gObjectTable->Print();
184      tree->SetCacheSize(0);      tree->SetCacheSize(0);
185    
186    //  -------------------------
187    //  check output options (II)
188    //  -------------------------
189        if(fillTree)event->SetWhichTrees(ooptions);
190    
191  //  ---------------  //  ---------------
192  //  initializations  //  initializations
193  //  ---------------  //  ---------------
# Line 186  Int_t Loop(TString ddir,TString list, UL Line 199  Int_t Loop(TString ddir,TString list, UL
199      ULong64_t nevents = tree->GetEntries();          ULong64_t nevents = tree->GetEntries();    
200      if(!nmax)nmax = numeric_limits<ULong64_t>::max();        if(!nmax)nmax = numeric_limits<ULong64_t>::max();  
201      if(nevents < nmax)nmax=nevents;      if(nevents < nmax)nmax=nevents;
202        nmax=nmax-nmin;
203            
204      cout << endl<<" Start loop over events:  "<< nmax<<endl;      cout << endl<<" Start loop over events:  "<< nmax <<endl;
205      Int_t ntrk = 0;      Int_t ntrk = 0;
206      Int_t ncls = 0;      Int_t ncls = 0;
207      Int_t sel  = 0;      Int_t sel  = 0;
# Line 197  Int_t Loop(TString ddir,TString list, UL Line 211  Int_t Loop(TString ddir,TString list, UL
211            
212      TString current_file = "";      TString current_file = "";
213      if(DEBUG)gObjectTable->Print();      if(DEBUG)gObjectTable->Print();
214      for(ULong64_t iev=0; iev<nmax; iev++){      for(ULong64_t iev=nmin; iev<nmin+nmax; iev++){
215                    
216          event->Clear();          event->Clear();
217          if( tree->GetEntry(iev) ){  //      if( tree->GetEntry(iev) ){
218            if( event->GetEntry(iev) ){ //<<< new feature
219              get++;              get++;
220              if( Process(iev) ){              if( Process(iev) ){
221    //              cout << "%%%% "<<iev << endl;
222                  sel++;                  sel++;
223              }              }
224              if(current_file.CompareTo(tree->GetFile()->GetName())){              if(current_file.CompareTo(tree->GetFile()->GetName())){
225                  current_file=tree->GetFile()->GetName();                  current_file=tree->GetFile()->GetName();
226                  cout << iev<< " -> "<< current_file << endl;                  cout <<endl<<"@ entry "<< iev<< " -> "<< current_file << endl;
227              };              };
228          }else{          }else{
229              cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;              cout << "Chain entry "<<iev<<" -- ERROR --"<<endl;
230          };          };
231          if( !(iev%5000) && DEBUG)gObjectTable->Print();          if( !(iev%5000) && DEBUG)gObjectTable->Print();
232            
233            if( !(iev%1000) )cout <<"|";
234    
235      };      };
236        cout <<endl;
237      benchmark->Show("event-loop");      benchmark->Show("event-loop");
238      cout << sel <<" selected events over "<<get;      cout << sel <<" selected events over "<<get;
239      if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;      if(get)cout<<" ("<< 100*sel/get<<"%)"<<endl;
# Line 244  Int_t Loop(TString ddir,TString list, UL Line 263  Int_t Loop(TString ddir,TString list, UL
263  void usage(){  void usage(){
264    
265      cout << "------------------------------------------------------------"<<endl;      cout << "------------------------------------------------------------"<<endl;
266      cout << "Loop over events (on one or more Level2-files), applying some selection cuts (defined in My-Selection.cpp), \n";      cout << "Loop over events (on one or more Level2-files), applying some selection cuts, \n";
267      cout << "creating output histograms (defined in My-Histos.cpp) and/or trees with selected events. \n \n ";      cout << "creating output histograms and/or trees with selected events. \n \n ";
268      cout << "USAGE:"<<endl;      cout << "USAGE:"<<endl;
269      cout << "-processDir  DIR     -  Level2 data directory \n";      cout << "-processDir  DIR     -  Level2 data directory \n";
270      cout << "-processList LIST    -  list  of files (.txt) or single file (.root) to be analysed \n";      cout << "-processList LIST    -  list  of files (.txt) or single file (.root) to be analysed \n";
271      cout << "-outputFile  PATH    -  name of the output file \n";      cout << "-outputFile  PATH    -  name of the output file \n";
272      cout << "-NumEvents   XXX     -  number of events to be analysed \n";      cout << "-NumEvents   XXX     -  number of events to be analysed \n";
273        cout << "-FirstEvent  XXX     -  first event to be analysed \n";
274      cout << "--debug, -g          -  debug mode \n";      cout << "--debug, -g          -  debug mode \n";
275      cout << "--help, -h           -  print this help \n";      cout << "--help, -h           -  print this help \n";
276      cout << "-options [ options ] -  options: \n";      cout << "-input-options [ options ] -  options: \n";
277        cout << "                        +(-)ALL    --> inlcude(exclude) all default level1/level2 trees and branches \n "   ;
278        cout << "                        +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) level1/level2 trees and branches \n"  ;
279        cout << "                        +TRK0 +CAL0 +TOF0 --> include level0 branches \n"  ;
280        cout << "-output-options [ options ] -  options: \n";
281      cout << "                        fillHistos --> create an output file with histograms  \n";      cout << "                        fillHistos --> create an output file with histograms  \n";
282      cout << "                        fillTree   --> create an output file with trees storing the selected events \n ";      cout << "                        fillTree   --> create an output file with trees storing the selected events \n ";
283        cout << "                        == NB == "<<endl;
284        cout << "                        output level1/level2 tree is created with the same trees/branches as read from input file."<<endl;
285        cout << "                        in order to modify the output structure use the following options:"<<endl;
286      cout << "                        +(-)ALL    --> inlcude(exclude) all trees and branches \n "   ;      cout << "                        +(-)ALL    --> inlcude(exclude) all trees and branches \n "   ;
287      cout << "                        +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) trees and branches  \n"  ;      cout << "                        +(-)TRK1 +(-)TRK2 +(-)CAL1 +(-)CAL2 +(-)TOF +(-)TRG +(-)ND +(-)S4 +(-)ORB --> inlcude(exclude) trees and branches \n"  ;
288        cout << "--simulation, -s     -  simulation file \n";    
289      cout << "------------------------------------------------------------"<<endl;      cout << "------------------------------------------------------------"<<endl;
290  }  }
291  //  //
# Line 276  int HandleInputPar(int argc, char **argv Line 304  int HandleInputPar(int argc, char **argv
304          DIR     = gSystem->WorkingDirectory();          DIR     = gSystem->WorkingDirectory();
305          LIST    = "";          LIST    = "";
306          OUTFILE = "";          OUTFILE = "";
307          OPTIONS = "+ALL fillTree";          I_OPTIONS = "+ALL ";
308            O_OPTIONS = "fillTree";
309          MAXEV   = 0;              MAXEV   = 0;    
310            MINEV   = 0;    
311            SIMU    = false;        
312                                    
313    
314          for (int i = 1; i < argc; i++){                  for (int i = 1; i < argc; i++){        
# Line 302  int HandleInputPar(int argc, char **argv Line 333  int HandleInputPar(int argc, char **argv
333                  cout << "outputFile "<<OUTFILE<<endl;                  cout << "outputFile "<<OUTFILE<<endl;
334                  continue;                  continue;
335              }                }  
336                // -----------------------------------------------------//
337                else if (!strcmp(argv[i], "-FirstEvent")){                
338                    if (++i >= argc) throw -1;                            
339                    MINEV = atoi(argv[i]);                                
340                    cout << "FirstEvent "<<MINEV<<endl;                    
341                    continue;                                              
342                }                                                          
343              // -----------------------------------------------------//              // -----------------------------------------------------//
344              else if (!strcmp(argv[i], "-NumEvents")){              else if (!strcmp(argv[i], "-NumEvents")){
345                  if (++i >= argc) throw -1;                  if (++i >= argc) throw -1;
# Line 310  int HandleInputPar(int argc, char **argv Line 348  int HandleInputPar(int argc, char **argv
348                  continue;                        continue;      
349              }              }
350              // -----------------------------------------------------//              // -----------------------------------------------------//
351              else if (!strcmp(argv[i], "-options")){              else if (!strcmp(argv[i], "-input-options")){
352                  if (++i >= argc) throw -1;                  if (++i >= argc) throw -1;
353                  OPTIONS = argv[i];                  I_OPTIONS = argv[i];
354                  if( OPTIONS.Contains("[") ){                  if( I_OPTIONS.Contains("[") ){
355                      do{                      do{
356                          if (++i >= argc) throw -1;                          if (++i >= argc) throw -1;
357                          OPTIONS.Append(argv[i]);                          I_OPTIONS.Append(argv[i]);
358                      }while(!OPTIONS.Contains("]"));                      }while(!I_OPTIONS.Contains("]"));
359                    }else cout << "wrong option format --> ignoring " << endl;
360                }
361                // -----------------------------------------------------//
362                else if (!strcmp(argv[i], "-output-options")){
363                    if (++i >= argc) throw -1;
364                    O_OPTIONS = argv[i];
365                    if( O_OPTIONS.Contains("[") ){
366                        do{
367                            if (++i >= argc) throw -1;
368                            O_OPTIONS.Append(argv[i]);
369                        }while(!O_OPTIONS.Contains("]"));
370                  }else cout << "wrong option format --> ignoring " << endl;                  }else cout << "wrong option format --> ignoring " << endl;
371              }              }
372              // -----------------------------------------------------//              // -----------------------------------------------------//
# Line 326  int HandleInputPar(int argc, char **argv Line 375  int HandleInputPar(int argc, char **argv
375                  continue;                        continue;      
376              }              }
377              // -----------------------------------------------------//              // -----------------------------------------------------//
378                else if (!strcmp(argv[i], "--simulation") || !strcmp(argv[i], "-s")){
379                    SIMU = true;
380                    continue;      
381                }
382                // -----------------------------------------------------//
383              else{              else{
384                  cout << "Unidentified input parameter. Ignored."<< endl;                  cout << "Unidentified input parameter. Ignored."<< endl;
385              };              };
# Line 350  int main(int argc, char **argv) Line 404  int main(int argc, char **argv)
404      if( HandleInputPar(argc,argv) )return(1);      if( HandleInputPar(argc,argv) )return(1);
405                    
406  //    Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE);  //    Loop(DIR,LIST,MAXEV,"-ALL+TRK1+TRK2+CAL1+CAL2+TOF+AC",OUTFILE);
407      cout << "OPTIONS "<<OPTIONS<<endl;      cout << "INPUT OPTIONS "<<I_OPTIONS<<endl;
408      Loop(DIR,LIST,MAXEV,OPTIONS,OUTFILE);      cout << "OUTPUT OPTIONS "<<O_OPTIONS<<endl;
409        Loop(DIR,LIST,MAXEV,MINEV,I_OPTIONS,O_OPTIONS,OUTFILE);
410            
411      cout << "Back to main - end"<<endl;      cout << "Back to main - end"<<endl;
412            

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

  ViewVC Help
Powered by ViewVC 1.1.23