CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PatBasicFWLiteJetAnalyzer.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 
20 
21 
22 int main(int argc, char* argv[])
23 {
24  // ----------------------------------------------------------------------
25  // First Part:
26  //
27  // * enable the AutoLibraryLoader
28  // * book the histograms of interest
29  // * open the input file
30  // ----------------------------------------------------------------------
31 
32  // load framework libraries
33  gSystem->Load( "libFWCoreFWLite" );
35 
36  // only allow one argument for this simple example which should be the
37  // the python cfg file
38  if ( argc < 2 ) {
39  std::cout << "Usage : " << argv[0] << " [parameters.py]" << std::endl;
40  return 0;
41  }
42 
43  // get the python configuration
44  PythonProcessDesc builder(argv[1]);
45  const edm::ParameterSet& fwliteParameters = builder.processDesc()->getProcessPSet()->getParameter<edm::ParameterSet>("FWLiteParams");
46 
47  // now get each parameter
48  std::string input_ ( fwliteParameters.getParameter<std::string >("inputFile" ) );
49  std::string output_( fwliteParameters.getParameter<std::string >("outputFile" ) );
50  edm::InputTag jets_ ( fwliteParameters.getParameter<edm::InputTag>("jets") );
51 
52  // book a set of histograms
53  fwlite::TFileService fs = fwlite::TFileService(output_.c_str());
54  TFileDirectory theDir = fs.mkdir("analyzeBasicPat");
55  TH1F* jetPt_ = theDir.make<TH1F>("jetPt", "pt", 100, 0.,300.);
56  TH1F* jetEta_ = theDir.make<TH1F>("jetEta","eta", 100, -3., 3.);
57  TH1F* jetPhi_ = theDir.make<TH1F>("jetPhi","phi", 100, -5., 5.);
58  TH1F* disc_ = theDir.make<TH1F>("disc", "Discriminant", 100, 0.0, 10.0);
59  TH1F* constituentPt_ = theDir.make<TH1F>("constituentPt", "Constituent pT", 100, 0, 300.0);
60 
61  // open input file (can be located on castor)
62  TFile* inFile = TFile::Open(input_.c_str());
63 
64  // ----------------------------------------------------------------------
65  // Second Part:
66  //
67  // * loop the events in the input file
68  // * receive the collections of interest via fwlite::Handle
69  // * fill the histograms
70  // * after the loop close the input file
71  // ----------------------------------------------------------------------
72 
73  // loop the events
74  unsigned int iEvent=0;
75  fwlite::Event ev(inFile);
76  for(ev.toBegin(); !ev.atEnd(); ++ev, ++iEvent){
77  edm::EventBase const & event = ev;
78 
79  // break loop after end of file is reached
80  // or after 1000 events have been processed
81  if( iEvent==1000 ) break;
82 
83  // simple event counter
84  if(iEvent>0 && iEvent%1==0){
85  std::cout << " processing event: " << iEvent << std::endl;
86  }
87 
88  // Handle to the jet collection
90  edm::InputTag jetLabel( argv[3] );
91  event.getByLabel(jets_, jets);
92 
93  // loop jet collection and fill histograms
94  for(unsigned i=0; i<jets->size(); ++i){
95  // basic kinematics
96  jetPt_ ->Fill( (*jets)[i].pt() );
97  jetEta_->Fill( (*jets)[i].eta() );
98  jetPhi_->Fill( (*jets)[i].phi() );
99  // access tag infos
100  reco::SecondaryVertexTagInfo const *svTagInfos = (*jets)[i].tagInfoSecondaryVertex("secondaryVertex");
101  if( svTagInfos != 0 ) {
102  if( svTagInfos->nVertices() > 0 ){
103  disc_->Fill( svTagInfos->flightDistance(0).value() );
104  }
105  }
106  // access calo towers
107  std::vector<reco::PFCandidatePtr> const & pfConstituents = (*jets)[i].getPFConstituents();
108  for( std::vector<reco::PFCandidatePtr>::const_iterator ibegin=pfConstituents.begin(), iend=pfConstituents.end(), iconstituent=ibegin; iconstituent!=iend; ++iconstituent){
109  constituentPt_->Fill( (*iconstituent)->pt() );
110  }
111  }
112  }
113  // close input file
114  inFile->Close();
115 
116  // ----------------------------------------------------------------------
117  // Third Part:
118  //
119  // * never forget to free the memory of objects you created
120  // ----------------------------------------------------------------------
121 
122  // in this example there is nothing to do
123 
124  // that's it!
125  return 0;
126 }
127 
Measurement1D flightDistance(unsigned int index, bool in2d=false) const
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool ev
T eta() const
std::shared_ptr< edm::ProcessDesc > processDesc()
int main(int argc, char **argv)
Event const & toBegin()
Go to the very first Event.
Definition: Event.cc:242
int iEvent
Definition: GenABIO.cc:230
vector< PseudoJet > jets
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
double value() const
Definition: Measurement1D.h:28
static void enable()
enable automatic library loading
tuple cout
Definition: gather_cfg.py:121
Definition: DDAxes.h:10