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 // $Id: PATPhotonProducer.cc,v 1.29 2012/05/20 20:12:25 rwolf Exp $
3 //
4 
10 
13 
14 #include <memory>
15 
16 using namespace pat;
17 
19  isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), false) ,
20  useUserData_(iConfig.exists("userData"))
21 {
22  // initialize the configurables
23  photonSrc_ = iConfig.getParameter<edm::InputTag>("photonSource");
24  embedSuperCluster_ = iConfig.getParameter<bool>("embedSuperCluster");
25  // MC matching configurables
26  addGenMatch_ = iConfig.getParameter<bool>( "addGenMatch" );
27  if (addGenMatch_) {
28  embedGenMatch_ = iConfig.getParameter<bool>( "embedGenMatch" );
29  if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
30  genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
31  }
32  else {
33  genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
34  }
35  }
36  // Efficiency configurables
37  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
38  if (addEfficiencies_) {
40  }
41  // photon ID configurables
42  addPhotonID_ = iConfig.getParameter<bool>( "addPhotonID" );
43  if (addPhotonID_) {
44  // it might be a single photon ID
45  if (iConfig.existsAs<edm::InputTag>("photonIDSource")) {
46  photIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("photonIDSource")));
47  }
48  // or there might be many of them
49  if (iConfig.existsAs<edm::ParameterSet>("photonIDSources")) {
50  // please don't configure me twice
51  if (!photIDSrcs_.empty()){
52  throw cms::Exception("Configuration") << "PATPhotonProducer: you can't specify both 'photonIDSource' and 'photonIDSources'\n";
53  }
54  // read the different photon ID names
55  edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("photonIDSources");
56  std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
57  for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
58  photIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
59  }
60  }
61  // but in any case at least once
62  if (photIDSrcs_.empty()) throw cms::Exception("Configuration") <<
63  "PATPhotonProducer: id addPhotonID is true, you must specify either:\n" <<
64  "\tInputTag photonIDSource = <someTag>\n" << "or\n" <<
65  "\tPSet photonIDSources = { \n" <<
66  "\t\tInputTag <someName> = <someTag> // as many as you want \n " <<
67  "\t}\n";
68  }
69  // Resolution configurables
70  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
71  if (addResolutions_) {
73  }
74  // Check to see if the user wants to add user data
75  if ( useUserData_ ) {
77  }
78  // produces vector of photons
79  produces<std::vector<Photon> >();
80 
81  if (iConfig.exists("isoDeposits")) {
82  edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
83  if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
84  if (depconf.exists("ecal")) isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
85  if (depconf.exists("hcal")) isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
86  if (depconf.exists("user")) {
87  std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
88  std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
89  int key = UserBaseIso;
90  for ( ; it != ed; ++it, ++key) {
91  isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
92  }
93  }
94  }
95 }
96 
98 }
99 
101 {
102  // switch off embedding (in unschedules mode)
103  if (iEvent.isRealData()){
104  addGenMatch_ = false;
105  embedGenMatch_ = false;
106  }
107 
108  // Get the vector of Photon's from the event
110  iEvent.getByLabel(photonSrc_, photons);
111 
112  // prepare the MC matching
113  std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchSrc_.size());
114  if (addGenMatch_) {
115  for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
116  iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
117  }
118  }
119 
120  if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
121 
123  if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
124 
125  std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
126  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
127  iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
128  }
129 
130  // prepare ID extraction
131  std::vector<edm::Handle<edm::ValueMap<Bool_t> > > idhandles;
132  std::vector<pat::Photon::IdPair> ids;
133  if (addPhotonID_) {
134  idhandles.resize(photIDSrcs_.size());
135  ids.resize(photIDSrcs_.size());
136  for (size_t i = 0; i < photIDSrcs_.size(); ++i) {
137  iEvent.getByLabel(photIDSrcs_[i].second, idhandles[i]);
138  ids[i].first = photIDSrcs_[i].first;
139  }
140  }
141 
142  // loop over photons
143  std::vector<Photon> * PATPhotons = new std::vector<Photon>();
144  for (edm::View<reco::Photon>::const_iterator itPhoton = photons->begin(); itPhoton != photons->end(); itPhoton++) {
145  // construct the Photon from the ref -> save ref to original object
146  unsigned int idx = itPhoton - photons->begin();
147  edm::RefToBase<reco::Photon> photonRef = photons->refAt(idx);
148  edm::Ptr<reco::Photon> photonPtr = photons->ptrAt(idx);
149  Photon aPhoton(photonRef);
150  if (embedSuperCluster_) aPhoton.embedSuperCluster();
151 
152  // store the match to the generated final state muons
153  if (addGenMatch_) {
154  for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
155  reco::GenParticleRef genPhoton = (*genMatches[i])[photonRef];
156  aPhoton.addGenParticleRef(genPhoton);
157  }
158  if (embedGenMatch_) aPhoton.embedGenParticle();
159  }
160 
161  if (efficiencyLoader_.enabled()) {
162  efficiencyLoader_.setEfficiencies( aPhoton, photonRef );
163  }
164 
165  if (resolutionLoader_.enabled()) {
167  }
168 
169  // here comes the extra functionality
170  if (isolator_.enabled()) {
171  isolator_.fill(*photons, idx, isolatorTmpStorage_);
172  typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
173  // better to loop backwards, so the vector is resized less times
174  for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
175  aPhoton.setIsolation(it->first, it->second);
176  }
177  }
178 
179  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
180  aPhoton.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[photonRef]);
181  }
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<std::vector<edm::InputTag> >("user");
246  iDesc.addOptional("isoDeposits", isoDepositsPSet);
247 
248  // Efficiency configurables
249  edm::ParameterSetDescription efficienciesPSet;
250  efficienciesPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
251  iDesc.add("efficiencies", efficienciesPSet);
252  iDesc.add<bool>("addEfficiencies", false);
253 
254  // Check to see if the user wants to add user data
255  edm::ParameterSetDescription userDataPSet;
257  iDesc.addOptional("userData", userDataPSet);
258 
259  edm::ParameterSetDescription isolationPSet;
260  isolationPSet.setAllowAnything(); // TODO: the pat helper needs to implement a description.
261  iDesc.add("userIsolation", isolationPSet);
262 
263  descriptions.add("PATPhotonProducer", iDesc);
264 
265 }
266 
268 
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:46
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)
std::vector< edm::InputTag > genMatchSrc_
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:187
static const HistoName names[]
std::pair< std::string, edm::InputTag > NameTag
void setAllowAnything()
allow any parameter label/value pairs
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
void embedSuperCluster()
method to store the photon&#39;s supercluster internally
Definition: Photon.cc:71
pat::helper::KinResolutionsLoader resolutionLoader_
GreaterByEt< Photon > eTComparator_
IsolationKeys
Enum defining isolation keys.
Definition: Isolation.h:9
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
std::vector< std::pair< pat::IsolationKeys, edm::InputTag > > isoDepositLabels_
static void fillDescription(edm::ParameterSetDescription &iDesc)
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:195
U second(std::pair< T, U > const &p)
bool enabled() const
True if it has a non null configuration.
Definition: MultiIsolator.h:50
void setIsolation(IsolationKeys key, float value)
Definition: Photon.h:130
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:170
int iEvent
Definition: GenABIO.cc:243
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
void embedGenParticle()
Definition: PATObject.h:675
pat::helper::EfficiencyLoader efficiencyLoader_
void setPhotonIDs(const std::vector< IdPair > &ids)
Definition: Photon.h:89
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:94
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
pat::helper::MultiIsolator isolator_
void addGenParticleRef(const reco::GenParticleRef &ref)
Definition: PATObject.h:659
static void fillDescription(edm::ParameterSetDescription &iDesc)
Method for documentation and validation of PSet.
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
list key
Definition: combine.py:13
PATPhotonProducer(const edm::ParameterSet &iConfig)
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
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.