Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-08 01:45:51

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