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_;
34  std::vector<std::string> pdfSetNames_;
35  std::vector<std::string> pdfShortNames_;
36 };
37 
39 namespace LHAPDF {
40  void initPDFSet(int nset, const std::string& filename, int member=0);
41  int numberPDF(int nset);
42  void usePDFMember(int nset, int member);
43  double xfx(int nset, double x, double Q, int fl);
44  double getXmin(int nset, int member);
45  double getXmax(int nset, int member);
46  double getQ2min(int nset, int member);
47  double getQ2max(int nset, int member);
48  void extrapolate(bool extrapolate=true);
49 }
50 
53  fixPOWHEG_(pset.getUntrackedParameter<std::string> ("FixPOWHEG", "")),
54  useFirstAsDefault_(pset.getUntrackedParameter<bool>("useFirstAsDefault",false)),
55  genTag_(pset.getUntrackedParameter<edm::InputTag> ("GenTag", edm::InputTag("genParticles"))),
56  pdfInfoTag_(pset.getUntrackedParameter<edm::InputTag> ("PdfInfoTag", edm::InputTag("generator"))),
57  pdfSetNames_(pset.getUntrackedParameter<std::vector<std::string> > ("PdfSetNames"))
58 {
59  if (fixPOWHEG_ != "") pdfSetNames_.insert(pdfSetNames_.begin(),fixPOWHEG_);
60 
61  if (pdfSetNames_.size()>3) {
62  edm::LogWarning("") << pdfSetNames_.size() << " PDF sets requested on input. Using only the first 3 sets and ignoring the rest!!";
63  pdfSetNames_.erase(pdfSetNames_.begin()+3,pdfSetNames_.end());
64  }
65 
66  for (unsigned int k=0; k<pdfSetNames_.size(); k++) {
67  size_t dot = pdfSetNames_[k].find_first_of('.');
68  size_t underscore = pdfSetNames_[k].find_first_of('_');
69  if (underscore<dot) {
70  pdfShortNames_.push_back(pdfSetNames_[k].substr(0,underscore));
71  } else {
72  pdfShortNames_.push_back(pdfSetNames_[k].substr(0,dot));
73  }
74  produces<std::vector<double> >(pdfShortNames_[k].data());
75  }
76 }
77 
80 
83  for (unsigned int k=1; k<=pdfSetNames_.size(); k++) {
85  }
86 }
87 
90 
93 
94  if (iEvent.isRealData()) return;
95 
97  if (!iEvent.getByLabel(pdfInfoTag_, pdfstuff)) {
98  edm::LogError("PDFWeightProducer") << ">>> PdfInfo not found: " << pdfInfoTag_.encode() << " !!!";
99  return;
100  }
101 
102  float Q = pdfstuff->pdf()->scalePDF;
103 
104  int id1 = pdfstuff->pdf()->id.first;
105  if ( id1 == 21 ) { id1 = 0; }
106  double x1 = pdfstuff->pdf()->x.first;
107  double pdf1 = pdfstuff->pdf()->xPDF.first;
108 
109  int id2 = pdfstuff->pdf()->id.second;
110  if ( id2 == 21 ) { id2 = 0; }
111  double x2 = pdfstuff->pdf()->x.second;
112  double pdf2 = pdfstuff->pdf()->xPDF.second;
113  // if (useFirstAsDefault_ && pdf1 == -1. && pdf2 == -1. ) {
114  if (useFirstAsDefault_ ) {
116  pdf1 = LHAPDF::xfx(1, x1, Q, id1)/x1;
117  pdf2 = LHAPDF::xfx(1, x2, Q, id2)/x2;
118  }
119 
120  // std::cout << "Orig PDF " << Q << " " << id1 << " " << x1 << " " << pdf1 << " " << id2 << " " << x2 << " " << pdf2 << std::endl;
121 
122  // Ad-hoc fix for POWHEG
123  if (fixPOWHEG_!="") {
125  if (!iEvent.getByLabel(genTag_, genParticles)) {
126  edm::LogError("PDFWeightProducer") << ">>> genParticles not found: " << genTag_.encode() << " !!!";
127  return;
128  }
129  unsigned int gensize = genParticles->size();
130  double mboson = 0.;
131  for(unsigned int i = 0; i<gensize; ++i) {
132  const reco::GenParticle& part = (*genParticles)[i];
133  int status = part.status();
134  if (status!=3) continue;
135  int id = part.pdgId();
136  if (id!=23 && abs(id)!=24) continue;
137  mboson = part.mass();
138  break;
139  }
140  Q = sqrt(mboson*mboson+Q*Q);
142  pdf1 = LHAPDF::xfx(1, x1, Q, id1)/x1;
143  pdf2 = LHAPDF::xfx(1, x2, Q, id2)/x2;
144  }
145 
146  // Put PDF weights in the event
147  for (unsigned int k=1; k<=pdfSetNames_.size(); ++k) {
148  std::auto_ptr<std::vector<double> > weights (new std::vector<double>);
149  unsigned int nweights = 1;
150  if (LHAPDF::numberPDF(k)>1) nweights += LHAPDF::numberPDF(k);
151  weights->reserve(nweights);
152 
153  for (unsigned int i=0; i<nweights; ++i) {
155  double newpdf1 = LHAPDF::xfx(k, x1, Q, id1)/x1;
156  double newpdf2 = LHAPDF::xfx(k, x2, Q, id2)/x2;
157  weights->push_back(newpdf1/pdf1*newpdf2/pdf2);
158 
159  // std::cout << i << " " << pdfSetNames_[k-1] << " " << Q << " " << id1 << " " << x1 << " " << newpdf1 << " " << id2 << " " << x2 << " " << newpdf2 << " " << (*weights)[i] << std::endl;
160  }
161  iEvent.put(weights,pdfShortNames_[k-1]);
162  }
163 }
164 
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
Double_t pdf1(double mHstar, double mHreq)
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:85
T sqrt(T t)
Definition: SSEVec.h:46
void extrapolate(bool extrapolate=true)
int numberPDF(int nset)
double getXmin(int nset, int member)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
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_
T dot(const Basic3DVector &v) const
Scalar product, or &quot;dot&quot; product, with a vector of same type.
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
x
Definition: VDTMath.h:216
virtual void endJob()
void usePDFMember(int nset, int member)