CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWLiteWithSelectorUtils.cc
Go to the documentation of this file.
1 #include <TH1F.h>
2 #include <TROOT.h>
3 #include <TFile.h>
4 #include <TSystem.h>
5 
9 
14 
15 #include "TStopwatch.h"
18 //#include "PhysicsTools/FWLite/interface/WSelectorFast.h"
19 
20 
21 int main(int argc, char* argv[])
22 {
23  // define what muon you are using; this is necessary as FWLite is not
24  // capable of reading edm::Views
25  using reco::Muon;
26 
27  // ----------------------------------------------------------------------
28  // First Part:
29  //
30  // * enable the AutoLibraryLoader
31  // * book the histograms of interest
32  // * open the input file
33  // ----------------------------------------------------------------------
34 
35  // load framework libraries
36  gSystem->Load( "libFWCoreFWLite" );
38 
39  if ( argc < 2 ) {
40  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
41  return 0;
42  }
43 
44  if( !edm::readPSetsFrom(argv[1])->existsAs<edm::ParameterSet>("process") ){
45  std::cout << " ERROR: ParametersSet 'process' is missing in your configuration file" << std::endl; exit(0);
46  }
47  // get the python configuration
48  const edm::ParameterSet& process = edm::readPSetsFrom(argv[1])->getParameter<edm::ParameterSet>("process");
49  fwlite::InputSource inputHandler_(process); fwlite::OutputFiles outputHandler_(process);
50 
51  // initialize the W selector
53  WSelector wSelector( selection ); pat::strbitset wSelectorReturns = wSelector.getBitTemplate();
54 
55  // book a set of histograms
56  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file().c_str());
57  TFileDirectory theDir = fs.mkdir("analyzeBasicPat");
58  TH1F* muonPt_ = theDir.make<TH1F>("muonPt", "pt", 100, 0.,300.);
59  TH1F* muonEta_ = theDir.make<TH1F>("muonEta","eta", 100, -3., 3.);
60  TH1F* muonPhi_ = theDir.make<TH1F>("muonPhi","phi", 100, -5., 5.);
61 
62  // start a CPU timer
63  TStopwatch timer; timer.Start();
64 
65  // loop the events
66  int ievt=0;
67  unsigned int nEventsAnalyzed = 0;
68  int maxEvents_( inputHandler_.maxEvents() );
69  for(unsigned int iFile=0; iFile<inputHandler_.files().size(); ++iFile){
70  // open input file (can be located on castor)
71  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
72  if( inFile ){
73  // ----------------------------------------------------------------------
74  // Second Part:
75  //
76  // * loop the events in the input file
77  // * receive the collections of interest via fwlite::Handle
78  // * fill the histograms
79  // * after the loop close the input file
80  // ----------------------------------------------------------------------
81  fwlite::Event ev(inFile);
82  for(ev.toBegin(); !ev.atEnd(); ++ev, ++ievt){
83  edm::EventBase const & event = ev;
84  // break loop if maximal number of events is reached
85  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
86  // simple event counter
87  if(inputHandler_.reportAfter()!=0 ? (ievt>0 && ievt%inputHandler_.reportAfter()==0) : false)
88  std::cout << " processing event: " << ievt << std::endl;
89 
90  if ( wSelector(event, wSelectorReturns ) ) {
91  pat::Muon const & wMuon = wSelector.wMuon();
92  muonPt_ ->Fill( wMuon.pt() );
93  muonEta_->Fill( wMuon.eta() );
94  muonPhi_->Fill( wMuon.phi() );
95  }
96  ++nEventsAnalyzed;
97  }
98  // close input file
99  inFile->Close();
100  }
101  // break loop if maximal number of events is reached:
102  // this has to be done twice to stop the file loop as well
103  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
104  }
105  // stop CPU timer
106  timer.Stop();
107 
108  // print selector
109  wSelector.print(std::cout);
110 
111  // print some timing statistics
112  double rtime = timer.RealTime();
113  double ctime = timer.CpuTime ();
114  // timing printouts
115  printf("Analyzed events: %d \n",nEventsAnalyzed);
116  printf("RealTime=%f seconds, CpuTime=%f seconds\n",rtime,ctime);
117  printf("%4.2f events / RealTime second .\n", (double)nEventsAnalyzed/rtime);
118  printf("%4.2f events / CpuTime second .\n", (double)nEventsAnalyzed/ctime);
119  return 0;
120 }
T getParameter(std::string const &) const
selection
main part
Definition: corrVsCorr.py:98
int main(int argc, char **argv)
virtual float phi() const GCC11_FINAL
momentum azimuthal angle
Event const & toBegin()
Go to the very first Event.
Definition: Event.cc:205
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
boost::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
virtual float eta() const GCC11_FINAL
momentum pseudorapidity
virtual bool atEnd() const
Definition: Event.cc:248
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
tuple argc
Definition: dir2webdir.py:41
T * make() const
make new ROOT object
static void enable()
enable automatic library loading
tuple cout
Definition: gather_cfg.py:121
tuple process
Definition: LaserDQM_cfg.py:3
virtual float pt() const GCC11_FINAL
transverse momentum
Example class of an EventSelector to apply a simple W Boson selection.
Definition: WSelector.h:15
Analysis-level muon class.
Definition: Muon.h:51
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:29