Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:37

0001 #ifndef HeavyFlavorAnalysis_SpecificDecay_BPHWriteSpecificDecay_h
0002 #define HeavyFlavorAnalysis_SpecificDecay_BPHWriteSpecificDecay_h
0003 
0004 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHAnalyzerTokenWrapper.h"
0005 
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0010 
0011 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHPlusMinusCandidate.h"
0012 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoCandidate.h"
0013 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHTrackReference.h"
0014 
0015 #include "DataFormats/Candidate/interface/VertexCompositeCandidate.h"
0016 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHVertexCompositePtrCandidate.h"
0017 
0018 #include "DataFormats/Common/interface/Ref.h"
0019 #include "DataFormats/TrackReco/interface/Track.h"
0020 #include "DataFormats/PatCandidates/interface/Muon.h"
0021 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0022 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0023 
0024 #include "RecoVertex/KinematicFitPrimitives/interface/RefCountedKinematicParticle.h"
0025 #include "RecoVertex/KinematicFitPrimitives/interface/RefCountedKinematicVertex.h"
0026 
0027 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0028 #include "TrackingTools/PatternTools/interface/TwoTrackMinimumDistance.h"
0029 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
0030 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
0031 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0032 
0033 #include <string>
0034 #include <vector>
0035 #include <map>
0036 
0037 class TH1F;
0038 class BPHRecoCandidate;
0039 
0040 class BPHWriteSpecificDecay : public BPHAnalyzerWrapper<BPHModuleWrapper::stream_producer> {
0041 public:
0042   explicit BPHWriteSpecificDecay(const edm::ParameterSet& ps);
0043   ~BPHWriteSpecificDecay() override = default;
0044 
0045   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0046 
0047   void produce(edm::Event& ev, const edm::EventSetup& es) override;
0048   virtual void fill(edm::Event& ev, const BPHEventSetupWrapper& es);
0049 
0050 private:
0051   std::string pVertexLabel;
0052   std::string patMuonLabel;
0053   std::string ccCandsLabel;
0054   std::string pfCandsLabel;
0055   std::string pcCandsLabel;
0056   std::string gpCandsLabel;
0057   std::string k0CandsLabel;
0058   std::string l0CandsLabel;
0059   std::string kSCandsLabel;
0060   std::string lSCandsLabel;
0061 
0062   // token wrappers to allow running both on "old" and "new" CMSSW versions
0063   BPHESTokenWrapper<MagneticField, IdealMagneticFieldRecord> magFieldToken;
0064   BPHESTokenWrapper<TransientTrackBuilder, TransientTrackRecord> ttBToken;
0065   BPHTokenWrapper<std::vector<reco::Vertex> > pVertexToken;
0066   BPHTokenWrapper<pat::MuonCollection> patMuonToken;
0067   BPHTokenWrapper<std::vector<pat::CompositeCandidate> > ccCandsToken;
0068   BPHTokenWrapper<std::vector<reco::PFCandidate> > pfCandsToken;
0069   BPHTokenWrapper<std::vector<BPHTrackReference::candidate> > pcCandsToken;
0070   BPHTokenWrapper<std::vector<pat::GenericParticle> > gpCandsToken;
0071   BPHTokenWrapper<std::vector<reco::VertexCompositeCandidate> > k0CandsToken;
0072   BPHTokenWrapper<std::vector<reco::VertexCompositeCandidate> > l0CandsToken;
0073   BPHTokenWrapper<std::vector<reco::VertexCompositePtrCandidate> > kSCandsToken;
0074   BPHTokenWrapper<std::vector<reco::VertexCompositePtrCandidate> > lSCandsToken;
0075 
0076   bool usePV;
0077   bool usePM;
0078   bool useCC;
0079   bool usePF;
0080   bool usePC;
0081   bool useGP;
0082   bool useK0;
0083   bool useL0;
0084   bool useKS;
0085   bool useLS;
0086 
0087   std::string oniaName;
0088   std::string sdName;
0089   std::string ssName;
0090   std::string buName;
0091   std::string bpName;
0092   std::string bdName;
0093   std::string bsName;
0094   std::string k0Name;
0095   std::string l0Name;
0096   std::string b0Name;
0097   std::string lbName;
0098   std::string bcName;
0099   std::string psi2SName;
0100   std::string x3872Name;
0101 
0102   enum recoType {
0103     Onia,
0104     Pmm,
0105     Psi1,
0106     Psi2,
0107     Ups,
0108     Ups1,
0109     Ups2,
0110     Ups3,
0111     Kx0,
0112     Pkk,
0113     Bu,
0114     Bp,
0115     Bd,
0116     Bs,
0117     K0s,
0118     Lambda0,
0119     B0,
0120     Lambdab,
0121     Bc,
0122     Psi2S,
0123     X3872
0124   };
0125   enum parType {
0126     ptMin,
0127     etaMax,
0128     mPsiMin,
0129     mPsiMax,
0130     mKx0Min,
0131     mKx0Max,
0132     mPhiMin,
0133     mPhiMax,
0134     mK0sMin,
0135     mK0sMax,
0136     mLambda0Min,
0137     mLambda0Max,
0138     massMin,
0139     massMax,
0140     probMin,
0141     mFitMin,
0142     mFitMax,
0143     constrMass,
0144     constrSigma,
0145     requireJPsi,
0146     constrMJPsi,
0147     constrMPsi2,
0148     writeCandidate
0149   };
0150   std::map<std::string, recoType> rMap;
0151   std::map<std::string, parType> pMap;
0152   std::map<std::string, parType> fMap;
0153   std::map<recoType, std::map<parType, double> > parMap;
0154 
0155   bool recoOnia;
0156   bool recoKx0;
0157   bool recoPkk;
0158   bool recoBu;
0159   bool recoBp;
0160   bool recoBd;
0161   bool recoBs;
0162   bool recoK0s;
0163   bool recoLambda0;
0164   bool recoB0;
0165   bool recoLambdab;
0166   bool recoBc;
0167   bool recoPsi2S;
0168   bool recoX3872;
0169 
0170   bool allKx0;
0171   bool allPkk;
0172   bool allK0s;
0173   bool allLambda0;
0174 
0175   bool writeOnia;
0176   bool writeKx0;
0177   bool writePkk;
0178   bool writeBu;
0179   bool writeBp;
0180   bool writeBd;
0181   bool writeBs;
0182   bool writeK0s;
0183   bool writeLambda0;
0184   bool writeB0;
0185   bool writeLambdab;
0186   bool writeBc;
0187   bool writePsi2S;
0188   bool writeX3872;
0189 
0190   bool writeVertex;
0191   bool writeMomentum;
0192 
0193   std::vector<BPHPlusMinusConstCandPtr> lFull;
0194   std::vector<BPHPlusMinusConstCandPtr> lJPsi;
0195   std::vector<BPHRecoConstCandPtr> lSd;
0196   std::vector<BPHRecoConstCandPtr> lSs;
0197   std::vector<BPHRecoConstCandPtr> lBu;
0198   std::vector<BPHRecoConstCandPtr> lBp;
0199   std::vector<BPHRecoConstCandPtr> lBd;
0200   std::vector<BPHRecoConstCandPtr> lBs;
0201   std::vector<BPHPlusMinusConstCandPtr> lK0;
0202   std::vector<BPHPlusMinusConstCandPtr> lL0;
0203   std::vector<BPHRecoConstCandPtr> lB0;
0204   std::vector<BPHRecoConstCandPtr> lLb;
0205   std::vector<BPHRecoConstCandPtr> lBc;
0206   std::vector<BPHRecoConstCandPtr> lPsi2S;
0207   std::vector<BPHRecoConstCandPtr> lX3872;
0208 
0209   std::map<const BPHRecoCandidate*, const BPHRecoCandidate*> jPsiOMap;
0210   std::map<const BPHRecoCandidate*, const BPHRecoCandidate*> daughMap;
0211   typedef edm::Ref<std::vector<reco::Vertex> > vertex_ref;
0212   std::map<const BPHRecoCandidate*, vertex_ref> pvRefMap;
0213   typedef edm::Ref<pat::CompositeCandidateCollection> compcc_ref;
0214   std::map<const BPHRecoCandidate*, compcc_ref> ccRefMap;
0215 
0216   void setRecoParameters(const edm::ParameterSet& ps);
0217 
0218   static void addTrackModes(const std::string& name, const BPHRecoCandidate& cand, std::string& modes, bool& count);
0219 
0220   static void addTrackModes(const std::string& name, const BPHRecoCandidate& cand, pat::CompositeCandidate& cc);
0221 
0222   template <class T>
0223   edm::OrphanHandle<pat::CompositeCandidateCollection> write(edm::Event& ev,
0224                                                              const std::vector<T>& list,
0225                                                              const std::string& name) {
0226     pat::CompositeCandidateCollection* ccList = new pat::CompositeCandidateCollection;
0227     int i;
0228     int n = list.size();
0229     std::map<const BPHRecoCandidate*, const BPHRecoCandidate*>::const_iterator dauIter;
0230     std::map<const BPHRecoCandidate*, const BPHRecoCandidate*>::const_iterator dauIend = daughMap.end();
0231     std::map<const BPHRecoCandidate*, const BPHRecoCandidate*>::const_iterator jpoIter;
0232     std::map<const BPHRecoCandidate*, const BPHRecoCandidate*>::const_iterator jpoIend = jPsiOMap.end();
0233     std::map<const BPHRecoCandidate*, vertex_ref>::const_iterator pvrIter;
0234     std::map<const BPHRecoCandidate*, vertex_ref>::const_iterator pvrIend = pvRefMap.end();
0235     std::map<const BPHRecoCandidate*, compcc_ref>::const_iterator ccrIter;
0236     std::map<const BPHRecoCandidate*, compcc_ref>::const_iterator ccrIend = ccRefMap.end();
0237     for (i = 0; i < n; ++i) {
0238       const T& ptr = list[i];
0239       ccList->push_back(ptr->composite());
0240       pat::CompositeCandidate& cc = ccList->back();
0241       std::string modes;
0242       bool count = false;
0243       addTrackModes("", *ptr, modes, count);
0244       cc.addUserData("trackModes", modes, true);
0245       addTrackModes("trackMode_", *ptr, cc);
0246       if ((pvrIter = pvRefMap.find(ptr.get())) != pvrIend)
0247         cc.addUserData("primaryVertex", pvrIter->second);
0248       const std::vector<std::string>& cNames = ptr->compNames();
0249       int j = 0;
0250       int m = cNames.size();
0251       while (j < m) {
0252         const std::string& compName = cNames[j++];
0253         const BPHRecoCandidate* cptr = ptr->getComp(compName).get();
0254         if ((ccrIter = ccRefMap.find(cptr)) == ccrIend) {
0255           if ((dauIter = daughMap.find(cptr)) != dauIend)
0256             cptr = dauIter->second;
0257           if ((jpoIter = jPsiOMap.find(cptr)) != jpoIend)
0258             cptr = jpoIter->second;
0259         }
0260         if ((ccrIter = ccRefMap.find(cptr)) != ccrIend) {
0261           compcc_ref cref = ccrIter->second;
0262           if (cref.isNonnull())
0263             cc.addUserData("refTo" + compName, cref);
0264         }
0265       }
0266       const BPHPlusMinusCandidate* pmp = dynamic_cast<const BPHPlusMinusCandidate*>(ptr.get());
0267       if (pmp != nullptr) {
0268         cc.addUserInt("cowboy", (pmp->isCowboy() ? +1 : -1));
0269         //        cc.addUserFloat(    "dca", pmp->cAppInRPhi().distance() );
0270       }
0271       if (writeVertex)
0272         cc.addUserData("vertex", ptr->vertex());
0273       if (ptr->isEmpty())
0274         continue;
0275       if (writeVertex)
0276         cc.addUserData("fitVertex", reco::Vertex(*ptr->topDecayVertex()));
0277       if (ptr->isValidFit()) {
0278         const RefCountedKinematicParticle kinPart = ptr->topParticle();
0279         const KinematicState kinStat = kinPart->currentState();
0280         cc.addUserFloat("fitMass", kinStat.mass());
0281         if (writeMomentum)
0282           cc.addUserData("fitMomentum", kinStat.kinematicParameters().momentum());
0283       }
0284     }
0285     typedef std::unique_ptr<pat::CompositeCandidateCollection> ccc_pointer;
0286     edm::OrphanHandle<pat::CompositeCandidateCollection> ccHandle = ev.put(ccc_pointer(ccList), name);
0287     for (i = 0; i < n; ++i) {
0288       const BPHRecoCandidate* ptr = list[i].get();
0289       edm::Ref<pat::CompositeCandidateCollection> ccRef(ccHandle, i);
0290       ccRefMap[ptr] = ccRef;
0291     }
0292     return ccHandle;
0293   }
0294 };
0295 
0296 #endif