CMS 3D CMS Logo

FWLiteHistograms.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <vector>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 
8 #include <TH1F.h>
9 #include <TROOT.h>
10 #include <TFile.h>
11 #include <TSystem.h>
12 
16 
21 
22 int main(int argc, char* argv[]) {
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 FWLite
31  // * book the histograms of interest
32  // * open the input file
33  // ----------------------------------------------------------------------
34 
35  // load framework libraries
36  gSystem->Load("libFWCoreFWLite");
38 
39  // initialize command line parser
40  optutl::CommandLineParser parser("Analyze FWLite Histograms");
41 
42  // set defaults
43  parser.integerValue("maxEvents") = 1000;
44  parser.integerValue("outputEvery") = 10;
45  parser.stringValue("outputFile") = "analyzeFWLiteHistograms.root";
46 
47  // parse arguments
48  parser.parseArguments(argc, argv);
49  int maxEvents_ = parser.integerValue("maxEvents");
50  unsigned int outputEvery_ = parser.integerValue("outputEvery");
51  std::string outputFile_ = parser.stringValue("outputFile");
52  std::vector<std::string> inputFiles_ = parser.stringVector("inputFiles");
53 
54  // book a set of histograms
56  TFileDirectory dir = fs.mkdir("analyzeBasicPat");
57  TH1F* muonPt_ = dir.make<TH1F>("muonPt", "pt", 100, 0., 300.);
58  TH1F* muonEta_ = dir.make<TH1F>("muonEta", "eta", 100, -3., 3.);
59  TH1F* muonPhi_ = dir.make<TH1F>("muonPhi", "phi", 100, -5., 5.);
60  TH1F* mumuMass_ = dir.make<TH1F>("mumuMass", "mass", 90, 30., 120.);
61 
62  // loop the events
63  int ievt = 0;
64  for (unsigned int iFile = 0; iFile < inputFiles_.size(); ++iFile) {
65  // open input file (can be located on castor)
66  TFile* inFile = TFile::Open(inputFiles_[iFile].c_str());
67  if (inFile) {
68  // ----------------------------------------------------------------------
69  // Second Part:
70  //
71  // * loop the events in the input file
72  // * receive the collections of interest via fwlite::Handle
73  // * fill the histograms
74  // * after the loop close the input file
75  // ----------------------------------------------------------------------
76  fwlite::Event ev(inFile);
77  for (ev.toBegin(); !ev.atEnd(); ++ev, ++ievt) {
78  edm::EventBase const& event = ev;
79  // break loop if maximal number of events is reached
80  if (maxEvents_ > 0 ? ievt + 1 > maxEvents_ : false)
81  break;
82  // simple event counter
83  if (outputEvery_ != 0 ? (ievt > 0 && ievt % outputEvery_ == 0) : false)
84  std::cout << " processing event: " << ievt << std::endl;
85 
86  // Handle to the muon collection
88  event.getByLabel(std::string("muons"), muons);
89 
90  // loop muon collection and fill histograms
91  for (std::vector<Muon>::const_iterator mu1 = muons->begin(); mu1 != muons->end(); ++mu1) {
92  muonPt_->Fill(mu1->pt());
93  muonEta_->Fill(mu1->eta());
94  muonPhi_->Fill(mu1->phi());
95  if (mu1->pt() > 20 && fabs(mu1->eta()) < 2.1) {
96  for (std::vector<Muon>::const_iterator mu2 = muons->begin(); mu2 != muons->end(); ++mu2) {
97  if (mu2 > mu1) { // prevent double conting
98  if (mu1->charge() * mu2->charge() < 0) { // check only muon pairs of unequal charge
99  if (mu2->pt() > 20 && fabs(mu2->eta()) < 2.1) {
100  mumuMass_->Fill((mu1->p4() + mu2->p4()).mass());
101  }
102  }
103  }
104  }
105  }
106  }
107  }
108  // close input file
109  inFile->Close();
110  }
111  // break loop if maximal number of events is reached:
112  // this has to be done twice to stop the file loop as well
113  if (maxEvents_ > 0 ? ievt + 1 > maxEvents_ : false)
114  break;
115  }
116  return 0;
117 }
int main(int argc, char *argv[])
static void enable()
enable automatic library loading