CMS 3D CMS Logo

PDFWeightsTest.cc
Go to the documentation of this file.
15 #include "TTree.h"
16 #include <iostream>
17 using namespace std;
18 using namespace edm;
19 using namespace HepMC;
20 
22 private:
27 
28  unsigned int pdfWeightOffset_;
29  unsigned int nPdfWeights_;
30  unsigned int nPdfEigWeights_;
31 
32  TTree* tree_;
33  std::vector<float> pdfweights_;
34  std::vector<float> pdfeigweights_;
35  float weight_;
36 
37 public:
38  explicit PDFWeightsTest(const ParameterSet& cfg)
39  : srcToken_(consumes<LHEEventProduct>(edm::InputTag("externalLHEProducer"))),
40  srcTokenAlt_(consumes<LHEEventProduct>(edm::InputTag("source"))),
41  srcTokenGen_(consumes<GenEventInfoProduct>(edm::InputTag("generator"))),
42  pdfWeightOffset_(cfg.getParameter<unsigned int>("pdfWeightOffset")),
43  nPdfWeights_(cfg.getParameter<unsigned int>("nPdfWeights")),
44  nPdfEigWeights_(cfg.getParameter<unsigned int>("nPdfEigWeights")),
45  pdfweights_(nPdfWeights_),
46  pdfeigweights_(nPdfEigWeights_) {
48  tree_ = fs->make<TTree>("tree", "");
49 
50  tree_->Branch("pdfrep", &pdfweights_);
51  tree_->Branch("pdfeig", &pdfeigweights_);
52  tree_->Branch("weight", &weight_);
53 
54  edm::FileInPath mc2hessianCSV = cfg.getParameter<edm::FileInPath>("mc2hessianCSV");
55  pdfweightshelper_.Init(nPdfWeights_, nPdfEigWeights_, mc2hessianCSV);
56  }
57 
58 private:
59  void analyze(const Event& evt, const EventSetup& es) override {
61  evt.getByToken(srcToken_, lheInfo);
62 
63  if (!lheInfo.isValid()) {
64  evt.getByToken(srcTokenAlt_, lheInfo);
65  }
66 
67  double nomlheweight = lheInfo->weights()[0].wgt;
68 
70  evt.getByToken(srcTokenGen_, genInfo);
71 
72  weight_ = genInfo->weight();
73 
74  //get the original mc replica weights
75  std::vector<double> inpdfweights(nPdfWeights_);
76  for (unsigned int ipdf = 0; ipdf < nPdfWeights_; ++ipdf) {
77  unsigned int iwgt = ipdf + pdfWeightOffset_;
78 
79  //this is the weight to be used for evaluating uncertainties with mc replica weights
80  pdfweights_[ipdf] = lheInfo->weights()[iwgt].wgt * weight_ / nomlheweight;
81 
82  //this is the raw weight to be fed to the mc2hessian convertor
83  inpdfweights[ipdf] = lheInfo->weights()[iwgt].wgt;
84  }
85 
86  std::vector<double> outpdfweights(nPdfEigWeights_);
87  //do the actual conversion, where the nominal lhe weight is needed as the reference point for the linearization
88  pdfweightshelper_.DoMC2Hessian(nomlheweight, inpdfweights.data(), outpdfweights.data());
89 
90  for (unsigned int iwgt = 0; iwgt < nPdfEigWeights_; ++iwgt) {
91  double wgtval = outpdfweights[iwgt];
92 
93  //the is the weight to be used for evaluating uncertainties with hessian weights
94  pdfeigweights_[iwgt] = wgtval * weight_ / nomlheweight;
95  }
96 
97  tree_->Fill();
98  }
99 };
100 
102 
unsigned int nPdfWeights_
EDGetTokenT< LHEEventProduct > srcToken_
std::vector< float > pdfeigweights_
void Init(unsigned int nreplicas, unsigned int neigenvectors, const edm::FileInPath &incsv)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:540
void DoMC2Hessian(double nomweight, const double *inweights, double *outweights) const
unsigned int pdfWeightOffset_
EDGetTokenT< GenEventInfoProduct > srcTokenGen_
unsigned int nPdfEigWeights_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
PDFWeightsHelper pdfweightshelper_
std::vector< float > pdfweights_
void analyze(const Event &evt, const EventSetup &es) override
HLT enums.
PDFWeightsTest(const ParameterSet &cfg)
EDGetTokenT< LHEEventProduct > srcTokenAlt_