Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-28 03:59:08

0001 /**
0002   \class    pat::PATGenericParticleProducer PATGenericParticleProducer.h "PhysicsTools/PatAlgos/interface/PATGenericParticleProducer.h"
0003   \brief    Produces the pat::GenericParticle
0004 
0005    The PATGenericParticleProducer produces the analysis-level pat::GenericParticle starting from
0006    any collection of Candidates
0007 
0008   \author   Giovanni Petrucciani
0009   \version  $Id: PATGenericParticleProducer.h,v 1.9 2009/06/25 23:49:35 gpetrucc Exp $
0010 */
0011 
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "DataFormats/Common/interface/View.h"
0014 #include "FWCore/Utilities/interface/transform.h"
0015 #include "FWCore/Framework/interface/stream/EDProducer.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
0019 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0020 #include "DataFormats/Common/interface/Association.h"
0021 #include "DataFormats/Common/interface/ValueMap.h"
0022 #include "CommonTools/Utils/interface/EtComparator.h"
0023 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0024 #include "PhysicsTools/PatAlgos/interface/MultiIsolator.h"
0025 #include "PhysicsTools/PatAlgos/interface/EfficiencyLoader.h"
0026 #include "PhysicsTools/PatAlgos/interface/KinResolutionsLoader.h"
0027 #include "PhysicsTools/PatAlgos/interface/VertexingHelper.h"
0028 #include "DataFormats/PatCandidates/interface/UserData.h"
0029 #include "PhysicsTools/PatAlgos/interface/PATUserDataHelper.h"
0030 
0031 #include <memory>
0032 
0033 namespace pat {
0034 
0035   class PATGenericParticleProducer : public edm::stream::EDProducer<> {
0036   public:
0037     explicit PATGenericParticleProducer(const edm::ParameterSet& iConfig);
0038     ~PATGenericParticleProducer() override;
0039 
0040     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0041 
0042   private:
0043     // configurables
0044     edm::EDGetTokenT<edm::View<reco::Candidate> > srcToken_;
0045 
0046     // embed RECo objects
0047     bool embedSuperCluster_, embedTrack_, embedTracks_, embedGsfTrack_, embedCaloTower_, embedStandalone_,
0048         embedCombined_;
0049 
0050     bool addQuality_;
0051     edm::EDGetTokenT<edm::ValueMap<float> > qualitySrcToken_;
0052 
0053     bool addGenMatch_;
0054     bool embedGenMatch_;
0055     std::vector<edm::EDGetTokenT<edm::Association<reco::GenParticleCollection> > > genMatchTokens_;
0056 
0057     // tools
0058     GreaterByEt<GenericParticle> eTComparator_;
0059 
0060     pat::helper::MultiIsolator isolator_;
0061     pat::helper::MultiIsolator::IsolationValuePairs isolatorTmpStorage_;  // better here than recreate at each event
0062     std::vector<std::pair<pat::IsolationKeys, edm::InputTag> > isoDepositLabels_;
0063     std::vector<edm::EDGetTokenT<edm::ValueMap<IsoDeposit> > > isoDepositTokens_;
0064 
0065     bool addEfficiencies_;
0066     pat::helper::EfficiencyLoader efficiencyLoader_;
0067 
0068     bool addResolutions_;
0069     pat::helper::KinResolutionsLoader resolutionLoader_;
0070 
0071     pat::helper::VertexingHelper vertexingHelper_;
0072 
0073     bool useUserData_;
0074     pat::PATUserDataHelper<pat::GenericParticle> userDataHelper_;
0075   };
0076 
0077 }  // namespace pat
0078 
0079 using namespace pat;
0080 
0081 PATGenericParticleProducer::PATGenericParticleProducer(const edm::ParameterSet& iConfig)
0082     : isolator_(iConfig.exists("userIsolation") ? iConfig.getParameter<edm::ParameterSet>("userIsolation")
0083                                                 : edm::ParameterSet(),
0084                 consumesCollector(),
0085                 false),
0086       userDataHelper_(iConfig.getParameter<edm::ParameterSet>("userData"), consumesCollector()) {
0087   // initialize the configurables
0088   srcToken_ = consumes<edm::View<reco::Candidate> >(iConfig.getParameter<edm::InputTag>("src"));
0089 
0090   // RECO embedding
0091   embedTrack_ = iConfig.getParameter<bool>("embedTrack");
0092   embedGsfTrack_ = iConfig.getParameter<bool>("embedGsfTrack");
0093   embedStandalone_ = iConfig.getParameter<bool>("embedStandAloneMuon");
0094   embedCombined_ = iConfig.getParameter<bool>("embedCombinedMuon");
0095   embedSuperCluster_ = iConfig.getParameter<bool>("embedSuperCluster");
0096   embedTracks_ = iConfig.getParameter<bool>("embedMultipleTracks");
0097   embedCaloTower_ = iConfig.getParameter<bool>("embedCaloTower");
0098 
0099   // MC matching configurables
0100   addGenMatch_ = iConfig.getParameter<bool>("addGenMatch");
0101   if (addGenMatch_) {
0102     embedGenMatch_ = iConfig.getParameter<bool>("embedGenMatch");
0103     if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
0104       genMatchTokens_.push_back(consumes<edm::Association<reco::GenParticleCollection> >(
0105           iConfig.getParameter<edm::InputTag>("genParticleMatch")));
0106     } else {
0107       genMatchTokens_ = edm::vector_transform(
0108           iConfig.getParameter<std::vector<edm::InputTag> >("genParticleMatch"),
0109           [this](edm::InputTag const& tag) { return consumes<edm::Association<reco::GenParticleCollection> >(tag); });
0110     }
0111   }
0112 
0113   // quality
0114   addQuality_ = iConfig.getParameter<bool>("addQuality");
0115   qualitySrcToken_ = mayConsume<edm::ValueMap<float> >(iConfig.getParameter<edm::InputTag>("qualitySource"));
0116 
0117   // produces vector of particles
0118   produces<std::vector<GenericParticle> >();
0119 
0120   if (iConfig.exists("isoDeposits")) {
0121     edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
0122     if (depconf.exists("tracker"))
0123       isoDepositLabels_.push_back(std::make_pair(pat::TrackIso, depconf.getParameter<edm::InputTag>("tracker")));
0124     if (depconf.exists("ecal"))
0125       isoDepositLabels_.push_back(std::make_pair(pat::EcalIso, depconf.getParameter<edm::InputTag>("ecal")));
0126     if (depconf.exists("hcal"))
0127       isoDepositLabels_.push_back(std::make_pair(pat::HcalIso, depconf.getParameter<edm::InputTag>("hcal")));
0128     if (depconf.exists("user")) {
0129       std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
0130       std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
0131       int key = UserBaseIso;
0132       for (; it != ed; ++it, ++key) {
0133         isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
0134       }
0135     }
0136   }
0137   isoDepositTokens_ =
0138       edm::vector_transform(isoDepositLabels_, [this](std::pair<IsolationKeys, edm::InputTag> const& label) {
0139         return consumes<edm::ValueMap<IsoDeposit> >(label.second);
0140       });
0141 
0142   // Efficiency configurables
0143   addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
0144   if (addEfficiencies_) {
0145     efficiencyLoader_ =
0146         pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"), consumesCollector());
0147   }
0148 
0149   // Resolution configurables
0150   addResolutions_ = iConfig.getParameter<bool>("addResolutions");
0151   if (addResolutions_) {
0152     resolutionLoader_ =
0153         pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"), consumesCollector());
0154   }
0155 
0156   if (iConfig.exists("vertexing")) {
0157     vertexingHelper_ =
0158         pat::helper::VertexingHelper(iConfig.getParameter<edm::ParameterSet>("vertexing"), consumesCollector());
0159   }
0160 
0161   // Check to see if the user wants to add user data
0162   useUserData_ = false;
0163   if (iConfig.exists("userData")) {
0164     useUserData_ = true;
0165   }
0166 }
0167 
0168 PATGenericParticleProducer::~PATGenericParticleProducer() {}
0169 
0170 void PATGenericParticleProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0171   // Get the vector of GenericParticle's from the event
0172   edm::Handle<edm::View<reco::Candidate> > cands;
0173   iEvent.getByToken(srcToken_, cands);
0174 
0175   // prepare isolation
0176   if (isolator_.enabled())
0177     isolator_.beginEvent(iEvent, iSetup);
0178 
0179   if (efficiencyLoader_.enabled())
0180     efficiencyLoader_.newEvent(iEvent);
0181   if (resolutionLoader_.enabled())
0182     resolutionLoader_.newEvent(iEvent, iSetup);
0183   if (vertexingHelper_.enabled())
0184     vertexingHelper_.newEvent(iEvent, iSetup);
0185 
0186   // prepare IsoDeposits
0187   std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositTokens_.size());
0188   for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
0189     iEvent.getByToken(isoDepositTokens_[j], deposits[j]);
0190   }
0191 
0192   // prepare the MC matching
0193   std::vector<edm::Handle<edm::Association<reco::GenParticleCollection> > > genMatches(genMatchTokens_.size());
0194   if (addGenMatch_) {
0195     for (size_t j = 0, nd = genMatchTokens_.size(); j < nd; ++j) {
0196       iEvent.getByToken(genMatchTokens_[j], genMatches[j]);
0197     }
0198   }
0199 
0200   // prepare the quality
0201   edm::Handle<edm::ValueMap<float> > qualities;
0202   if (addQuality_)
0203     iEvent.getByToken(qualitySrcToken_, qualities);
0204 
0205   // loop over cands
0206   std::vector<GenericParticle>* PATGenericParticles = new std::vector<GenericParticle>();
0207   for (edm::View<reco::Candidate>::const_iterator itGenericParticle = cands->begin(); itGenericParticle != cands->end();
0208        itGenericParticle++) {
0209     // construct the GenericParticle from the ref -> save ref to original object
0210     unsigned int idx = itGenericParticle - cands->begin();
0211     edm::RefToBase<reco::Candidate> candRef = cands->refAt(idx);
0212 
0213     PATGenericParticles->push_back(GenericParticle(candRef));
0214     GenericParticle& aGenericParticle = PATGenericParticles->back();
0215 
0216     // embed RECO
0217     if (embedTrack_)
0218       aGenericParticle.embedTrack();
0219     if (embedGsfTrack_)
0220       aGenericParticle.embedGsfTrack();
0221     if (embedTracks_)
0222       aGenericParticle.embedTracks();
0223     if (embedStandalone_)
0224       aGenericParticle.embedStandalone();
0225     if (embedCombined_)
0226       aGenericParticle.embedCombined();
0227     if (embedSuperCluster_)
0228       aGenericParticle.embedSuperCluster();
0229     if (embedCaloTower_)
0230       aGenericParticle.embedCaloTower();
0231 
0232     // isolation
0233     if (isolator_.enabled()) {
0234       isolator_.fill(*cands, idx, isolatorTmpStorage_);
0235       typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
0236       // better to loop backwards, so the vector is resized less times
0237       for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(),
0238                                                        ed = isolatorTmpStorage_.rend();
0239            it != ed;
0240            ++it) {
0241         aGenericParticle.setIsolation(it->first, it->second);
0242       }
0243     }
0244 
0245     // isodeposit
0246     for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
0247       aGenericParticle.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[candRef]);
0248     }
0249 
0250     // store the match to the generated final state muons
0251     if (addGenMatch_) {
0252       for (size_t i = 0, n = genMatches.size(); i < n; ++i) {
0253         reco::GenParticleRef genGenericParticle = (*genMatches[i])[candRef];
0254         aGenericParticle.addGenParticleRef(genGenericParticle);
0255       }
0256       if (embedGenMatch_)
0257         aGenericParticle.embedGenParticle();
0258     }
0259 
0260     if (addQuality_) {
0261       aGenericParticle.setQuality((*qualities)[candRef]);
0262     }
0263 
0264     if (efficiencyLoader_.enabled()) {
0265       efficiencyLoader_.setEfficiencies(aGenericParticle, candRef);
0266     }
0267 
0268     if (resolutionLoader_.enabled()) {
0269       resolutionLoader_.setResolutions(aGenericParticle);
0270     }
0271 
0272     if (vertexingHelper_.enabled()) {
0273       aGenericParticle.setVertexAssociation(vertexingHelper_(candRef));
0274     }
0275 
0276     if (useUserData_) {
0277       userDataHelper_.add(aGenericParticle, iEvent, iSetup);
0278     }
0279 
0280     // PATGenericParticles->push_back(aGenericParticle); // NOOOOO!!!!
0281     // We have already pushed_back this generic particle in the collection
0282     // (we first push an empty particle and then fill it, to avoid useless copies)
0283   }
0284 
0285   // sort GenericParticles in ET
0286   std::sort(PATGenericParticles->begin(), PATGenericParticles->end(), eTComparator_);
0287 
0288   // put genEvt object in Event
0289   std::unique_ptr<std::vector<GenericParticle> > myGenericParticles(PATGenericParticles);
0290   iEvent.put(std::move(myGenericParticles));
0291   if (isolator_.enabled())
0292     isolator_.endEvent();
0293 }
0294 
0295 #include "FWCore/Framework/interface/MakerMacros.h"
0296 DEFINE_FWK_MODULE(PATGenericParticleProducer);