File indexing completed on 2024-04-06 12:15:38
0001 #include "HeavyFlavorAnalysis/SpecificDecay/test/stubs/CheckBPHWriteDecay.h"
0002
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004
0005 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoBuilder.h"
0006 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoSelect.h"
0007 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHRecoCandidate.h"
0008 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHPlusMinusCandidate.h"
0009 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHMomentumSelect.h"
0010 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHVertexSelect.h"
0011 #include "HeavyFlavorAnalysis/RecoDecay/interface/BPHTrackReference.h"
0012
0013 #include "DataFormats/PatCandidates/interface/Muon.h"
0014 #include "DataFormats/TrackReco/interface/Track.h"
0015
0016 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0017 #include "DataFormats/PatCandidates/interface/CompositeCandidate.h"
0018 #include "RecoVertex/KinematicFitPrimitives/interface/KinematicState.h"
0019
0020 #include "CommonTools/Statistics/interface/ChiSquaredProbability.h"
0021
0022 #include "TH1.h"
0023 #include "TFile.h"
0024
0025 #include <set>
0026 #include <string>
0027 #include <iostream>
0028 #include <fstream>
0029
0030 using namespace std;
0031
0032 #define GET_PAR(TYPE, NAME, PSET) PSET.getParameter<TYPE>(#NAME)
0033
0034
0035
0036 #define SET_PAR(TYPE, NAME, PSET) (NAME = GET_PAR(TYPE, NAME, PSET))
0037
0038
0039
0040
0041
0042 #define GET_UTP(TYPE, NAME, PSET) PSET.getUntrackedParameter<TYPE>(#NAME)
0043 #define SET_UTP(TYPE, NAME, PSET) (NAME = GET_UTP(TYPE, NAME, PSET))
0044
0045 CheckBPHWriteDecay::CheckBPHWriteDecay(const edm::ParameterSet& ps) {
0046 SET_PAR(unsigned int, runNumber, ps);
0047 SET_PAR(unsigned int, evtNumber, ps);
0048 SET_PAR(vector<string>, candsLabel, ps);
0049 SET_UTP(bool, writePtr, ps);
0050
0051 int i;
0052 int n = candsLabel.size();
0053 candsToken.resize(n);
0054 for (i = 0; i < n; ++i)
0055 consume<vector<pat::CompositeCandidate> >(candsToken[i], candsLabel[i]);
0056
0057 string fileName = GET_UTP(string, fileName, ps);
0058 if (fileName.empty())
0059 osPtr = &cout;
0060 else
0061 osPtr = new ofstream(fileName.c_str());
0062 }
0063
0064 void CheckBPHWriteDecay::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0065 edm::ParameterSetDescription desc;
0066 vector<string> v;
0067 desc.add<vector<string> >("candsLabel", v);
0068 desc.add<unsigned int>("runNumber", 0);
0069 desc.add<unsigned int>("evtNumber", 0);
0070 desc.addUntracked<string>("fileName", "");
0071 desc.addUntracked<bool>("writePtr", false);
0072 descriptions.add("checkBPHWriteDecay", desc);
0073 return;
0074 }
0075
0076 void CheckBPHWriteDecay::beginJob() { return; }
0077
0078 void CheckBPHWriteDecay::analyze(const edm::Event& ev, const edm::EventSetup& es) {
0079 ostream& os = *osPtr;
0080
0081 if ((runNumber != 0) && (ev.id().run() != runNumber))
0082 return;
0083 if ((evtNumber != 0) && (ev.id().event() != evtNumber))
0084 return;
0085 os << "--------- event " << ev.id().run() << " / " << ev.id().event() << " ---------" << endl;
0086
0087 int il;
0088 int nl = candsLabel.size();
0089 vector<edm::Handle<vector<pat::CompositeCandidate> > > clist(nl);
0090 for (il = 0; il < nl; ++il) {
0091 edm::Handle<vector<pat::CompositeCandidate> >& cands = clist[il];
0092 candsToken[il].get(ev, cands);
0093 int ic;
0094 int nc = cands->size();
0095 vector<const pat::CompositeCandidate*> csort(nc);
0096 for (ic = 0; ic < nc; ++ic)
0097 csort[ic] = &cands->at(ic);
0098 sort(csort.begin(), csort.end(), [](const pat::CompositeCandidate* lc, const pat::CompositeCandidate* rc) {
0099 return lc->pt() < rc->pt();
0100 });
0101 for (ic = 0; ic < nc; ++ic) {
0102 os << "*********** " << candsLabel[il] << " " << ic << "/" << nc << " ***********" << endl;
0103 const pat::CompositeCandidate* cand = csort[ic];
0104 dump(os, *cand);
0105 idMap[cand] = ic;
0106 }
0107 }
0108 idMap.clear();
0109 return;
0110 }
0111
0112 void CheckBPHWriteDecay::endJob() { return; }
0113
0114 void CheckBPHWriteDecay::dump(ostream& os, const pat::CompositeCandidate& cand) {
0115 float mfit = (cand.hasUserFloat("fitMass") ? cand.userFloat("fitMass") : -1);
0116 if (writePtr)
0117 os << &cand;
0118 os << " mass : " << cand.mass() << " " << mfit
0119 << (cand.hasUserInt("cowboy") ? (cand.userInt("cowboy") > 0 ? " cowboy" : " sailor") : "") << endl;
0120 writeCylindric(os, "cmom ", cand, false);
0121 writeCartesian(os, " xyz ", cand.momentum());
0122 if (cand.hasUserData("trackModes"))
0123 os << "trackModes: " << *cand.userData<string>("trackModes") << endl;
0124 const reco::Vertex* vptr = (cand.hasUserData("vertex") ? cand.userData<reco::Vertex>("vertex") : nullptr);
0125 if (vptr != nullptr) {
0126 writeCartesian(os, "vpos : ", *vptr, false);
0127 os << " --- " << vptr->chi2() << " / " << vptr->ndof() << " ( " << ChiSquaredProbability(vptr->chi2(), vptr->ndof())
0128 << " )" << endl;
0129 }
0130 const reco::Vertex* vfit = (cand.hasUserData("fitVertex") ? cand.userData<reco::Vertex>("fitVertex") : nullptr);
0131 if (vfit != nullptr) {
0132 writeCartesian(os, "vfit : ", *vfit, false);
0133 os << " --- " << vfit->chi2() << " / " << vfit->ndof() << " ( " << ChiSquaredProbability(vfit->chi2(), vfit->ndof())
0134 << " )" << endl;
0135 }
0136 if (cand.hasUserData("fitMomentum"))
0137 writeCartesian(os, "fmom : ", *cand.userData<Vector3DBase<float, GlobalTag> >("fitMomentum"));
0138
0139 if (cand.hasUserData("primaryVertex")) {
0140 const vertex_ref* pvr = cand.userData<vertex_ref>("primaryVertex");
0141 if (pvr->isNonnull()) {
0142 const reco::Vertex* pvtx = pvr->get();
0143 if (pvtx != nullptr)
0144 writeCartesian(os, "ppos ", *pvtx);
0145 }
0146 }
0147 const pat::CompositeCandidate::role_collection& dNames = cand.roles();
0148 int i;
0149 int n = cand.numberOfDaughters();
0150 for (i = 0; i < n; ++i) {
0151 const reco::Candidate* dptr = cand.daughter(i);
0152 const string& nDau = dNames[i];
0153 string tDau = "trackMode_" + nDau;
0154 os << "daug " << i << "/" << n;
0155 os << ' ' << nDau;
0156 if (writePtr)
0157 os << " : " << dptr;
0158 writeCylindric(os, " == ", *dptr, false);
0159 os << " " << dptr->mass() << " " << dptr->charge();
0160 if (cand.hasUserData(tDau))
0161 os << ' ' << *cand.userData<string>(tDau);
0162 os << endl;
0163 const pat::Muon* mptr = dynamic_cast<const pat::Muon*>(dptr);
0164 os << "muon " << i << "/" << n << " : " << (mptr == nullptr ? 'N' : 'Y') << endl;
0165 const reco::Track* tptr = BPHTrackReference::getTrack(*dptr, "cfhpmnigs");
0166 os << "trk " << i << "/" << n;
0167 if (writePtr)
0168 os << " : " << tptr;
0169 if (tptr != nullptr)
0170 writeCylindric(os, " == ", *tptr);
0171 else
0172 os << "no track" << endl;
0173 }
0174 const vector<string>& names = cand.userDataNames();
0175 map<const pat::CompositeCandidate*, int>::const_iterator iter;
0176 map<const pat::CompositeCandidate*, int>::const_iterator iend = idMap.end();
0177 int j;
0178 int m = names.size();
0179 for (j = 0; j < m; ++j) {
0180 const string& dname = names[j];
0181 if (dname.substr(0, 5) != "refTo")
0182 continue;
0183 const compcc_ref* ref = cand.userData<compcc_ref>(dname);
0184 const pat::CompositeCandidate* cptr = ref->get();
0185 os << dname << " : " << (cptr == nullptr ? -2 : ((iter = idMap.find(cptr)) == iend ? -1 : iter->second));
0186 if (writePtr)
0187 os << " : " << cptr;
0188 os << endl;
0189 }
0190
0191 return;
0192 }
0193
0194 #include "FWCore/Framework/interface/MakerMacros.h"
0195
0196 DEFINE_FWK_MODULE(CheckBPHWriteDecay);