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 
37 
41 
42 #include <TTree.h>
43 #include <TFile.h>
44 #include <Math/VectorUtil.h>
45 
46 //
47 // class declaration
48 //
49 
50 // If the analyzer does not use TFileService, please remove
51 // the template argument to the base class so the class inherits
52 // from edm::one::EDAnalyzer<>
53 // This will improve performance in multithreaded jobs.
54 //
55 
56 class PhotonMVANtuplizer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
57 
58  public:
59  explicit PhotonMVANtuplizer(const edm::ParameterSet&);
60  ~PhotonMVANtuplizer() override;
61 
62  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
63 
64 
65  private:
66  void analyze(const edm::Event&, const edm::EventSetup&) override;
67 
68  // method called once each job just before starting event loop
69  void beginJob() override {};
70  // method called once each job just after ending the event loop
71  void endJob() override {};
72 
73  // ----------member data ---------------------------
74 
75  // other
76  TTree* tree_;
77 
78  std::vector<float> vars_;
79  int nVars_;
80 
81  //global variables
83  int genNpu_;
84  int vtxN_;
85 
86  // to hold ID decisions and categories
87  std::vector<int> mvaPasses_;
88  std::vector<float> mvaValues_;
89  std::vector<int> mvaCats_;
90 
91  // ID decisions objects
92  const std::vector< std::string > phoMapTags_;
93  std::vector< edm::EDGetTokenT< edm::ValueMap<bool> > > phoMapTokens_;
94  const std::vector< std::string > phoMapBranchNames_;
95  const size_t nPhoMaps_;
96 
97  // MVA values and categories (optional)
98  const std::vector< std::string > valMapTags_;
99  std::vector< edm::EDGetTokenT<edm::ValueMap<float> > > valMapTokens_;
100  const std::vector< std::string > valMapBranchNames_;
101  const size_t nValMaps_;
102 
103  const std::vector< std::string > mvaCatTags_;
104  std::vector< edm::EDGetTokenT<edm::ValueMap<int> > > mvaCatTokens_;
105  const std::vector< std::string > mvaCatBranchNames_;
106  const size_t nCats_;
107 
108  // config
109  const bool isMC_;
110  const double ptThreshold_;
111 
112  // for AOD or MiniAOD case
116 
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  : phoMapTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAs"))
132  , phoMapBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVALabels"))
134  , valMapTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAValMaps"))
135  , valMapBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVAValMapLabels"))
137  , mvaCatTags_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVACats"))
138  , mvaCatBranchNames_ (iConfig.getUntrackedParameter<std::vector<std::string>>("phoMVACatLabels"))
140  , isMC_ (iConfig.getParameter<bool>("isMC"))
141  , ptThreshold_ (iConfig.getParameter<double>("ptThreshold"))
142  , src_ (consumesCollector(), iConfig, "src" , "srcMiniAOD")
143  , vertices_ (src_, consumesCollector(), iConfig, "vertices", "verticesMiniAOD")
144  , pileup_ (src_, consumesCollector(), iConfig, "pileup" , "pileupMiniAOD")
145 {
146  // phoMaps
147  for (size_t k = 0; k < nPhoMaps_; ++k) {
148 
150 
151  // Initialize vectors for holding ID decisions
152  mvaPasses_.push_back(0);
153  }
154 
155  // valMaps
156  for (size_t k = 0; k < nValMaps_; ++k) {
158 
159  // Initialize vectors for holding MVA values
160  mvaValues_.push_back(0.0);
161  }
162 
163  // categories
164  for (size_t k = 0; k < nCats_; ++k) {
166 
167  // Initialize vectors for holding MVA values
168  mvaCats_.push_back(0);
169  }
170 
171  // Book tree
172  usesResource(TFileService::kSharedResource);
174  tree_ = fs->make<TTree>("tree","tree");
175 
176  tree_->Branch("nEvent", &nEvent_);
177  tree_->Branch("nRun", &nRun_);
178  tree_->Branch("nLumi", &nLumi_);
179  if (isMC_) tree_->Branch("genNpu", &genNpu_);
180  tree_->Branch("vtxN", &vtxN_);
181 
182  // Has to be in two different loops
183  for (int i = 0; i < nVars_; ++i) {
184  vars_.push_back(0.0);
185  }
186 
187  // IDs
188  for (size_t k = 0; k < nValMaps_; ++k) {
189  tree_->Branch(valMapBranchNames_[k].c_str() , &mvaValues_[k]);
190  }
191 
192  for (size_t k = 0; k < nPhoMaps_; ++k) {
193  tree_->Branch(phoMapBranchNames_[k].c_str() , &mvaPasses_[k]);
194  }
195 
196  for (size_t k = 0; k < nCats_; ++k) {
197  tree_->Branch(mvaCatBranchNames_[k].c_str() , &mvaCats_[k]);
198  }
199 }
200 
201 
203 {
204 
205  // do anything here that needs to be done at desctruction time
206  // (e.g. close files, deallocate resources etc.)
207 
208 }
209 
210 
211 //
212 // member functions
213 //
214 
215 // ------------ method called for each event ------------
216 void
218 {
219  // Fill global event info
220  nEvent_ = iEvent.id().event();
221  nRun_ = iEvent.id().run();
222  nLumi_ = iEvent.luminosityBlock();
223 
224  // Get Handles
225  auto src = src_.getValidHandle(iEvent);
226  auto vertices = vertices_.getValidHandle(iEvent);
227  auto pileup = pileup_.getValidHandle(iEvent);
228 
229  vtxN_ = vertices->size();
230 
231  // Fill with true number of pileup
232  if(isMC_) {
233  for(const auto& pu : *pileup)
234  {
235  int bx = pu.getBunchCrossing();
236  if(bx == 0)
237  {
238  genNpu_ = pu.getPU_NumInteractions();
239  break;
240  }
241  }
242  }
243 
244  // Get MVA decisions
246  for (size_t k = 0; k < nPhoMaps_; ++k) {
247  iEvent.getByToken(phoMapTokens_[k],decisions[k]);
248  }
249 
250  // Get MVA values
252  for (size_t k = 0; k < nValMaps_; ++k) {
253  iEvent.getByToken(valMapTokens_[k],values[k]);
254  }
255 
256  // Get MVA categories
258  for (size_t k = 0; k < nCats_; ++k) {
259  iEvent.getByToken(mvaCatTokens_[k],mvaCats[k]);
260  }
261 
262  int nPho = src->size();
263 
264  for(int iPho = 0; iPho < nPho; ++iPho) {
265 
266  const auto pho = src->ptrAt(iPho);
267 
268  if (pho->pt() < ptThreshold_) {
269  continue;
270  }
271 
272  //
273  // Look up and save the ID decisions
274  //
275  for (size_t k = 0; k < nPhoMaps_; ++k) {
276  mvaPasses_[k] = (int)(*decisions[k])[pho];
277  }
278 
279  for (size_t k = 0; k < nValMaps_; ++k) {
280  mvaValues_[k] = (*values[k])[pho];
281  }
282 
283  for (size_t k = 0; k < nCats_; ++k) {
284  mvaCats_[k] = (*mvaCats[k])[pho];
285  }
286 
287 
288  tree_->Fill();
289  }
290 
291 }
292 
293 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
294 void
296 
298  desc.add<edm::InputTag>("src");
299  desc.add<edm::InputTag>("vertices");
300  desc.add<edm::InputTag>("pileup");
301  desc.add<edm::InputTag>("srcMiniAOD");
302  desc.add<edm::InputTag>("verticesMiniAOD");
303  desc.add<edm::InputTag>("pileupMiniAOD");
304  desc.addUntracked<std::vector<std::string>>("phoMVAs");
305  desc.addUntracked<std::vector<std::string>>("phoMVALabels");
306  desc.addUntracked<std::vector<std::string>>("phoMVAValMaps");
307  desc.addUntracked<std::vector<std::string>>("phoMVAValMapLabels");
308  desc.addUntracked<std::vector<std::string>>("phoMVACats");
309  desc.addUntracked<std::vector<std::string>>("phoMVACatLabels");
310  desc.add<bool>("isMC");
311  desc.add<double>("ptThreshold", 5.0);
312  descriptions.addDefault(desc);
313 
314 }
315 
316 //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
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
MultiTokenT< std::vector< PileupSummaryInfo > > pileup_
MultiTokenT< edm::View< reco::Photon > > src_
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_
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_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
std::vector< edm::EDGetTokenT< edm::ValueMap< int > > > mvaCatTokens_
void beginJob() override
ParameterDescriptionBase * add(U const &iLabel, T const &value)
MultiTokenT< std::vector< reco::Vertex > > vertices_
int k[5][pyjets_maxn]
edm::Handle< T > getValidHandle(const edm::Event &iEvent)
Definition: MultiToken.h:83
std::vector< edm::EDGetTokenT< edm::ValueMap< bool > > > phoMapTokens_
const std::vector< std::string > mvaCatBranchNames_
edm::EventID id() const
Definition: EventBase.h:60
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_