CMS 3D CMS Logo

PhotonMVANtuplizer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: RecoEgamma/PhotonIdentification
4 // Class: PhotonMVANtuplizer
5 //
13 //
14 // Original Author: Jonas REMBSER
15 // Created: Thu, 22 Mar 2018 14:54:24 GMT
16 //
17 //
18 
19 
20 // user include files
23 
25 
28 
32 
34 
36 
40 
41 #include <TTree.h>
42 #include <TFile.h>
43 #include <Math/VectorUtil.h>
44 
45 //
46 // class declaration
47 //
48 
49 // If the analyzer does not use TFileService, please remove
50 // the template argument to the base class so the class inherits
51 // from edm::one::EDAnalyzer<>
52 // This will improve performance in multithreaded jobs.
53 //
54 
55 class PhotonMVANtuplizer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
56 
57  public:
58  explicit PhotonMVANtuplizer(const edm::ParameterSet&);
59  ~PhotonMVANtuplizer() override;
60 
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63 
64  private:
65  void beginJob() override;
66  void analyze(const edm::Event&, const edm::EventSetup&) override;
67  void endJob() override;
68 
69  // ----------member data ---------------------------
70 
71  // for AOD case
75 
76  // for miniAOD case
80 
81  // other
82  TTree* tree_;
83 
84  std::vector<float> vars_;
85  int nVars_;
86 
87  //global variables
89  int genNpu_;
90  int vtxN_;
91 
92  // to hold ID decisions and categories
93  std::vector<int> mvaPasses_;
94  std::vector<float> mvaValues_;
95  std::vector<int> mvaCats_;
96 
97  // ID decisions objects
98  const std::vector< std::string > phoMapTags_;
99  std::vector< edm::EDGetTokenT< edm::ValueMap<bool> > > phoMapTokens_;
100  const std::vector< std::string > phoMapBranchNames_;
101  const size_t nPhoMaps_;
102 
103  // MVA values and categories (optional)
104  const std::vector< std::string > valMapTags_;
105  std::vector< edm::EDGetTokenT<edm::ValueMap<float> > > valMapTokens_;
106  const std::vector< std::string > valMapBranchNames_;
107  const size_t nValMaps_;
108 
109  const std::vector< std::string > mvaCatTags_;
110  std::vector< edm::EDGetTokenT<edm::ValueMap<int> > > mvaCatTokens_;
111  const std::vector< std::string > mvaCatBranchNames_;
112  const size_t nCats_;
113 
114  // config
115  const bool isMC_;
116  const double ptThreshold_;
117 };
118 
119 //
120 // constants, enums and typedefs
121 //
122 
123 //
124 // static data member definitions
125 //
126 
127 //
128 // constructors and destructor
129 //
131  :
132  src_ (consumes<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("src"))),
133  vertices_ (consumes<std::vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("vertices"))),
134  pileup_ (consumes<std::vector< PileupSummaryInfo > >(iConfig.getParameter<edm::InputTag>("pileup"))),
135  srcMiniAOD_ (consumes<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("srcMiniAOD"))),
136  verticesMiniAOD_ (consumes<std::vector<reco::Vertex> >(iConfig.getParameter<edm::InputTag>("verticesMiniAOD"))),
137  pileupMiniAOD_ (consumes<std::vector< PileupSummaryInfo > >(iConfig.getParameter<edm::InputTag>("pileupMiniAOD"))),
138  phoMapTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAs")),
139  phoMapBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVALabels")),
141  valMapTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAValMaps")),
142  valMapBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAValMapLabels")),
144  mvaCatTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVACats")),
145  mvaCatBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVACatLabels")),
147  isMC_ (iConfig.getParameter<bool>("isMC")),
148  ptThreshold_ (iConfig.getParameter<double>("ptThreshold"))
149 {
150  // phoMaps
151  for (size_t k = 0; k < nPhoMaps_; ++k) {
152 
154 
155  // Initialize vectors for holding ID decisions
156  mvaPasses_.push_back(0);
157  }
158 
159  // valMaps
160  for (size_t k = 0; k < nValMaps_; ++k) {
162 
163  // Initialize vectors for holding MVA values
164  mvaValues_.push_back(0.0);
165  }
166 
167  // categories
168  for (size_t k = 0; k < nCats_; ++k) {
170 
171  // Initialize vectors for holding MVA values
172  mvaCats_.push_back(0);
173  }
174 
175  // Book tree
176  usesResource(TFileService::kSharedResource);
178  tree_ = fs->make<TTree>("tree","tree");
179 
180  tree_->Branch("nEvent", &nEvent_);
181  tree_->Branch("nRun", &nRun_);
182  tree_->Branch("nLumi", &nLumi_);
183  if (isMC_) tree_->Branch("genNpu", &genNpu_);
184  tree_->Branch("vtxN", &vtxN_);
185 
186  // Has to be in two different loops
187  for (int i = 0; i < nVars_; ++i) {
188  vars_.push_back(0.0);
189  }
190 
191  // IDs
192  for (size_t k = 0; k < nValMaps_; ++k) {
193  tree_->Branch(valMapBranchNames_[k].c_str() , &mvaValues_[k]);
194  }
195 
196  for (size_t k = 0; k < nPhoMaps_; ++k) {
197  tree_->Branch(phoMapBranchNames_[k].c_str() , &mvaPasses_[k]);
198  }
199 
200  for (size_t k = 0; k < nCats_; ++k) {
201  tree_->Branch(mvaCatBranchNames_[k].c_str() , &mvaCats_[k]);
202  }
203 }
204 
205 
207 {
208 
209  // do anything here that needs to be done at desctruction time
210  // (e.g. close files, deallocate resources etc.)
211 
212 }
213 
214 
215 //
216 // member functions
217 //
218 
219 // ------------ method called for each event ------------
220 void
222 {
223  // Fill global event info
224  nEvent_ = iEvent.id().event();
225  nRun_ = iEvent.id().run();
226  nLumi_ = iEvent.luminosityBlock();
227 
228 
229  // Retrieve Vertecies
231  iEvent.getByToken(vertices_, vertices);
232  if( !vertices.isValid() ){
233  iEvent.getByToken(verticesMiniAOD_,vertices);
234  if( !vertices.isValid() )
235  throw cms::Exception(" Collection not found: ")
236  << " failed to find a standard AOD or miniAOD vertex collection " << std::endl;
237  }
238 
239  vtxN_ = vertices->size();
240 
241  // Retrieve Pileup Info
243  iEvent.getByToken(pileup_, pileup);
244  if( !pileup.isValid() ){
245  iEvent.getByToken(pileupMiniAOD_,pileup);
246  if( !pileup.isValid() )
247  throw cms::Exception(" Collection not found: ")
248  << " failed to find a standard AOD or miniAOD pileup collection " << std::endl;
249  }
250 
251  // Fill with true number of pileup
252  if(isMC_) {
253  for(const auto& pu : *pileup)
254  {
255  int bx = pu.getBunchCrossing();
256  if(bx == 0)
257  {
258  genNpu_ = pu.getPU_NumInteractions();
259  break;
260  }
261  }
262  }
263 
265 
266  // Retrieve the collection of particles from the event.
267  // If we fail to retrieve the collection with the standard AOD
268  // name, we next look for the one with the stndard miniAOD name.
269  iEvent.getByToken(src_, src);
270  if( !src.isValid() ){
271  iEvent.getByToken(srcMiniAOD_,src);
272  if( !src.isValid() )
273  throw cms::Exception(" Collection not found: ")
274  << " failed to find a standard AOD or miniAOD particle collection " << std::endl;
275  }
276 
277  // Get MVA decisions
279  for (size_t k = 0; k < nPhoMaps_; ++k) {
280  iEvent.getByToken(phoMapTokens_[k],decisions[k]);
281  }
282 
283  // Get MVA values
285  for (size_t k = 0; k < nValMaps_; ++k) {
286  iEvent.getByToken(valMapTokens_[k],values[k]);
287  }
288 
289  // Get MVA categories
291  for (size_t k = 0; k < nCats_; ++k) {
292  iEvent.getByToken(mvaCatTokens_[k],mvaCats[k]);
293  }
294 
295  int nPho = src->size();
296 
297  for(int iPho = 0; iPho < nPho; ++iPho) {
298 
299  const auto pho = src->ptrAt(iPho);
300 
301  if (pho->pt() < ptThreshold_) {
302  continue;
303  }
304 
305  //
306  // Look up and save the ID decisions
307  //
308  for (size_t k = 0; k < nPhoMaps_; ++k) {
309  mvaPasses_[k] = (int)(*decisions[k])[pho];
310  }
311 
312  for (size_t k = 0; k < nValMaps_; ++k) {
313  mvaValues_[k] = (*values[k])[pho];
314  }
315 
316  for (size_t k = 0; k < nCats_; ++k) {
317  mvaCats_[k] = (*mvaCats[k])[pho];
318  }
319 
320 
321  tree_->Fill();
322  }
323 
324 }
325 
326 // ------------ method called once each job just before starting event loop ------------
327 void
329 {
330 }
331 
332 // ------------ method called once each job just after ending the event loop ------------
333 void
335 {
336 }
337 
338 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
339 void
341 
343  desc.add<edm::InputTag>("src");
344  desc.add<edm::InputTag>("vertices");
345  desc.add<edm::InputTag>("pileup");
346  desc.add<edm::InputTag>("srcMiniAOD");
347  desc.add<edm::InputTag>("verticesMiniAOD");
348  desc.add<edm::InputTag>("pileupMiniAOD");
349  desc.addUntracked<std::vector<std::string>>("phoMVAs");
350  desc.addUntracked<std::vector<std::string>>("phoMVALabels");
351  desc.addUntracked<std::vector<std::string>>("phoMVAValMaps");
352  desc.addUntracked<std::vector<std::string>>("phoMVAValMapLabels");
353  desc.addUntracked<std::vector<std::string>>("phoMVACats");
354  desc.addUntracked<std::vector<std::string>>("phoMVACatLabels");
355  desc.add<bool>("isMC");
356  desc.add<double>("ptThreshold", 5.0);
357  descriptions.addDefault(desc);
358 
359 }
360 
361 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
size
Write out results.
static const std::string kSharedResource
Definition: TFileService.h:76
EventNumber_t event() const
Definition: EventID.h:41
const edm::EDGetToken src_
Definition: Photon.py:1
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
const edm::EDGetToken verticesMiniAOD_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:63
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
void endJob() override
const std::vector< std::string > valMapBranchNames_
std::vector< int > mvaCats_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const std::vector< std::string > phoMapTags_
const edm::EDGetToken pileupMiniAOD_
std::vector< int > mvaPasses_
int iEvent
Definition: GenABIO.cc:230
const std::vector< std::string > valMapTags_
void addDefault(ParameterSetDescription const &psetDescription)
std::vector< float > mvaValues_
std::vector< edm::EDGetTokenT< edm::ValueMap< int > > > mvaCatTokens_
void beginJob() override
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
const edm::EDGetToken pileup_
const edm::EDGetToken srcMiniAOD_
int k[5][pyjets_maxn]
const edm::EDGetToken vertices_
std::vector< edm::EDGetTokenT< edm::ValueMap< bool > > > phoMapTokens_
const std::vector< std::string > mvaCatBranchNames_
edm::EventID id() const
Definition: EventBase.h:60
fixed size matrix
HLT enums.
std::vector< edm::EDGetTokenT< edm::ValueMap< float > > > valMapTokens_
const std::vector< std::string > phoMapBranchNames_
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< float > vars_
PhotonMVANtuplizer(const edm::ParameterSet &)
const std::vector< std::string > mvaCatTags_