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 
35 
36 #include <TTree.h>
37 #include <TFile.h>
38 #include <Math/VectorUtil.h>
39 
40 //
41 // class declaration
42 //
43 
44 class PhotonMVANtuplizer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
45 public:
46  explicit PhotonMVANtuplizer(const edm::ParameterSet&);
47 
48  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
49 
50 private:
51  void analyze(const edm::Event&, const edm::EventSetup&) override;
52 
53  // ----------member data ---------------------------
54 
55  // other
56  TTree* tree_;
57 
58  // global variables
60  int genNpu_;
61  int vtxN_;
62 
63  // photon variables
64  double pT_, eta_;
65  std::vector<float> energyMatrix_;
66 
67  // photon genMatch variable
70 
71  // ID decisions objects
72  const std::vector<std::string> phoMapTags_;
73  std::vector<edm::EDGetTokenT<edm::ValueMap<bool>>> phoMapTokens_;
74  const std::vector<std::string> phoMapBranchNames_;
75  const size_t nPhoMaps_;
76 
77  // MVA values and categories (optional)
78  const std::vector<std::string> valMapTags_;
79  std::vector<edm::EDGetTokenT<edm::ValueMap<float>>> valMapTokens_;
80  const std::vector<std::string> valMapBranchNames_;
81  const size_t nValMaps_;
82 
83  const std::vector<std::string> mvaCatTags_;
84  std::vector<edm::EDGetTokenT<edm::ValueMap<int>>> mvaCatTokens_;
85  const std::vector<std::string> mvaCatBranchNames_;
86  const size_t nCats_;
87 
88  // config
89  const bool isMC_;
90  const double ptThreshold_;
91  const double deltaR_;
92 
93  // Tokens
100 
101  // to hold ID decisions and categories
102  std::vector<int> mvaPasses_;
103  std::vector<float> mvaValues_;
104  std::vector<int> mvaCats_;
105 
106  // To get the auxiliary MVA variables
108 
109  // To manage the variables which are parsed from the text file
111 
112  const int nVars_;
113  std::vector<float> vars_;
114 
115  const bool doEnergyMatrix_;
116  const int energyMatrixSize_;
117 };
118 
123 };
124 
125 namespace {
126 
127  int matchToTruth(const reco::Photon& ph, const edm::View<reco::GenParticle>& genParticles, double deltaR) {
128  // Find the closest status 1 gen photon to the reco photon
129  double dR = 999;
130  reco::GenParticle const* closestPhoton = &genParticles[0];
131  for (auto& particle : genParticles) {
132  // Drop everything that is not photon or not status 1
133  if (abs(particle.pdgId()) != 22 || particle.status() != 1)
134  continue;
135 
136  double dRtmp = ROOT::Math::VectorUtil::DeltaR(ph.p4(), particle.p4());
137  if (dRtmp < dR) {
138  dR = dRtmp;
139  closestPhoton = &particle;
140  }
141  }
142  // See if the closest photon (if it exists) is close enough.
143  // If not, no match found.
144  if (dR < deltaR) {
145  if (closestPhoton->isPromptFinalState())
146  return TRUE_PROMPT_PHOTON;
147  else
148  return TRUE_NON_PROMPT_PHOTON;
149  }
150  return FAKE_PHOTON;
151  }
152 
153 }; // namespace
154 
155 // constructor
157  : phoMapTags_(iConfig.getParameter<std::vector<std::string>>("phoMVAs")),
158  phoMapBranchNames_(iConfig.getParameter<std::vector<std::string>>("phoMVALabels")),
159  nPhoMaps_(phoMapBranchNames_.size()),
160  valMapTags_(iConfig.getParameter<std::vector<std::string>>("phoMVAValMaps")),
161  valMapBranchNames_(iConfig.getParameter<std::vector<std::string>>("phoMVAValMapLabels")),
162  nValMaps_(valMapBranchNames_.size()),
163  mvaCatTags_(iConfig.getParameter<std::vector<std::string>>("phoMVACats")),
164  mvaCatBranchNames_(iConfig.getParameter<std::vector<std::string>>("phoMVACatLabels")),
165  nCats_(mvaCatBranchNames_.size()),
166  isMC_(iConfig.getParameter<bool>("isMC")),
167  ptThreshold_(iConfig.getParameter<double>("ptThreshold")),
168  deltaR_(iConfig.getParameter<double>("deltaR")),
169  src_(consumes<edm::View<reco::Photon>>(iConfig.getParameter<edm::InputTag>("src"))),
170  vertices_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vertices"))),
171  pileup_(consumes<std::vector<PileupSummaryInfo>>(iConfig.getParameter<edm::InputTag>("pileup"))),
172  genParticles_(consumes<edm::View<reco::GenParticle>>(iConfig.getParameter<edm::InputTag>("genParticles"))),
173  ebRecHits_(consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("ebReducedRecHitCollection"))),
174  eeRecHits_(consumes<EcalRecHitCollection>(iConfig.getParameter<edm::InputTag>("eeReducedRecHitCollection"))),
175  mvaPasses_(nPhoMaps_),
176  mvaValues_(nValMaps_),
177  mvaCats_(nCats_),
178  variableHelper_(consumesCollector()),
179  mvaVarMngr_(iConfig.getParameter<std::string>("variableDefinition")),
180  nVars_(mvaVarMngr_.getNVars()),
181  vars_(nVars_),
182  doEnergyMatrix_(iConfig.getParameter<bool>("doEnergyMatrix")),
183  energyMatrixSize_(iConfig.getParameter<int>("energyMatrixSize")) {
184  // phoMaps
185  for (auto const& tag : phoMapTags_) {
187  }
188  // valMaps
189  for (auto const& tag : valMapTags_) {
191  }
192  // categories
193  for (auto const& tag : mvaCatTags_) {
195  }
196 
197  // Book tree
198  usesResource(TFileService::kSharedResource);
200  tree_ = fs->make<TTree>("tree", "tree");
201 
202  tree_->Branch("nEvent", &nEvent_);
203  tree_->Branch("nRun", &nRun_);
204  tree_->Branch("nLumi", &nLumi_);
205  if (isMC_) {
206  tree_->Branch("genNpu", &genNpu_);
207  tree_->Branch("matchedToGenPh", &matchedToGenPh_);
208  }
209  tree_->Branch("vtxN", &vtxN_);
210  tree_->Branch("pT", &pT_);
211  tree_->Branch("eta", &eta_);
212 
213  if (doEnergyMatrix_)
214  tree_->Branch("energyMatrix", &energyMatrix_);
215 
216  for (int i = 0; i < nVars_; ++i) {
217  tree_->Branch(mvaVarMngr_.getName(i).c_str(), &vars_[i]);
218  }
219 
220  // IDs
221  for (size_t k = 0; k < nValMaps_; ++k) {
222  tree_->Branch(valMapBranchNames_[k].c_str(), &mvaValues_[k]);
223  }
224 
225  for (size_t k = 0; k < nPhoMaps_; ++k) {
226  tree_->Branch(phoMapBranchNames_[k].c_str(), &mvaPasses_[k]);
227  }
228 
229  for (size_t k = 0; k < nCats_; ++k) {
230  tree_->Branch(mvaCatBranchNames_[k].c_str(), &mvaCats_[k]);
231  }
232 }
233 
234 // ------------ method called for each event ------------
236  // Fill global event info
237  nEvent_ = iEvent.id().event();
238  nRun_ = iEvent.id().run();
239  nLumi_ = iEvent.luminosityBlock();
240 
241  // Get Handles
242  auto src = iEvent.getHandle(src_);
243  auto vertices = iEvent.getHandle(vertices_);
244  auto pileup = iEvent.getHandle(pileup_);
245  auto genParticles = iEvent.getHandle(genParticles_);
246 
247  vtxN_ = vertices->size();
248 
249  // initialize cluster tools
250  std::unique_ptr<noZS::EcalClusterLazyTools> lazyTools;
251  if (doEnergyMatrix_) {
252  // Configure Lazy Tools, which will compute 5x5 quantities
253  lazyTools = std::make_unique<noZS::EcalClusterLazyTools>(iEvent, iSetup, ebRecHits_, eeRecHits_);
254  }
255 
256  // Fill with true number of pileup
257  if (isMC_) {
258  for (const auto& pu : *pileup) {
259  int bx = pu.getBunchCrossing();
260  if (bx == 0) {
261  genNpu_ = pu.getPU_NumInteractions();
262  break;
263  }
264  }
265  }
266 
267  // Get MVA decisions
269  for (size_t k = 0; k < nPhoMaps_; ++k) {
270  iEvent.getByToken(phoMapTokens_[k], decisions[k]);
271  }
272 
273  // Get MVA values
275  for (size_t k = 0; k < nValMaps_; ++k) {
276  iEvent.getByToken(valMapTokens_[k], values[k]);
277  }
278 
279  // Get MVA categories
281  for (size_t k = 0; k < nCats_; ++k) {
282  iEvent.getByToken(mvaCatTokens_[k], mvaCats[k]);
283  }
284 
285  std::vector<float> extraVariables = variableHelper_.getAuxVariables(iEvent);
286 
287  for (auto const& pho : src->ptrs()) {
288  if (pho->pt() < ptThreshold_)
289  continue;
290 
291  pT_ = pho->pt();
292  eta_ = pho->eta();
293 
294  // Fill the energy matrix around the seed
295  if (doEnergyMatrix_) {
296  const auto& seed = *(pho->superCluster()->seed());
297  energyMatrix_ = lazyTools->energyMatrix(seed, energyMatrixSize_);
298  }
299 
300  // variables from the text file
301  for (int iVar = 0; iVar < nVars_; ++iVar) {
302  vars_[iVar] = mvaVarMngr_.getValue(iVar, *pho, extraVariables);
303  }
304 
305  if (isMC_)
306  matchedToGenPh_ = matchToTruth(*pho, *genParticles, deltaR_);
307 
308  //
309  // Look up and save the ID decisions
310  //
311  for (size_t k = 0; k < nPhoMaps_; ++k)
312  mvaPasses_[k] = static_cast<int>((*decisions[k])[pho]);
313  for (size_t k = 0; k < nValMaps_; ++k)
314  mvaValues_[k] = (*values[k])[pho];
315  for (size_t k = 0; k < nCats_; ++k)
316  mvaCats_[k] = (*mvaCats[k])[pho];
317 
318  tree_->Fill();
319  }
320 }
321 
322 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
325  desc.add<edm::InputTag>("src", edm::InputTag("slimmedPhotons"));
326  desc.add<edm::InputTag>("vertices", edm::InputTag("offlineSlimmedPrimaryVertices"));
327  desc.add<edm::InputTag>("pileup", edm::InputTag("slimmedAddPileupInfo"));
328  desc.add<edm::InputTag>("genParticles", edm::InputTag("prunedGenParticles"));
329  desc.add<edm::InputTag>("ebReducedRecHitCollection", edm::InputTag("reducedEgamma", "reducedEBRecHits"));
330  desc.add<edm::InputTag>("eeReducedRecHitCollection", edm::InputTag("reducedEgamma", "reducedEERecHits"));
331  desc.add<std::vector<std::string>>("phoMVAs", {});
332  desc.add<std::vector<std::string>>("phoMVALabels", {});
333  desc.add<std::vector<std::string>>("phoMVAValMaps", {});
334  desc.add<std::vector<std::string>>("phoMVAValMapLabels", {});
335  desc.add<std::vector<std::string>>("phoMVACats", {});
336  desc.add<std::vector<std::string>>("phoMVACatLabels", {});
337  desc.add<bool>("doEnergyMatrix", false);
338  desc.add<int>("energyMatrixSize", 2)->setComment("extension of crystals in each direction away from the seed");
339  desc.add<bool>("isMC", true);
340  desc.add<double>("ptThreshold", 15.0);
341  desc.add<double>("deltaR", 0.1);
342  desc.add<std::string>("variableDefinition");
343  descriptions.addDefault(desc);
344 }
345 
346 //define this as a plug-in
PhotonMVANtuplizer::nRun_
int nRun_
Definition: PhotonMVANtuplizer.cc:59
PhotonMVANtuplizer::mvaPasses_
std::vector< int > mvaPasses_
Definition: PhotonMVANtuplizer.cc:102
PileupSummaryInfo.h
PhotonMVANtuplizer::pT_
double pT_
Definition: PhotonMVANtuplizer.cc:64
electrons_cff.bool
bool
Definition: electrons_cff.py:372
EDAnalyzer.h
mps_fire.i
i
Definition: mps_fire.py:355
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
genParticles2HepMC_cfi.genParticles
genParticles
Definition: genParticles2HepMC_cfi.py:4
PhotonMVANtuplizer::genNpu_
int genNpu_
Definition: PhotonMVANtuplizer.cc:60
MVAVariableManager::getValue
float getValue(int index, const ParticleType &particle, const std::vector< float > &auxVariables) const
Definition: MVAVariableManager.h:51
PhotonMVANtuplizer::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: PhotonMVANtuplizer.cc:235
sistrip::View
View
Definition: ConstantsForView.h:26
reco::GenParticle
Definition: GenParticle.h:21
edm::EDGetTokenT
Definition: EDGetToken.h:33
PhotonMVANtuplizer::pileup_
const edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileup_
Definition: PhotonMVANtuplizer.cc:96
edm
HLT enums.
Definition: AlignableModifier.h:19
TRUE_PROMPT_PHOTON
Definition: PhotonMVANtuplizer.cc:121
Photon.h
l1GtPatternGenerator_cfi.bx
bx
Definition: l1GtPatternGenerator_cfi.py:18
reco::Photon::p4
const LorentzVector & p4(P4type type) const
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
PhotonMVANtuplizer::nLumi_
int nLumi_
Definition: PhotonMVANtuplizer.cc:59
PhotonMVANtuplizer::vertices_
const edm::EDGetTokenT< std::vector< reco::Vertex > > vertices_
Definition: PhotonMVANtuplizer.cc:95
edm::SortedCollection< EcalRecHit >
EcalClusterLazyTools.h
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
PhotonMVANtuplizer::isMC_
const bool isMC_
Definition: PhotonMVANtuplizer.cc:89
edm::Handle
Definition: AssociativeIterator.h:50
GenParticle
Definition: GenParticle.py:1
MVAVariableManager::getName
const std::string & getName(int index) const
Definition: MVAVariableManager.h:47
TRUE_NON_PROMPT_PHOTON
Definition: PhotonMVANtuplizer.cc:122
FAKE_PHOTON
Definition: PhotonMVANtuplizer.cc:120
MakerMacros.h
Photon.h
PhotonMVANtuplizer::mvaVarMngr_
MVAVariableManager< reco::Photon > mvaVarMngr_
Definition: PhotonMVANtuplizer.cc:110
PhotonMVANtuplizer::energyMatrix_
std::vector< float > energyMatrix_
Definition: PhotonMVANtuplizer.cc:65
PhotonMVANtuplizer::vars_
std::vector< float > vars_
Definition: PhotonMVANtuplizer.cc:113
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
MVAVariableHelper::getAuxVariables
const std::vector< float > getAuxVariables(const edm::Event &iEvent) const
Definition: MVAVariableHelper.cc:7
Service.h
mixOne_premix_on_sim_cfi.pileup
pileup
Definition: mixOne_premix_on_sim_cfi.py:42
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
MVAVariableManager.h
PhotonMVANtuplizer::energyMatrixSize_
const int energyMatrixSize_
Definition: PhotonMVANtuplizer.cc:116
PhotonMVANtuplizer::mvaCats_
std::vector< int > mvaCats_
Definition: PhotonMVANtuplizer.cc:104
PhotonMVANtuplizer::valMapBranchNames_
const std::vector< std::string > valMapBranchNames_
Definition: PhotonMVANtuplizer.cc:80
PhotonMVANtuplizer::nVars_
const int nVars_
Definition: PhotonMVANtuplizer.cc:112
PhotonMVANtuplizer::mvaCatTokens_
std::vector< edm::EDGetTokenT< edm::ValueMap< int > > > mvaCatTokens_
Definition: PhotonMVANtuplizer.cc:84
PhotonMVANtuplizer::PhotonMVANtuplizer
PhotonMVANtuplizer(const edm::ParameterSet &)
Definition: PhotonMVANtuplizer.cc:156
dqmdumpme.k
k
Definition: dqmdumpme.py:60
EDGetToken.h
PhotonMVANtuplizer::nValMaps_
const size_t nValMaps_
Definition: PhotonMVANtuplizer.cc:81
PhotonMVANtuplizer::phoMapBranchNames_
const std::vector< std::string > phoMapBranchNames_
Definition: PhotonMVANtuplizer.cc:74
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Vertex.h
PhotonMVANtuplizer::valMapTokens_
std::vector< edm::EDGetTokenT< edm::ValueMap< float > > > valMapTokens_
Definition: PhotonMVANtuplizer.cc:79
TFileService.h
edm::View
Definition: CaloClusterFwd.h:14
PhotonMVANtuplizer::vtxN_
int vtxN_
Definition: PhotonMVANtuplizer.cc:61
PhotonMVANtuplizer::phoMapTokens_
std::vector< edm::EDGetTokenT< edm::ValueMap< bool > > > phoMapTokens_
Definition: PhotonMVANtuplizer.cc:73
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
TrackRefitter_38T_cff.src
src
Definition: TrackRefitter_38T_cff.py:24
PhotonMVANtuplizer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: PhotonMVANtuplizer.cc:323
Event.h
PhotonMVANtuplizer::ebRecHits_
const edm::EDGetTokenT< EcalRecHitCollection > ebRecHits_
Definition: PhotonMVANtuplizer.cc:98
PhotonMVANtuplizer::mvaCatTags_
const std::vector< std::string > mvaCatTags_
Definition: PhotonMVANtuplizer.cc:83
Photon
Definition: Photon.py:1
edm::Service< TFileService >
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
PhotonMVANtuplizer
Definition: PhotonMVANtuplizer.cc:44
PhotonMVANtuplizer::genParticles_
const edm::EDGetTokenT< edm::View< reco::GenParticle > > genParticles_
Definition: PhotonMVANtuplizer.cc:97
PhotonMVANtuplizer::doEnergyMatrix_
const bool doEnergyMatrix_
Definition: PhotonMVANtuplizer.cc:115
edm::EventSetup
Definition: EventSetup.h:57
electronAnalyzer_cfi.DeltaR
DeltaR
Definition: electronAnalyzer_cfi.py:33
PhotonMVANtuplizer::phoMapTags_
const std::vector< std::string > phoMapTags_
Definition: PhotonMVANtuplizer.cc:72
PhotonMVANtuplizer::nEvent_
int nEvent_
Definition: PhotonMVANtuplizer.cc:59
PhotonMVANtuplizer::matchedGenIdx_
int matchedGenIdx_
Definition: PhotonMVANtuplizer.cc:69
InputTag.h
VertexFwd.h
reco::Photon
Definition: Photon.h:21
PhotonMatchType
PhotonMatchType
Definition: PhotonMVANtuplizer.cc:119
PhotonMVANtuplizer::variableHelper_
const MVAVariableHelper variableHelper_
Definition: PhotonMVANtuplizer.cc:107
nVars_
constexpr int nVars_
Definition: PhotonIDValueMapProducer.cc:120
std
Definition: JetResolutionObject.h:76
MVAVariableManager< reco::Photon >
HltBtagValidation_cff.Vertex
Vertex
Definition: HltBtagValidation_cff.py:32
PhotonMVANtuplizer::ptThreshold_
const double ptThreshold_
Definition: PhotonMVANtuplizer.cc:90
PhotonMVANtuplizer::deltaR_
const double deltaR_
Definition: PhotonMVANtuplizer.cc:91
Frameworkfwd.h
edm::ValueMap
Definition: ValueMap.h:107
TFileService::kSharedResource
static const std::string kSharedResource
Definition: TFileService.h:76
PhotonMVANtuplizer::nPhoMaps_
const size_t nPhoMaps_
Definition: PhotonMVANtuplizer.cc:75
muons2muons_cfi.pu
pu
Definition: muons2muons_cfi.py:31
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
HGC3DClusterGenMatchSelector_cfi.dR
dR
Definition: HGC3DClusterGenMatchSelector_cfi.py:7
ParameterSet.h
edm::EDConsumerBase::consumes
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: EDConsumerBase.h:126
PhotonMVANtuplizer::matchedToGenPh_
int matchedToGenPh_
Definition: PhotonMVANtuplizer.cc:68
PhotonMVANtuplizer::src_
const edm::EDGetTokenT< edm::View< reco::Photon > > src_
Definition: PhotonMVANtuplizer.cc:94
PhotonMVANtuplizer::eeRecHits_
const edm::EDGetTokenT< EcalRecHitCollection > eeRecHits_
Definition: PhotonMVANtuplizer.cc:99
edm::Event
Definition: Event.h:73
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
PhotonMVANtuplizer::valMapTags_
const std::vector< std::string > valMapTags_
Definition: PhotonMVANtuplizer.cc:78
PhotonMVANtuplizer::nCats_
const size_t nCats_
Definition: PhotonMVANtuplizer.cc:86
PhotonMVANtuplizer::tree_
TTree * tree_
Definition: PhotonMVANtuplizer.cc:56
PhotonMVANtuplizer::eta_
double eta_
Definition: PhotonMVANtuplizer.cc:64
MVAVariableHelper
Definition: MVAVariableHelper.h:23
edm::InputTag
Definition: InputTag.h:15
reco::GenParticle::isPromptFinalState
bool isPromptFinalState() const
Definition: GenParticle.h:51
PileupSummaryInfo
Definition: PileupSummaryInfo.h:22
SurveyInfoScenario_cff.seed
seed
Definition: SurveyInfoScenario_cff.py:295
TFileService::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileService.h:64
PhotonMVANtuplizer::mvaValues_
std::vector< float > mvaValues_
Definition: PhotonMVANtuplizer.cc:103
PhotonMVANtuplizer::mvaCatBranchNames_
const std::vector< std::string > mvaCatBranchNames_
Definition: PhotonMVANtuplizer.cc:85
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
pwdgSkimBPark_cfi.vertices
vertices
Definition: pwdgSkimBPark_cfi.py:7