Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:49:20

0001 #ifndef HeavyFlavorAnalysis_RecoDecay_BPHRecoBuilder_h
0002 #define HeavyFlavorAnalysis_RecoDecay_BPHRecoBuilder_h
0003 /** \class BPHRecoBuilder
0004  *
0005  *  Description: 
0006  *     Class to build all the combinations of decay products
0007  *     starting from reco::Candidate collections and applying
0008  *     selection cuts to decay products and reconstructed candidates
0009  *
0010  *  \author Paolo Ronchese INFN Padova
0011  *
0012  */
0013 
0014 //----------------------
0015 // Base Class Headers --
0016 //----------------------
0017 
0018 //------------------------------------
0019 // Collaborating Class Declarations --
0020 //------------------------------------
0021 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHDecayMomentum.h"
0022 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHDecayVertex.h"
0023 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHKinematicFit.h"
0024 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHGenericPtr.h"
0025 class BPHRecoSelect;
0026 class BPHMomentumSelect;
0027 class BPHVertexSelect;
0028 class BPHFitSelect;
0029 
0030 namespace edm {
0031   class EventSetup;
0032 }
0033 
0034 namespace reco {
0035   class RecoCandidate;
0036 }
0037 
0038 //---------------
0039 // C++ Headers --
0040 //---------------
0041 #include <string>
0042 #include <vector>
0043 #include <map>
0044 #include <set>
0045 
0046 //              ---------------------
0047 //              -- Class Interface --
0048 //              ---------------------
0049 
0050 class BPHRecoBuilder {
0051   friend class BPHRecoSelect;
0052 
0053 public:
0054   /** Constructor
0055    */
0056   BPHRecoBuilder(const edm::EventSetup& es);
0057 
0058   // deleted copy constructor and assignment operator
0059   BPHRecoBuilder(const BPHRecoBuilder& x) = delete;
0060   BPHRecoBuilder& operator=(const BPHRecoBuilder& x) = delete;
0061 
0062   /** Destructor
0063    */
0064   virtual ~BPHRecoBuilder();
0065 
0066   /** Operations
0067    */
0068 
0069   // common object to interface with edm collections
0070   class BPHGenericCollection {
0071   public:
0072     BPHGenericCollection(const std::string& list) : sList(list) {}
0073     virtual ~BPHGenericCollection() {}
0074     virtual const reco::Candidate& get(int i) const = 0;
0075     virtual int size() const = 0;
0076     const std::string& searchList() const { return sList; }
0077 
0078   private:
0079     std::string sList;
0080   };
0081   template <class T>
0082   static BPHGenericCollection* createCollection(const edm::Handle<T>& collection, const std::string& list = "cfhpmig");
0083   static BPHGenericCollection* createCollection(const std::vector<const reco::Candidate*>& candList,
0084                                                 const std::string& list = "cfhpmig");
0085 
0086   /// add collection of particles giving them a name
0087   /// collections can be added as
0088   /// - for simple particles as edm::Handle of an edm collection
0089   ///   (an object with an operator [] returning a pointer to reco::Candidate)
0090   ///   or a BPHGenericCollection created from it
0091   /// - for previously reconstructed particles as std::vector
0092   ///   of pointers to BPHRecoCandidate or objects inheriting
0093   ///   from BPHRecoCandidate
0094   void add(const std::string& name, const BPHGenericCollection* collection, double mass = -1.0, double msig = -1.0);
0095   template <class T>
0096   void add(const std::string& name, const edm::Handle<T>& collection, double mass = -1.0, double msig = -1.0);
0097   void add(const std::string& name, const std::vector<BPHRecoConstCandPtr>& collection);
0098   template <class T>
0099   void add(const std::string& name, const std::vector<T>& collection);
0100 
0101   /// define selections to particles to be used in the reconstruction:
0102   /// simple particles
0103   void filter(const std::string& name, const BPHRecoSelect& sel) const;
0104   /// previously reconstructed particles, at simple momentum sum level
0105   void filter(const std::string& name, const BPHMomentumSelect& sel) const;
0106   /// previously reconstructed particles, at vertex reconstruction level
0107   void filter(const std::string& name, const BPHVertexSelect& sel) const;
0108   /// previously reconstructed particles, at kinematical fit level
0109   void filter(const std::string& name, const BPHFitSelect& sel) const;
0110 
0111   /// define selections to recontructed particles, at different levels:
0112   /// simple momentum sum
0113   void filter(const BPHMomentumSelect& sel);
0114   /// vertex reconstruction
0115   void filter(const BPHVertexSelect& sel);
0116   /// kinematical fit
0117   void filter(const BPHFitSelect& sel);
0118   // apply selection to reconstructed candidate
0119   bool accept(const BPHRecoCandidate& cand) const;
0120 
0121   /// define a min. squared momentum difference between two particles
0122   /// (relative difference to squared momentum sum is used)
0123   void setMinPDiffererence(double pMin);
0124 
0125   /// object to contain a combination of simple and previously reconstructed
0126   /// particles with their names
0127   struct ComponentSet {
0128     std::map<std::string, BPHDecayMomentum::Component> daugMap;
0129     std::map<std::string, BPHRecoConstCandPtr> compMap;
0130   };
0131 
0132   /// build a set of combinations of particles fulfilling the selections
0133   std::vector<ComponentSet> build() const;
0134 
0135   /// get the EventSetup set in the constructor
0136   const edm::EventSetup* eventSetup() const;
0137 
0138   // compare two particles with their track reference and return
0139   // true or false for same or different particles, including a
0140   // check with momentum difference
0141   static bool sameTrack(const reco::Candidate* lCand, const reco::Candidate* rCand, double minPDifference);
0142 
0143 private:
0144   // object to interface with a specific edm collection
0145   typedef std::vector<const reco::Candidate*> rcpV;
0146   template <class T>
0147   class BPHInterfaceCollection : public BPHGenericCollection {
0148   public:
0149     BPHInterfaceCollection(const T& c, const std::string& list) : BPHGenericCollection(list), cPtr(&c) {}
0150     ~BPHInterfaceCollection() override {}
0151     int size() const override { return cPtr->size(); }
0152 
0153   protected:
0154     const T* cPtr;
0155   };
0156   template <class T>
0157   class BPHSpecificCollection : public BPHInterfaceCollection<T> {
0158   public:
0159     BPHSpecificCollection(const T& c, const std::string& list) : BPHInterfaceCollection<T>(c, list) {}
0160     const reco::Candidate& get(int i) const override { return (*this->cPtr)[i]; }
0161   };
0162 
0163   // object to contain a list of simple particles
0164   // with their names, selections, masses and sigma
0165   struct BPHRecoSource {
0166     const std::string* name;
0167     const BPHGenericCollection* collection;
0168     std::vector<const BPHRecoSelect*> selector;
0169     double mass;
0170     double msig;
0171   };
0172 
0173   // object to contain a list of previously reconstructed particles
0174   // with their names and selections
0175   struct BPHCompSource {
0176     const std::string* name;
0177     const std::vector<BPHRecoConstCandPtr>* collection;
0178     std::vector<const BPHMomentumSelect*> momSelector;
0179     std::vector<const BPHVertexSelect*> vtxSelector;
0180     std::vector<const BPHFitSelect*> fitSelector;
0181   };
0182 
0183   // map of names to simple or previously recontructed particles
0184   // for currently tested combination
0185   mutable std::map<std::string, const reco::Candidate*> daugMap;
0186   mutable std::map<std::string, BPHRecoConstCandPtr> compMap;
0187 
0188   const edm::EventSetup* evSetup;
0189   double minPDiff;
0190 
0191   // list of simple and previously recontructed particles in the decay
0192   std::vector<BPHRecoSource*> sourceList;
0193   std::vector<BPHCompSource*> srCompList;
0194 
0195   // set of copies of previously reconstructed particles list
0196   // for bookkeeping and cleanup
0197   std::set<const std::vector<BPHRecoConstCandPtr>*> compCollectList;
0198 
0199   // list fo selections to reconstructed particle
0200   std::vector<const BPHMomentumSelect*> msList;
0201   std::vector<const BPHVertexSelect*> vsList;
0202   std::vector<const BPHFitSelect*> fsList;
0203 
0204   // map linking particles names to position in list position
0205   std::map<std::string, int> sourceId;
0206   std::map<std::string, int> srCompId;
0207 
0208   // recursive function to build particles combinations
0209   void build(std::vector<ComponentSet>& compList,
0210              ComponentSet& compSet,
0211              std::vector<BPHRecoSource*>::const_iterator r_iter,
0212              std::vector<BPHRecoSource*>::const_iterator r_iend,
0213              std::vector<BPHCompSource*>::const_iterator c_iter,
0214              std::vector<BPHCompSource*>::const_iterator c_iend) const;
0215 
0216   // check for already used particles in a combination
0217   // previously recontructed particles are assumed to be included
0218   // after simple particles
0219   bool contained(ComponentSet& compSet, const reco::Candidate* cand) const;
0220   bool contained(ComponentSet& compSet, BPHRecoConstCandPtr cand) const;
0221   // compare two particles with their track reference and return
0222   // true or false for same or different particles, including a
0223   // check with momentum difference
0224   bool sameTrack(const reco::Candidate* lCand, const reco::Candidate* rCand) const;
0225 };
0226 
0227 template <class T>
0228 BPHRecoBuilder::BPHGenericCollection* BPHRecoBuilder::createCollection(const edm::Handle<T>& collection,
0229                                                                        const std::string& list) {
0230   return new BPHSpecificCollection<T>(*collection, list);
0231 }
0232 
0233 template <class T>
0234 void BPHRecoBuilder::add(const std::string& name, const edm::Handle<T>& collection, double mass, double msig) {
0235   // forward call after creating an interface to the collection
0236   add(name, new BPHSpecificCollection<T>(*collection, "cfhpmig"), mass, msig);
0237   return;
0238 }
0239 
0240 template <class T>
0241 void BPHRecoBuilder::add(const std::string& name, const std::vector<T>& collection) {
0242   // forward call after converting the list of pointer to a list
0243   // of pointer to base objects
0244   int i;
0245   int n = collection.size();
0246   std::vector<BPHRecoConstCandPtr>* compCandList = new std::vector<BPHRecoConstCandPtr>(n);
0247   for (i = 0; i < n; ++i)
0248     (*compCandList)[i] = collection[i];
0249   // save the converted list for cleanup
0250   compCollectList.insert(compCandList);
0251   add(name, *compCandList);
0252   return;
0253 }
0254 
0255 template <>
0256 class BPHRecoBuilder::BPHSpecificCollection<BPHRecoBuilder::rcpV>
0257     : public BPHRecoBuilder::BPHInterfaceCollection<BPHRecoBuilder::rcpV> {
0258 public:
0259   BPHSpecificCollection(const BPHRecoBuilder::rcpV& c, const std::string& list)
0260       : BPHInterfaceCollection<BPHRecoBuilder::rcpV>(c, list) {}
0261   const reco::Candidate& get(int i) const override { return *(*this->cPtr)[i]; }
0262 };
0263 
0264 #endif