CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWLiteWithPythonConfig.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 
18 
19 
20 int main(int argc, char* argv[])
21 {
22  // define what muon you are using; this is necessary as FWLite is not
23  // capable of reading edm::Views
24  using reco::Muon;
25 
26  // ----------------------------------------------------------------------
27  // First Part:
28  //
29  // * enable FWLite
30  // * book the histograms of interest
31  // * open the input file
32  // ----------------------------------------------------------------------
33 
34  // load framework libraries
35  gSystem->Load( "libFWCoreFWLite" );
37 
38  // parse arguments
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 
52  // now get each parameter
53  const edm::ParameterSet& ana = process.getParameter<edm::ParameterSet>("muonAnalyzer");
54  edm::InputTag muons_( ana.getParameter<edm::InputTag>("muons") );
55 
56  // book a set of histograms
57  fwlite::TFileService fs = fwlite::TFileService(outputHandler_.file().c_str());
58  TFileDirectory dir = fs.mkdir("analyzeBasicPat");
59  TH1F* muonPt_ = dir.make<TH1F>("muonPt" , "pt" , 100, 0., 300.);
60  TH1F* muonEta_ = dir.make<TH1F>("muonEta" , "eta" , 100, -3., 3.);
61  TH1F* muonPhi_ = dir.make<TH1F>("muonPhi" , "phi" , 100, -5., 5.);
62  TH1F* mumuMass_= dir.make<TH1F>("mumuMass", "mass", 90, 30., 120.);
63 
64  // loop the events
65  int ievt=0;
66  int maxEvents_( inputHandler_.maxEvents() );
67  for(unsigned int iFile=0; iFile<inputHandler_.files().size(); ++iFile){
68  // open input file (can be located on castor)
69  TFile* inFile = TFile::Open(inputHandler_.files()[iFile].c_str());
70  if( inFile ){
71  // ----------------------------------------------------------------------
72  // Second Part:
73  //
74  // * loop the events in the input file
75  // * receive the collections of interest via fwlite::Handle
76  // * fill the histograms
77  // * after the loop close the input file
78  // ----------------------------------------------------------------------
79  fwlite::Event ev(inFile);
80  for(ev.toBegin(); !ev.atEnd(); ++ev, ++ievt){
81  edm::EventBase const & event = ev;
82  // break loop if maximal number of events is reached
83  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
84  // simple event counter
85  if(inputHandler_.reportAfter()!=0 ? (ievt>0 && ievt%inputHandler_.reportAfter()==0) : false)
86  std::cout << " processing event: " << ievt << std::endl;
87 
88  // Handle to the muon collection
90  event.getByLabel(muons_, muons);
91 
92  // loop muon collection and fill histograms
93  for(std::vector<Muon>::const_iterator mu1=muons->begin(); mu1!=muons->end(); ++mu1){
94  muonPt_ ->Fill( mu1->pt () );
95  muonEta_->Fill( mu1->eta() );
96  muonPhi_->Fill( mu1->phi() );
97  if( mu1->pt()>20 && fabs(mu1->eta())<2.1 ){
98  for(std::vector<Muon>::const_iterator mu2=muons->begin(); mu2!=muons->end(); ++mu2){
99  if(mu2>mu1){ // prevent double conting
100  if( mu1->charge()*mu2->charge()<0 ){ // check only muon pairs of unequal charge
101  if( mu2->pt()>20 && fabs(mu2->eta())<2.1 ){
102  mumuMass_->Fill( (mu1->p4()+mu2->p4()).mass() );
103  }
104  }
105  }
106  }
107  }
108  }
109  }
110  // close input file
111  inFile->Close();
112  }
113  // break loop if maximal number of events is reached:
114  // this has to be done twice to stop the file loop as well
115  if(maxEvents_>0 ? ievt+1>maxEvents_ : false) break;
116  }
117  return 0;
118 }
T getParameter(std::string const &) const
std::shared_ptr< ParameterSet > readPSetsFrom(std::string const &fileOrString)
bool ev
Event const & toBegin()
Go to the very first Event.
Definition: Event.cc:242
static void enable()
enable automatic library loading
T * make(const Args &...args) const
make new ROOT object
virtual bool atEnd() const
Definition: Event.cc:285
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
tuple argc
Definition: dir2webdir.py:38
tuple muons
Definition: patZpeak.py:38
tuple cout
Definition: gather_cfg.py:121
dbl *** dir
Definition: mlp_gen.cc:35
tuple process
Definition: LaserDQM_cfg.py:3
std::string const & file() const
return output fuke name
Definition: OutputFiles.h:29