CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PdfWeightProducer.cc
Go to the documentation of this file.
1 #include <memory>
2 
5 
8 
10 
15 #include <Math/VectorUtil.h>
16 
17 //
18 // class declaration
19 //
21  public:
22  explicit PdfWeightProducer(const edm::ParameterSet&);
24 
25  private:
26  virtual void beginJob() ;
27  virtual void produce(edm::Event&, const edm::EventSetup&);
28  virtual void endJob() ;
29 
30  std::string fixPOWHEG_;
33  std::vector<std::string> pdfSetNames_;
34  std::vector<std::string> pdfShortNames_;
35 };
36 
38 namespace LHAPDF {
39  void initPDFSet(int nset, const std::string& filename, int member=0);
40  int numberPDF(int nset);
41  void usePDFMember(int nset, int member);
42  double xfx(int nset, double x, double Q, int fl);
43  double getXmin(int nset, int member);
44  double getXmax(int nset, int member);
45  double getQ2min(int nset, int member);
46  double getQ2max(int nset, int member);
47  void extrapolate(bool extrapolate=true);
48 }
49 
52  fixPOWHEG_(pset.getUntrackedParameter<std::string> ("FixPOWHEG", "")),
53  genTag_(pset.getUntrackedParameter<edm::InputTag> ("GenTag", edm::InputTag("genParticles"))),
54  pdfInfoTag_(pset.getUntrackedParameter<edm::InputTag> ("PdfInfoTag", edm::InputTag("generator"))),
55  pdfSetNames_(pset.getUntrackedParameter<std::vector<std::string> > ("PdfSetNames"))
56 {
57  if (fixPOWHEG_ != "") pdfSetNames_.insert(pdfSetNames_.begin(),fixPOWHEG_);
58 
59  if (pdfSetNames_.size()>3) {
60  edm::LogWarning("") << pdfSetNames_.size() << " PDF sets requested on input. Using only the first 3 sets and ignoring the rest!!";
61  pdfSetNames_.erase(pdfSetNames_.begin()+3,pdfSetNames_.end());
62  }
63 
64  for (unsigned int k=0; k<pdfSetNames_.size(); k++) {
65  size_t dot = pdfSetNames_[k].find_first_of('.');
66  size_t underscore = pdfSetNames_[k].find_first_of('_');
67  if (underscore<dot) {
68  pdfShortNames_.push_back(pdfSetNames_[k].substr(0,underscore));
69  } else {
70  pdfShortNames_.push_back(pdfSetNames_[k].substr(0,dot));
71  }
72  produces<std::vector<double> >(pdfShortNames_[k].data());
73  }
74 }
75 
78 
81  for (unsigned int k=1; k<=pdfSetNames_.size(); k++) {
83  }
84 }
85 
88 
91 
92  if (iEvent.isRealData()) return;
93 
95  if (!iEvent.getByLabel(pdfInfoTag_, pdfstuff)) {
96  edm::LogError("PDFWeightProducer") << ">>> PdfInfo not found: " << pdfInfoTag_.encode() << " !!!";
97  return;
98  }
99 
100  float Q = pdfstuff->pdf()->scalePDF;
101 
102  int id1 = pdfstuff->pdf()->id.first;
103  double x1 = pdfstuff->pdf()->x.first;
104  double pdf1 = pdfstuff->pdf()->xPDF.first;
105 
106  int id2 = pdfstuff->pdf()->id.second;
107  double x2 = pdfstuff->pdf()->x.second;
108  double pdf2 = pdfstuff->pdf()->xPDF.second;
109 
110  // Ad-hoc fix for POWHEG
111  if (fixPOWHEG_!="") {
113  if (!iEvent.getByLabel(genTag_, genParticles)) {
114  edm::LogError("PDFWeightProducer") << ">>> genParticles not found: " << genTag_.encode() << " !!!";
115  return;
116  }
117  unsigned int gensize = genParticles->size();
118  double mboson = 0.;
119  for(unsigned int i = 0; i<gensize; ++i) {
120  const reco::GenParticle& part = (*genParticles)[i];
121  int status = part.status();
122  if (status!=3) continue;
123  int id = part.pdgId();
124  if (id!=23 && abs(id)!=24) continue;
125  mboson = part.mass();
126  break;
127  }
128  Q = sqrt(mboson*mboson+Q*Q);
130  pdf1 = LHAPDF::xfx(1, x1, Q, id1)/x1;
131  pdf2 = LHAPDF::xfx(1, x2, Q, id2)/x2;
132  }
133 
134  // Put PDF weights in the event
135  for (unsigned int k=1; k<=pdfSetNames_.size(); ++k) {
136  std::auto_ptr<std::vector<double> > weights (new std::vector<double>);
137  unsigned int nweights = 1;
138  if (LHAPDF::numberPDF(k)>1) nweights += LHAPDF::numberPDF(k);
139  weights->reserve(nweights);
140 
141  for (unsigned int i=0; i<nweights; ++i) {
143  double newpdf1 = LHAPDF::xfx(k, x1, Q, id1)/x1;
144  double newpdf2 = LHAPDF::xfx(k, x2, Q, id2)/x2;
145  weights->push_back(newpdf1/pdf1*newpdf2/pdf2);
146  }
147  iEvent.put(weights,pdfShortNames_[k-1]);
148  }
149 }
150 
int i
Definition: DBlmapReader.cc:9
virtual int pdgId() const
PDG identifier.
ProductID id() const
Definition: HandleBase.cc:15
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual int status() const
status word
virtual void produce(edm::Event &, const edm::EventSetup &)
#define abs(x)
Definition: mlp_lapack.h:159
T dot(const Basic3DVector &v) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
edm::InputTag genTag_
bool isRealData() const
Definition: EventBase.h:60
std::string encode() const
Definition: InputTag.cc:72
void initPDFSet(int nset, const std::string &filename, int member=0)
std::vector< std::string > pdfSetNames_
double getXmax(int nset, int member)
double getQ2max(int nset, int member)
virtual double mass() const
mass
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
T sqrt(T t)
Definition: SSEVec.h:28
void extrapolate(bool extrapolate=true)
int numberPDF(int nset)
tuple pset
Definition: CrabTask.py:85
double getXmin(int nset, int member)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
PdfWeightProducer(const edm::ParameterSet &)
int k[5][pyjets_maxn]
virtual void beginJob()
edm::InputTag pdfInfoTag_
part
Definition: HCALResponse.h:21
std::vector< std::string > pdfShortNames_
double xfx(int nset, double x, double Q, int fl)
tuple filename
Definition: lut2db_cfg.py:20
double getQ2min(int nset, int member)
tuple status
Definition: ntuplemaker.py:245
virtual void endJob()
void usePDFMember(int nset, int member)