File indexing completed on 2023-03-17 11:16:42
0001 #ifndef PhysicsTools_TagAndProbe_BaseTreeFiller_h
0002 #define PhysicsTools_TagAndProbe_BaseTreeFiller_h
0003
0004 #include <vector>
0005 #include <string>
0006 #include <cstdint>
0007
0008 #include "DataFormats/Common/interface/Handle.h"
0009 #include "DataFormats/Common/interface/ValueMap.h"
0010 #include "DataFormats/Candidate/interface/Candidate.h"
0011 #include "DataFormats/Candidate/interface/CandidateFwd.h"
0012 #include "DataFormats/VertexReco/interface/Vertex.h"
0013 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0014 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
0015 #include "DataFormats/PatCandidates/interface/MET.h"
0016 #include "DataFormats/METReco/interface/MET.h"
0017 #include "DataFormats/METReco/interface/METCollection.h"
0018 #include "DataFormats/METReco/interface/CaloMET.h"
0019 #include "DataFormats/METReco/interface/CaloMETCollection.h"
0020 #include "DataFormats/METReco/interface/PFMET.h"
0021 #include "DataFormats/METReco/interface/PFMETCollection.h"
0022 #include "FWCore/Framework/interface/Event.h"
0023 #include "FWCore/Framework/interface/ConsumesCollector.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/Utilities/interface/InputTag.h"
0026 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0027 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0028
0029 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h"
0030 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
0031 #include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"
0032 #include "SimDataFormats/GeneratorProducts/interface/GenLumiInfoHeader.h"
0033 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
0034
0035 #include <TTree.h>
0036
0037
0038
0039
0040
0041
0042
0043 namespace tnp {
0044
0045
0046 class ProbeVariable {
0047 public:
0048
0049 ProbeVariable(const std::string &name, const std::string &expression)
0050 : name_(name), external_(false), function_(expression) {}
0051
0052
0053 ProbeVariable(const std::string &name, const edm::EDGetTokenT<edm::ValueMap<float> > &srcToken)
0054 : name_(name), external_(true), function_("-1"), srcToken_(srcToken) {}
0055
0056
0057 ~ProbeVariable();
0058
0059
0060 float *address() const { return &value_; }
0061
0062
0063 const std::string &name() const { return name_; }
0064
0065
0066 void init(const edm::Event &iEvent) const {
0067 if (external_)
0068 iEvent.getByToken(srcToken_, handle_);
0069 }
0070
0071
0072 void fill(const reco::CandidateBaseRef &probe) const { value_ = external_ ? (*handle_)[probe] : function_(*probe); }
0073
0074 private:
0075
0076 std::string name_;
0077
0078 mutable float value_;
0079
0080
0081 bool external_;
0082
0083
0084 StringObjectFunction<reco::Candidate, true> function_;
0085
0086
0087
0088
0089
0090 edm::EDGetTokenT<edm::ValueMap<float> > srcToken_;
0091
0092 mutable edm::Handle<edm::ValueMap<float> > handle_;
0093 };
0094
0095 class ProbeFlag {
0096 public:
0097
0098 ProbeFlag(const std::string &name, const std::string &cut) : name_(name), external_(false), cut_(cut) {}
0099
0100
0101 ProbeFlag(const std::string &name, const edm::EDGetTokenT<edm::View<reco::Candidate> > &srcToken)
0102 : name_(name), external_(true), cut_(""), srcToken_(srcToken) {}
0103
0104
0105 ~ProbeFlag();
0106
0107
0108 int32_t *address() const { return &value_; }
0109
0110
0111 const std::string &name() const { return name_; }
0112
0113
0114 void init(const edm::Event &iEvent) const;
0115
0116
0117 void fill(const reco::CandidateBaseRef &probe) const;
0118
0119 private:
0120
0121 std::string name_;
0122
0123 mutable int32_t value_;
0124
0125
0126 bool external_;
0127
0128
0129 StringCutObjectSelector<reco::Candidate, true> cut_;
0130
0131
0132
0133 edm::EDGetTokenT<edm::View<reco::Candidate> > srcToken_;
0134
0135 mutable std::vector<reco::CandidateBaseRef> passingProbes_;
0136 };
0137
0138
0139 class BaseTreeFiller {
0140 public:
0141 BaseTreeFiller(const BaseTreeFiller &) = delete;
0142 BaseTreeFiller &operator=(const BaseTreeFiller &) = delete;
0143
0144 BaseTreeFiller(const char *name, const edm::ParameterSet &config, edm::ConsumesCollector &&iC)
0145 : BaseTreeFiller(name, config, iC){};
0146 BaseTreeFiller(const char *name, const edm::ParameterSet &config, edm::ConsumesCollector &iC);
0147
0148
0149 BaseTreeFiller(BaseTreeFiller &main,
0150 const edm::ParameterSet &iConfig,
0151 edm::ConsumesCollector &&iC,
0152 const std::string &branchNamePrefix);
0153
0154
0155 ~BaseTreeFiller();
0156
0157
0158
0159
0160
0161 void init(const edm::Event &iEvent) const;
0162
0163
0164 void fill(const reco::CandidateBaseRef &probe) const;
0165
0166
0167
0168 void writeProvenance(const edm::ParameterSet &pset) const;
0169
0170
0171 bool storePUweight() const { return storePUweight_; };
0172
0173 protected:
0174 std::vector<ProbeVariable> vars_;
0175 std::vector<ProbeFlag> flags_;
0176
0177
0178 enum WeightMode { None, Fixed, External };
0179 WeightMode weightMode_;
0180 bool LHEinfo_;
0181 edm::EDGetTokenT<GenEventInfoProduct> weightSrcToken_;
0182 edm::EDGetTokenT<LHEEventProduct> _LHECollection;
0183 edm::EDGetTokenT<GenLumiInfoHeader> _genLumiInfoToken;
0184 edm::EDGetTokenT<LHERunInfoProduct> _lheRunInfoToken;
0185 edm::EDGetTokenT<reco::GenParticleCollection> genParticlesToken_;
0186 edm::EDGetTokenT<double> PUweightSrcToken_;
0187 edm::EDGetTokenT<double> rhoToken_;
0188 edm::EDGetTokenT<reco::VertexCollection> recVtxsToken_;
0189 edm::EDGetTokenT<reco::BeamSpot> beamSpotToken_;
0190 edm::EDGetTokenT<reco::CaloMETCollection> metToken_;
0191 edm::EDGetTokenT<reco::METCollection> tcmetToken_;
0192 edm::EDGetTokenT<reco::PFMETCollection> pfmetToken_;
0193 edm::EDGetTokenT<pat::METCollection> pfmetTokenMiniAOD_;
0194 edm::EDGetTokenT<std::vector<PileupSummaryInfo> > pileupInfoToken_;
0195
0196
0197 bool addRunLumiInfo_;
0198
0199
0200 bool storePUweight_;
0201
0202
0203 bool addEventVariablesInfo_;
0204 bool addRho_;
0205 bool addCaloMet_;
0206
0207 void addBranches_(TTree *tree,
0208 const edm::ParameterSet &iConfig,
0209 edm::ConsumesCollector &iC,
0210 const std::string &branchNamePrefix = "");
0211
0212
0213 mutable TTree *tree_;
0214 mutable float weight_, PUweight_, totWeight_;
0215 mutable float lheWeight_[9];
0216 mutable float psWeight_[5];
0217 mutable uint32_t run_, lumi_, mNPV_;
0218 mutable uint64_t event_;
0219 mutable int truePU_;
0220
0221 mutable float mPVx_, mPVy_, mPVz_, mBSx_, mBSy_, mBSz_;
0222 mutable float rho_;
0223 mutable float mMET_, mSumET_, mMETSign_, mtcMET_, mtcSumET_, mtcMETSign_, mpfMET_, mpfSumET_, mpfMETSign_, mpfPhi_,
0224 lhe_ht_;
0225 };
0226
0227 }
0228
0229 #endif