CMS 3D CMS Logo

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