Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:27:46

0001 #ifndef RecoTauTag_RecoTau_RecoTauConstructor_h
0002 #define RecoTauTag_RecoTau_RecoTauConstructor_h
0003 
0004 /*
0005  * RecoTauConstructor
0006  *
0007  * A generalized builder of reco::PFTau objects.  Takes a variety of
0008  * different collections and converts them to the proper Ref format
0009  * needed for PFTau storage.  Automatically sets the p4, charge, and
0010  * other properties correctly.  Optionally, it can determine the
0011  * lead track information, and copy the gamma candidates owned by the
0012  * reconstructed pi zeros into the appropriate PiZero collection.
0013  *
0014  * If the gammas are copied from the PiZeros, the four vector will be
0015  * built using the PiZeros + Charged Hadrons.  This can be different than
0016  * the Gammas + Charged Hadrons, as the PiZero may have a mass hypothesis set by
0017  * the RecoTauPiZeroProducer
0018  *
0019  * Note that the p4 of the tau is *always* set as the sum of objects in
0020  * signal cone.
0021  *
0022  * Author: Evan K. Friis, UC Davis
0023  *
0024  */
0025 
0026 #include "DataFormats/Common/interface/Handle.h"
0027 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0028 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0029 #include "DataFormats/TauReco/interface/PFTau.h"
0030 #include "DataFormats/JetReco/interface/JetCollection.h"
0031 #include "DataFormats/TauReco/interface/PFRecoTauChargedHadron.h"
0032 #include "DataFormats/TauReco/interface/RecoTauPiZero.h"
0033 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0034 
0035 #include <vector>
0036 
0037 namespace reco {
0038   namespace tau {
0039 
0040     class RecoTauConstructor {
0041     public:
0042       enum Region { kSignal, kIsolation };
0043 
0044       enum ParticleType { kChargedHadron, kGamma, kNeutralHadron, kAll };
0045 
0046       /// Constructor with PFCandidate Handle
0047       RecoTauConstructor(const JetBaseRef& jetRef,
0048                          const edm::Handle<edm::View<reco::Candidate> >& pfCands,
0049                          bool copyGammasFromPiZeros = false,
0050                          const StringObjectFunction<reco::PFTau>* signalConeSize = nullptr,
0051                          double minAbsPhotonSumPt_insideSignalCone = 2.5,
0052                          double minRelPhotonSumPt_insideSignalCone = 0.,
0053                          double minAbsPhotonSumPt_outsideSignalCone = 1.e+9,
0054                          double minRelPhotonSumPt_outsideSignalCone = 1.e+9);
0055 
0056       /*
0057      * Code to set leading candidates.  These are just wrappers about
0058      * the embedded taus methods, but with the ability to convert Ptrs
0059      * to Refs.
0060      */
0061 
0062       /// Set leading PFChargedHadron candidate
0063       template <typename T>
0064       void setleadChargedHadrCand(const T& cand) {
0065         tau_->setleadChargedHadrCand(convertToPtr(cand));
0066       }
0067 
0068       /// Set leading PFGamma candidate
0069       template <typename T>
0070       void setleadNeutralCand(const T& cand) {
0071         tau_->setleadNeutralCand(convertToPtr(cand));
0072       }
0073 
0074       /// Set leading PF candidate
0075       template <typename T>
0076       void setleadCand(const T& cand) {
0077         tau_->setleadCand(convertToPtr(cand));
0078       }
0079 
0080       /// Append a PFCandidateRef/Ptr to a given collection
0081       void addPFCand(Region region, ParticleType type, const CandidatePtr& ptr, bool skipAddToP4 = false);
0082 
0083       /// Reserve a set amount of space for a given RefVector
0084       void reserve(Region region, ParticleType type, size_t size);
0085 
0086       // Add a collection of objects to a given collection
0087       template <typename InputIterator>
0088       void addPFCands(Region region, ParticleType type, const InputIterator& begin, const InputIterator& end) {
0089         for (InputIterator iter = begin; iter != end; ++iter) {
0090           addPFCand(region, type, convertToPtr(*iter));
0091         }
0092       }
0093 
0094       /// Reserve a set amount of space for the ChargedHadrons
0095       void reserveTauChargedHadron(Region region, size_t size);
0096 
0097       /// Add a ChargedHadron to the given collection
0098       void addTauChargedHadron(Region region, const PFRecoTauChargedHadron& chargedHadron);
0099 
0100       /// Add a list of charged hadrons to the input collection
0101       template <typename InputIterator>
0102       void addTauChargedHadrons(Region region, const InputIterator& begin, const InputIterator& end) {
0103         for (InputIterator iter = begin; iter != end; ++iter) {
0104           addTauChargedHadron(region, *iter);
0105         }
0106       }
0107 
0108       /// Reserve a set amount of space for the PiZeros
0109       void reservePiZero(Region region, size_t size);
0110 
0111       /// Add a PiZero to the given collection
0112       void addPiZero(Region region, const RecoTauPiZero& piZero);
0113 
0114       /// Add a list of pizeros to the input collection
0115       template <typename InputIterator>
0116       void addPiZeros(Region region, const InputIterator& begin, const InputIterator& end) {
0117         for (InputIterator iter = begin; iter != end; ++iter) {
0118           addPiZero(region, *iter);
0119         }
0120       }
0121 
0122       // Build and return the associated tau
0123       std::unique_ptr<reco::PFTau> get(bool setupLeadingCandidates = true);
0124 
0125       // Get the four vector of the signal objects added so far
0126       const reco::Candidate::LorentzVector& p4() const { return p4_; }
0127 
0128     private:
0129       typedef std::pair<Region, ParticleType> CollectionKey;
0130       typedef std::map<CollectionKey, std::vector<CandidatePtr>*> CollectionMap;
0131       typedef std::shared_ptr<std::vector<CandidatePtr> > SortedListPtr;
0132       typedef std::map<CollectionKey, SortedListPtr> SortedCollectionMap;
0133 
0134       bool copyGammas_;
0135 
0136       const StringObjectFunction<reco::PFTau>* signalConeSize_;
0137       double minAbsPhotonSumPt_insideSignalCone_;
0138       double minRelPhotonSumPt_insideSignalCone_;
0139       double minAbsPhotonSumPt_outsideSignalCone_;
0140       double minRelPhotonSumPt_outsideSignalCone_;
0141 
0142       // Retrieve collection associated to signal/iso and type
0143       std::vector<CandidatePtr>* getCollection(Region region, ParticleType type);
0144       SortedListPtr getSortedCollection(Region region, ParticleType type);
0145 
0146       // Sort all our collections by PT and copy them into the tau
0147       void sortAndCopyIntoTau();
0148 
0149       // Helper functions for dealing with refs
0150       CandidatePtr convertToPtr(const PFCandidatePtr& pfPtr) const;
0151       CandidatePtr convertToPtr(const CandidatePtr& candPtr) const;
0152 
0153       const edm::Handle<edm::View<reco::Candidate> >& pfCands_;
0154       std::unique_ptr<reco::PFTau> tau_;
0155       CollectionMap collections_;
0156 
0157       // Keep sorted (by descending pt) collections
0158       SortedCollectionMap sortedCollections_;
0159 
0160       // Keep track of the signal cone four vector in case we want it
0161       reco::Candidate::LorentzVector p4_;
0162     };
0163   }  // namespace tau
0164 }  // namespace reco
0165 #endif