CMS 3D CMS Logo

PATGenericParticleProducer.cc
Go to the documentation of this file.
1 //
2 //
3 
8 #include <memory>
9 
10 using namespace pat;
11 
13  isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation") : edm::ParameterSet(), consumesCollector(), false),
14  userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector() )
15 {
16  // initialize the configurables
17  srcToken_ = consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>( "src" ));
18 
19  // RECO embedding
20  embedTrack_ = iConfig.getParameter<bool>( "embedTrack" );
21  embedGsfTrack_ = iConfig.getParameter<bool>( "embedGsfTrack" );
22  embedStandalone_ = iConfig.getParameter<bool>( "embedStandAloneMuon" );
23  embedCombined_ = iConfig.getParameter<bool>( "embedCombinedMuon" );
24  embedSuperCluster_ = iConfig.getParameter<bool>( "embedSuperCluster" );
25  embedTracks_ = iConfig.getParameter<bool>( "embedMultipleTracks" );
26  embedCaloTower_ = iConfig.getParameter<bool>( "embedCaloTower" );
27 
28  // MC matching configurables
29  addGenMatch_ = iConfig.getParameter<bool>( "addGenMatch" );
30  if (addGenMatch_) {
31  embedGenMatch_ = iConfig.getParameter<bool>( "embedGenMatch" );
32  if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
34  } else {
35  genMatchTokens_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" ), [this](edm::InputTag const & tag){return consumes<edm::Association<reco::GenParticleCollection> >(tag);});
36  }
37  }
38 
39  // quality
40  addQuality_ = iConfig.getParameter<bool>("addQuality");
41  qualitySrcToken_ = mayConsume<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("qualitySource"));
42 
43  // produces vector of particles
44  produces<std::vector<GenericParticle> >();
45 
46  if (iConfig.exists("isoDeposits")) {
47  edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
48  if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
49  if (depconf.exists("ecal")) isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
50  if (depconf.exists("hcal")) isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
51  if (depconf.exists("user")) {
52  std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
53  std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
54  int key = UserBaseIso;
55  for ( ; it != ed; ++it, ++key) {
56  isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
57  }
58  }
59  }
60  isoDepositTokens_ = edm::vector_transform(isoDepositLabels_, [this](std::pair<IsolationKeys,edm::InputTag> const & label){return consumes<edm::ValueMap<IsoDeposit> >(label.second);});
61 
62  // Efficiency configurables
63  addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
64  if (addEfficiencies_) {
66  }
67 
68  // Resolution configurables
69  addResolutions_ = iConfig.getParameter<bool>("addResolutions");
70  if (addResolutions_) {
72  }
73 
74  if (iConfig.exists("vertexing")) {
76  }
77 
78  // Check to see if the user wants to add user data
79  useUserData_ = false;
80  if ( iConfig.exists("userData") ) {
81  useUserData_ = true;
82  }
83 }
84 
86 }
87 
89  // Get the vector of GenericParticle's from the event
91  iEvent.getByToken(srcToken_, cands);
92 
93  // prepare isolation
94  if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
95 
97  if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
98  if (vertexingHelper_.enabled()) vertexingHelper_.newEvent(iEvent,iSetup);
99 
100  // prepare IsoDeposits
101  std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositTokens_.size());
102  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
103  iEvent.getByToken(isoDepositTokens_[j], deposits[j]);
104  }
105 
106  // prepare the MC matching
107  std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchTokens_.size());
108  if (addGenMatch_) {
109  for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
110  iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
111  }
112  }
113 
114  // prepare the quality
116  if (addQuality_) iEvent.getByToken(qualitySrcToken_, qualities);
117 
118  // loop over cands
119  std::vector<GenericParticle> * PATGenericParticles = new std::vector<GenericParticle>();
120  for (edm::View<reco::Candidate>::const_iterator itGenericParticle = cands->begin(); itGenericParticle != cands->end(); itGenericParticle++) {
121  // construct the GenericParticle from the ref -> save ref to original object
122  unsigned int idx = itGenericParticle - cands->begin();
123  edm::RefToBase<reco::Candidate> candRef = cands->refAt(idx);
124 
125  PATGenericParticles->push_back(GenericParticle(candRef));
126  GenericParticle & aGenericParticle = PATGenericParticles->back();
127 
128  // embed RECO
129  if (embedTrack_) aGenericParticle.embedTrack();
130  if (embedGsfTrack_) aGenericParticle.embedGsfTrack();
131  if (embedTracks_) aGenericParticle.embedTracks();
132  if (embedStandalone_) aGenericParticle.embedStandalone();
133  if (embedCombined_) aGenericParticle.embedCombined();
134  if (embedSuperCluster_) aGenericParticle.embedSuperCluster();
135  if (embedCaloTower_) aGenericParticle.embedCaloTower();
136 
137  // isolation
138  if (isolator_.enabled()) {
139  isolator_.fill(*cands, idx, isolatorTmpStorage_);
140  typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
141  // better to loop backwards, so the vector is resized less times
142  for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
143  aGenericParticle.setIsolation(it->first, it->second);
144  }
145  }
146 
147  // isodeposit
148  for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
149  aGenericParticle.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[candRef]);
150  }
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 genGenericParticle = (*genMatches[i])[candRef];
156  aGenericParticle.addGenParticleRef(genGenericParticle);
157  }
158  if (embedGenMatch_) aGenericParticle.embedGenParticle();
159  }
160 
161  if (addQuality_) {
162  aGenericParticle.setQuality( (*qualities)[candRef] );
163  }
164 
165  if (efficiencyLoader_.enabled()) {
166  efficiencyLoader_.setEfficiencies( aGenericParticle, candRef );
167  }
168 
169  if (resolutionLoader_.enabled()) {
170  resolutionLoader_.setResolutions(aGenericParticle);
171  }
172 
173  if (vertexingHelper_.enabled()) {
174  aGenericParticle.setVertexAssociation( vertexingHelper_(candRef) );
175  }
176 
177  if ( useUserData_ ) {
178  userDataHelper_.add( aGenericParticle, iEvent, iSetup );
179  }
180 
181  // PATGenericParticles->push_back(aGenericParticle); // NOOOOO!!!!
182  // We have already pushed_back this generic particle in the collection
183  // (we first push an empty particle and then fill it, to avoid useless copies)
184  }
185 
186  // sort GenericParticles in ET
187  std::sort(PATGenericParticles->begin(), PATGenericParticles->end(), eTComparator_);
188 
189  // put genEvt object in Event
190  std::unique_ptr<std::vector<GenericParticle> > myGenericParticles(PATGenericParticles);
191  iEvent.put(std::move(myGenericParticles));
193 
194 }
195 
197 
bool enabled() const
&#39;true&#39; if this there is at least one efficiency configured
T getParameter(std::string const &) const
void newEvent(const edm::Event &event)
To be called for each new event, reads in the ValueMaps for efficiencies.
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:186
void setIsolation(IsolationKeys key, float value)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool exists(std::string const &parameterName) const
checks if a parameter exists
void embedTrack()
embeds the master track instead of keeping a reference to it
IsolationKeys
Enum defining isolation keys.
Definition: Isolation.h:9
std::vector< edm::EDGetTokenT< edm::Association< reco::GenParticleCollection > > > genMatchTokens_
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 enabled() const
&#39;true&#39; if this there is at least one efficiency configured
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
bool enabled() const
True if it has a non null configuration.
Definition: MultiIsolator.h:50
Analysis-level Generic Particle class (e.g. for hadron or muon not fully reconstructed) ...
pat::PATUserDataHelper< pat::GenericParticle > userDataHelper_
int iEvent
Definition: GenABIO.cc:230
void beginEvent(const edm::Event &event, const edm::EventSetup &eventSetup)
void embedStandalone()
embeds the stand-alone track instead of keeping a reference to it
void embedGenParticle()
Definition: PATObject.h:694
pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_
edm::EDGetTokenT< edm::View< reco::Candidate > > srcToken_
void newEvent(const edm::Event &event, const edm::EventSetup &setup)
To be called for each new event, reads in the EventSetup object.
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
PATGenericParticleProducer(const edm::ParameterSet &iConfig)
GreaterByEt< GenericParticle > eTComparator_
Produces the pat::GenericParticle.
std::vector< edm::EDGetTokenT< edm::ValueMap< IsoDeposit > > > isoDepositTokens_
void embedGsfTrack()
embeds the gsf track instead of keeping a reference to it
void embedSuperCluster()
embeds the supercluster instead of keeping a reference to it
void addGenParticleRef(const reco::GenParticleRef &ref)
Definition: PATObject.h:678
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void embedTracks()
embeds the other tracks instead of keeping references
void newEvent(const edm::Event &event)
To be called for each new event, reads in the vertex collection.
void setEfficiencies(pat::PATObject< T > &obj, const R &originalRef) const
Sets the efficiencies for this object, using the reference to the original objects.
bool enabled() const
returns true if this was given a non dummy configuration
pat::helper::EfficiencyLoader efficiencyLoader_
pat::helper::VertexingHelper vertexingHelper_
std::vector< std::pair< pat::IsolationKeys, float > > IsolationValuePairs
Definition: MultiIsolator.h:16
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
void setIsoDeposit(IsolationKeys key, const IsoDeposit &dep)
Sets the IsoDeposit associated with some key; if it is already existent, it is overwritten.
void embedCaloTower()
embeds the calotower instead of keeping a reference to it
std::vector< std::pair< pat::IsolationKeys, edm::InputTag > > isoDepositLabels_
void setQuality(float quality)
sets a user defined quality value
void embedCombined()
embeds the combined track instead of keeping a reference to it
pat::helper::KinResolutionsLoader resolutionLoader_
edm::EDGetTokenT< edm::ValueMap< float > > qualitySrcToken_
def move(src, dest)
Definition: eostools.py:510
Produces and/or checks pat::VertexAssociation&#39;s.
void setVertexAssociation(const pat::VertexAssociation &assoc)
Set a single vertex association.
void fill(const edm::View< T > &coll, int idx, IsolationValuePairs &isolations) const
Definition: MultiIsolator.h:82