Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:18

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 /** Class that makes a TTree with:
0038      - variables from a Probe
0039      - flags saying if the Probe passed one or more selections
0040      - MC flag saying if the Probe is to be used in MC efficiency
0041 */
0042 
0043 namespace tnp {
0044 
0045   /// A variable for the probe: can be a string expression or an external ValueMap<float>
0046   class ProbeVariable {
0047   public:
0048     /// Create a ProbeVariable to be evaluated on the fly from a string expression
0049     ProbeVariable(const std::string &name, const std::string &expression)
0050         : name_(name), external_(false), function_(expression) {}
0051 
0052     /// Create a ProbeVariable to be read from a ValueMap
0053     ProbeVariable(const std::string &name, const edm::EDGetTokenT<edm::ValueMap<float> > &srcToken)
0054         : name_(name), external_(true), function_("-1"), srcToken_(srcToken) {}
0055 
0056     /// Destructor (does nothing)
0057     ~ProbeVariable();
0058 
0059     /// Addess for ROOT Branch
0060     float *address() const { return &value_; }
0061 
0062     /// name
0063     const std::string &name() const { return name_; }
0064 
0065     /// To be called at the beginning of the event (will fetch ValueMap if needed)
0066     void init(const edm::Event &iEvent) const {
0067       if (external_)
0068         iEvent.getByToken(srcToken_, handle_);
0069     }
0070 
0071     /// To be called for each item
0072     void fill(const reco::CandidateBaseRef &probe) const { value_ = external_ ? (*handle_)[probe] : function_(*probe); }
0073 
0074   private:
0075     /// the name of the variable, which becomes the ROOT branch name
0076     std::string name_;
0077     /// the place where we store the value, and that ROOT uses to fill the tree
0078     mutable float value_;
0079 
0080     /// true if it's an external ValueMap, false if it's a StringParser function
0081     bool external_;
0082     // ---- this below is used if 'external_' is false
0083     /// a lazy-parsed StringObjectFunction<reco::Candidate> that gets all the methods of daughter classes too
0084     StringObjectFunction<reco::Candidate, true> function_;
0085     // In releases older than 3.4.X, it can be parially emulated by PATStringObjectFunction (PhysicsTools/PatUtils/interface/StringParserTools.h)
0086     // Or you can use StringObjectFunction<reco::Candidate> and get only reco::Candidate methods
0087 
0088     // ---- this below is used if 'external_' is true
0089     /// the external valuemap
0090     edm::EDGetTokenT<edm::ValueMap<float> > srcToken_;
0091     /// the handle to keep the ValueMap
0092     mutable edm::Handle<edm::ValueMap<float> > handle_;
0093   };
0094 
0095   class ProbeFlag {
0096   public:
0097     /// Create a ProbeFlag to be evaluated on the fly from a string cut
0098     ProbeFlag(const std::string &name, const std::string &cut) : name_(name), external_(false), cut_(cut) {}
0099 
0100     /// Create a ProbeFlag to be read from a ValueMap
0101     ProbeFlag(const std::string &name, const edm::EDGetTokenT<edm::View<reco::Candidate> > &srcToken)
0102         : name_(name), external_(true), cut_(""), srcToken_(srcToken) {}
0103 
0104     /// Destructor (does nothing)
0105     ~ProbeFlag();
0106 
0107     /// Addess for ROOT Branch
0108     int32_t *address() const { return &value_; }
0109 
0110     /// name
0111     const std::string &name() const { return name_; }
0112 
0113     /// To be called at the beginning of the event (will fetch Candidate View if needed)
0114     void init(const edm::Event &iEvent) const;
0115 
0116     /// To be called for each item
0117     void fill(const reco::CandidateBaseRef &probe) const;
0118 
0119   private:
0120     /// the name of the variable, which becomes the ROOT branch name
0121     std::string name_;
0122     /// the place where we store the value, and that ROOT uses to fill the tree
0123     mutable int32_t value_;
0124 
0125     /// true if it's an external Candidate View, false if it's a StringParser cut
0126     bool external_;
0127     // ---- this below is used if 'external_' is false
0128     /// implementation of the cut using a string selector
0129     StringCutObjectSelector<reco::Candidate, true> cut_;
0130 
0131     // ---- this below is used if 'external_' is true
0132     /// the external collection
0133     edm::EDGetTokenT<edm::View<reco::Candidate> > srcToken_;
0134     /// the handle to keep the refs to the passing probes
0135     mutable std::vector<reco::CandidateBaseRef> passingProbes_;
0136   };
0137 
0138   // This class is noncopyable, as copying it would break the addresses in the TTree
0139   class BaseTreeFiller {
0140   public:
0141     BaseTreeFiller(const BaseTreeFiller &) = delete;
0142     BaseTreeFiller &operator=(const BaseTreeFiller &) = delete;
0143     /// specify the name of the TTree, and the configuration for it
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     /// Add branches to an existing TTree managed by another BaseTreeFiller
0149     BaseTreeFiller(BaseTreeFiller &main,
0150                    const edm::ParameterSet &iConfig,
0151                    edm::ConsumesCollector &&iC,
0152                    const std::string &branchNamePrefix);
0153 
0154     /// Destructor, does nothing but it's out-of-line as we have complex data members
0155     ~BaseTreeFiller();
0156 
0157     //implementation notice: we declare 'const' the methods which don't change the configuration
0158     //                       and that can't mess up the addresses in the TTree.
0159 
0160     /// To be called once per event, to load possible external variables
0161     void init(const edm::Event &iEvent) const;
0162 
0163     /// To be called once per probe, to fill the values for this probe
0164     void fill(const reco::CandidateBaseRef &probe) const;
0165 
0166     /// Write a string dump of this PSet into the TTree header.
0167     /// see macro in test directory for how to retrieve it from the output root file
0168     void writeProvenance(const edm::ParameterSet &pset) const;
0169 
0170     //get the pileup weight informations
0171     bool storePUweight() const { return storePUweight_; };
0172 
0173   protected:
0174     std::vector<ProbeVariable> vars_;
0175     std::vector<ProbeFlag> flags_;
0176 
0177     /// How event weights are defined: 'None' = no weights, 'Fixed' = one value specified in cfg file, 'External' = read weight from the event (as double)
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     /// Add branches with run and lumisection number
0197     bool addRunLumiInfo_;
0198 
0199     /// Store Pileup weight when running over Monte Carlo
0200     bool storePUweight_;
0201 
0202     /// Add branches with event variables: met, sum ET, .. etc.
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     //implementation notice: these two are 'mutable' because we will fill them from a 'const' method
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 }  // namespace tnp
0228 
0229 #endif