CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATPhotonProducer.cc
Go to the documentation of this file.
1 //
2 //
3 
9 
12 
13 #include <memory>
14 
15 using namespace pat;
16 
18  isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), consumesCollector(), false) ,
19  useUserData_(iConfig.exists("userData"))
20 {
21  // initialize the configurables
22  photonToken_ = consumes<edm::View<reco::Photon> >(iConfig.getParameter<edm::InputTag>("photonSource"));
23  embedSuperCluster_ = iConfig.getParameter<bool>("embedSuperCluster");
24  // MC matching configurables
25  addGenMatch_ = iConfig.getParameter<bool>( "addGenMatch" );
26  if (addGenMatch_) {
27  embedGenMatch_ = iConfig.getParameter<bool>( "embedGenMatch" );
28  if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
30  }
31  else {
32  genMatchTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" ), [this](edm::InputTag const & tag){return consumes<edm::Association<reco::GenParticleCollection> >(tag);});
33  }
34  }
35  // Efficiency configurables
36  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
37  if (addEfficiencies_) {
38  efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
39  }
40  // photon ID configurables
41  addPhotonID_ = iConfig.getParameter<bool>( "addPhotonID" );
42  if (addPhotonID_) {
43  // it might be a single photon ID
44  if (iConfig.existsAs<edm::InputTag>("photonIDSource")) {
45  photIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("photonIDSource")));
46  }
47  // or there might be many of them
48  if (iConfig.existsAs<edm::ParameterSet>("photonIDSources")) {
49  // please don't configure me twice
50  if (!photIDSrcs_.empty()){
51  throw cms::Exception("Configuration") << "PATPhotonProducer: you can't specify both 'photonIDSource' and 'photonIDSources'\n";
52  }
53  // read the different photon ID names
54  edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("photonIDSources");
55  std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
56  for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
57  photIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
58  }
59  }
60  // but in any case at least once
61  if (photIDSrcs_.empty()) throw cms::Exception("Configuration") <<
62  "PATPhotonProducer: id addPhotonID is true, you must specify either:\n" <<
63  "\tInputTag photonIDSource = <someTag>\n" << "or\n" <<
64  "\tPSet photonIDSources = { \n" <<
65  "\t\tInputTag <someName> = <someTag> // as many as you want \n " <<
66  "\t}\n";
67  }
68  photIDTokens_ = edm::vector_transform(photIDSrcs_, [this](NameTag const & tag){return mayConsume<edm::ValueMap<Bool_t> >(tag.second);});
69  // Resolution configurables
70  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
71  if (addResolutions_) {
72  resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
73  }
74  // Check to see if the user wants to add user data
75  if ( useUserData_ ) {
76  userDataHelper_ = PATUserDataHelper<Photon>(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector());
77  }
78  // produces vector of photons
79  produces<std::vector<Photon> >();
80 
81  // read isoDeposit labels, for direct embedding
82  readIsolationLabels(iConfig, "isoDeposits", isoDepositLabels_, isoDepositTokens_);
83  // read isolation value labels, for direct embedding
84  readIsolationLabels(iConfig, "isolationValues", isolationValueLabels_, isolationValueTokens_);
85 
86 }
87 
88 PATPhotonProducer::~PATPhotonProducer() {
89 }
90 
92 {
93  // switch off embedding (in unschedules mode)
94  if (iEvent.isRealData()){
95  addGenMatch_ = false;
96  embedGenMatch_ = false;
97  }
98 
99  // Get the vector of Photon's from the event
101  iEvent.getByToken(photonToken_, photons);
102 
103  // prepare the MC matching
104  std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > >genMatches(genMatchTokens_.size());
105  if (addGenMatch_) {
106  for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
107  iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
108  }
109  }
110 
111  if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
112 
114  if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
115 
116  IsoDepositMaps deposits(isoDepositTokens_.size());
117  for (size_t j = 0, nd = isoDepositTokens_.size(); j < nd; ++j) {
118  iEvent.getByToken(isoDepositTokens_[j], deposits[j]);
119  }
120 
121  IsolationValueMaps isolationValues(isolationValueTokens_.size());
122  for (size_t j = 0; j<isolationValueTokens_.size(); ++j) {
123  iEvent.getByToken(isolationValueTokens_[j], isolationValues[j]);
124  }
125 
126 
127  // prepare ID extraction
128  std::vector<edm::Handle<edm::ValueMap<Bool_t> > > idhandles;
129  std::vector<pat::Photon::IdPair> ids;
130  if (addPhotonID_) {
131  idhandles.resize(photIDSrcs_.size());
132  ids.resize(photIDSrcs_.size());
133  for (size_t i = 0; i < photIDSrcs_.size(); ++i) {
134  iEvent.getByToken(photIDTokens_[i], idhandles[i]);
135  ids[i].first = photIDSrcs_[i].first;
136  }
137  }
138 
139  // loop over photons
140  std::vector<Photon> * PATPhotons = new std::vector<Photon>();
141  for (edm::View<reco::Photon>::const_iterator itPhoton = photons->begin(); itPhoton != photons->end(); itPhoton++) {
142  // construct the Photon from the ref -> save ref to original object
143  unsigned int idx = itPhoton - photons->begin();
144  edm::RefToBase<reco::Photon> photonRef = photons->refAt(idx);
145  edm::Ptr<reco::Photon> photonPtr = photons->ptrAt(idx);
146  Photon aPhoton(photonRef);
147  if (embedSuperCluster_) aPhoton.embedSuperCluster();
148 
149  // store the match to the generated final state muons
150  if (addGenMatch_) {
151  for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
152  reco::GenParticleRef genPhoton = (*genMatches[i])[photonRef];
153  aPhoton.addGenParticleRef(genPhoton);
154  }
155  if (embedGenMatch_) aPhoton.embedGenParticle();
156  }
157 
158  if (efficiencyLoader_.enabled()) {
159  efficiencyLoader_.setEfficiencies( aPhoton, photonRef );
160  }
161 
162  if (resolutionLoader_.enabled()) {
164  }
165 
166  // here comes the extra functionality
167  if (isolator_.enabled()) {
168  isolator_.fill(*photons, idx, isolatorTmpStorage_);
169  typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
170  // better to loop backwards, so the vector is resized less times
171  for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
172  aPhoton.setIsolation(it->first, it->second);
173  }
174  }
175 
176  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
177  aPhoton.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[photonRef]);
178  }
179 
180  for (size_t j = 0; j<isolationValues.size(); ++j) {
181  aPhoton.setIsolation(isolationValueLabels_[j].first,(*isolationValues[j])[photonRef]);
182  }
183 
184  // add photon ID info
185  if (addPhotonID_) {
186  for (size_t i = 0; i < photIDSrcs_.size(); ++i) {
187  ids[i].second = (*idhandles[i])[photonRef];
188  }
189  aPhoton.setPhotonIDs(ids);
190  }
191 
192  if ( useUserData_ ) {
193  userDataHelper_.add( aPhoton, iEvent, iSetup );
194  }
195 
196 
197  // add the Photon to the vector of Photons
198  PATPhotons->push_back(aPhoton);
199  }
200 
201  // sort Photons in ET
202  std::sort(PATPhotons->begin(), PATPhotons->end(), eTComparator_);
203 
204  // put genEvt object in Event
205  std::auto_ptr<std::vector<Photon> > myPhotons(PATPhotons);
206  iEvent.put(myPhotons);
208 
209 }
210 
211 // ParameterSet description for module
213 {
215  iDesc.setComment("PAT photon producer module");
216 
217  // input source
218  iDesc.add<edm::InputTag>("photonSource", edm::InputTag("no default"))->setComment("input collection");
219 
220  iDesc.add<bool>("embedSuperCluster", true)->setComment("embed external super cluster");
221 
222  // MC matching configurables
223  iDesc.add<bool>("addGenMatch", true)->setComment("add MC matching");
224  iDesc.add<bool>("embedGenMatch", false)->setComment("embed MC matched MC information");
225  std::vector<edm::InputTag> emptySourceVector;
226  iDesc.addNode( edm::ParameterDescription<edm::InputTag>("genParticleMatch", edm::InputTag(), true) xor
227  edm::ParameterDescription<std::vector<edm::InputTag> >("genParticleMatch", emptySourceVector, true)
228  )->setComment("input with MC match information");
229 
231 
232  // photon ID configurables
233  iDesc.add<bool>("addPhotonID",true)->setComment("add photon ID variables");
234  edm::ParameterSetDescription photonIDSourcesPSet;
235  photonIDSourcesPSet.setAllowAnything();
236  iDesc.addNode( edm::ParameterDescription<edm::InputTag>("photonIDSource", edm::InputTag(), true) xor
237  edm::ParameterDescription<edm::ParameterSetDescription>("photonIDSources", photonIDSourcesPSet, true)
238  )->setComment("input with photon ID variables");
239 
240  // IsoDeposit configurables
241  edm::ParameterSetDescription isoDepositsPSet;
242  isoDepositsPSet.addOptional<edm::InputTag>("tracker");
243  isoDepositsPSet.addOptional<edm::InputTag>("ecal");
244  isoDepositsPSet.addOptional<edm::InputTag>("hcal");
245  isoDepositsPSet.addOptional<edm::InputTag>("pfAllParticles");
246  isoDepositsPSet.addOptional<edm::InputTag>("pfChargedHadrons");
247  isoDepositsPSet.addOptional<edm::InputTag>("pfChargedAll");
248  isoDepositsPSet.addOptional<edm::InputTag>("pfPUChargedHadrons");
249  isoDepositsPSet.addOptional<edm::InputTag>("pfNeutralHadrons");
250  isoDepositsPSet.addOptional<edm::InputTag>("pfPhotons");
251  isoDepositsPSet.addOptional<std::vector<edm::InputTag> >("user");
252  iDesc.addOptional("isoDeposits", isoDepositsPSet);
253 
254  // isolation values configurables
255  edm::ParameterSetDescription isolationValuesPSet;
256  isolationValuesPSet.addOptional<edm::InputTag>("tracker");
257  isolationValuesPSet.addOptional<edm::InputTag>("ecal");
258  isolationValuesPSet.addOptional<edm::InputTag>("hcal");
259  isolationValuesPSet.addOptional<edm::InputTag>("pfAllParticles");
260  isolationValuesPSet.addOptional<edm::InputTag>("pfChargedHadrons");
261  isolationValuesPSet.addOptional<edm::InputTag>("pfChargedAll");
262  isolationValuesPSet.addOptional<edm::InputTag>("pfPUChargedHadrons");
263  isolationValuesPSet.addOptional<edm::InputTag>("pfNeutralHadrons");
264  isolationValuesPSet.addOptional<edm::InputTag>("pfPhotons");
265  isolationValuesPSet.addOptional<std::vector<edm::InputTag> >("user");
266  iDesc.addOptional("isolationValues", isolationValuesPSet);
267 
268  // Efficiency configurables
269  edm::ParameterSetDescription efficienciesPSet;
270  efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
271  iDesc.add("efficiencies", efficienciesPSet);
272  iDesc.add<bool>("addEfficiencies", false);
273 
274  // Check to see if the user wants to add user data
275  edm::ParameterSetDescription userDataPSet;
277  iDesc.addOptional("userData", userDataPSet);
278 
279  edm::ParameterSetDescription isolationPSet;
280  isolationPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
281  iDesc.add("userIsolation", isolationPSet);
282 
283  descriptions.add("PATPhotonProducer", iDesc);
284 
285 }
286 
288 
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
T getParameter(std::string const &) const
void setComment(std::string const &value)
Assists in assimilating all pat::UserData into pat objects.
int i
Definition: DBlmapReader.cc:9
Analysis-level Photon class.
Definition: Photon.h:44
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
pat::PATUserDataHelper< pat::Photon > userDataHelper_
ParameterDescriptionBase * addOptional(U const &iLabel, T const &value)
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:184
static const HistoName names[]
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
void setAllowAnything()
allow any parameter label/value pairs
std::vector< edm::Handle< edm::ValueMap< double > > > IsolationValueMaps
std::vector< edm::EDGetTokenT< edm::ValueMap< Bool_t > > > photIDTokens_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< edm::View< reco::Photon > > photonToken_
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
std::vector< edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > > genMatchTokens_
void embedSuperCluster()
method to store the photon&#39;s supercluster internally
Definition: Photon.cc:70
pat::helper::KinResolutionsLoader resolutionLoader_
GreaterByEt< Photon > eTComparator_
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void setResolutions(pat::PATObject< T > &obj) const
Sets the efficiencies for this object, using the reference to the original objects.
bool isRealData() const
Definition: EventBase.h:60
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
static void fillDescription(edm::ParameterSetDescription &iDesc)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:192
bool enabled() const
True if it has a non null configuration.
Definition: MultiIsolator.h:50
IsolationLabels isoDepositLabels_
void setIsolation(IsolationKeys key, float value)
Definition: Photon.h:145
void setComment(std::string const &value)
pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_
void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep)
Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
Definition: Photon.h:185
int iEvent
Definition: GenABIO.cc:243
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup)
IsolationLabels isolationValueLabels_
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
std::vector< edm::EDGetTokenT< edm::ValueMap< IsoDeposit > > > isoDepositTokens_
void embedGenParticle()
Definition: PATObject.h:673
pat::helper::EfficiencyLoader efficiencyLoader_
void setPhotonIDs(const std::vector< IdPair > &ids)
Definition: Photon.h:87
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
int j
Definition: DBlmapReader.cc:9
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::vector< NameTag > photIDSrcs_
bool first
Definition: L1TdeRCT.cc:79
std::vector< edm::Handle< edm::ValueMap< IsoDeposit > > > IsoDepositMaps
pat::helper::MultiIsolator isolator_
void addGenParticleRef(const reco::GenParticleRef &ref)
Definition: PATObject.h:657
static void fillDescription(edm::ParameterSetDescription &iDesc)
Method for documentation and validation of PSet.
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
void setEfficiencies(pat::PATObject< T > &obj, const R &originalRef) const
Sets the efficiencies for this object, using the reference to the original objects.
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< std::pair< pat::IsolationKeys, float > > IsolationValuePairs
Definition: MultiIsolator.h:16
PATPhotonProducer(const edm::ParameterSet &iConfig)
volatile std::atomic< bool > shutdown_flag false
std::vector< edm::EDGetTokenT< edm::ValueMap< double > > > isolationValueTokens_
void newEvent(const edm::Event &event, const edm::EventSetup &setup) const
To be called for each new event, reads in the EventSetup object.
void newEvent(const edm::Event &event) const
To be called for each new event, reads in the ValueMaps for efficiencies.
void fill(const edm::View< T > &coll, int idx, IsolationValuePairs &isolations) const
Definition: MultiIsolator.h:82
Produces the pat::Photon.