CMS 3D CMS Logo

PdfSystematicsAnalyzer.cc
Go to the documentation of this file.
5 
7 public:
9  ~PdfSystematicsAnalyzer() override;
10  bool filter(edm::Event &, const edm::EventSetup&) override;
11  void beginJob() override ;
12  void endJob() override ;
13 private:
15  std::vector<edm::InputTag> pdfWeightTags_;
16  std::vector<edm::EDGetTokenT<std::vector<double> > > pdfWeightTokens_;
18  unsigned int originalEvents_;
19  unsigned int selectedEvents_;
20  std::vector<int> pdfStart_;
21  std::vector<double> weightedSelectedEvents_;
22  std::vector<double> weighted2SelectedEvents_;
23  std::vector<double> weightedEvents_;
24 };
25 
33 
35 
37 
40  selectorPath_(pset.getUntrackedParameter<std::string> ("SelectorPath","")),
41  pdfWeightTags_(pset.getUntrackedParameter<std::vector<edm::InputTag> > ("PdfWeightTags")),
42  pdfWeightTokens_(edm::vector_transform(pdfWeightTags_, [this](edm::InputTag const & tag){return consumes<std::vector<double> >(tag);})),
43  triggerResultsToken_(consumes<edm::TriggerResults>(edm::InputTag("TriggerResults"))) {
44 }
45 
48 
51  originalEvents_ = 0;
52  selectedEvents_ = 0;
53  edm::LogVerbatim("PDFAnalysis") << "PDF uncertainties will be determined for the following sets: ";
54  for (unsigned int i=0; i<pdfWeightTags_.size(); ++i) {
55  edm::LogVerbatim("PDFAnalysis") << "\t" << pdfWeightTags_[i].instance();
56  pdfStart_.push_back(-1);
57  }
58 }
59 
62 
63  if (originalEvents_==0) {
64  edm::LogVerbatim("PDFAnalysis") << "NO EVENTS => NO RESULTS";
65  return;
66  }
67  if (selectedEvents_==0) {
68  edm::LogVerbatim("PDFAnalysis") << "NO SELECTED EVENTS => NO RESULTS";
69  return;
70  }
71 
72  edm::LogVerbatim("PDFAnalysis") << "\n>>>> Begin of PDF weight systematics summary >>>>";
73  edm::LogVerbatim("PDFAnalysis") << "Total number of analyzed data: " << originalEvents_ << " [events]";
74  double originalAcceptance = double(selectedEvents_)/originalEvents_;
75  edm::LogVerbatim("PDFAnalysis") << "Total number of selected data: " << selectedEvents_ << " [events], corresponding to acceptance: [" << originalAcceptance*100 << " +- " << 100*sqrt( originalAcceptance*(1.-originalAcceptance)/originalEvents_) << "] %";
76 
77  edm::LogVerbatim("PDFAnalysis") << "\n>>>>> PDF UNCERTAINTIES ON RATE >>>>>>";
78  for (unsigned int i=0; i<pdfWeightTags_.size(); ++i) {
79  bool nnpdfFlag = (pdfWeightTags_[i].instance().substr(0,5)=="NNPDF");
80  unsigned int nmembers = weightedSelectedEvents_.size()-pdfStart_[i];
81  if (i<pdfWeightTags_.size()-1) nmembers = pdfStart_[i+1] - pdfStart_[i];
82  unsigned int npairs = (nmembers-1)/2;
83  edm::LogVerbatim("PDFAnalysis") << "RATE Results for PDF set " << pdfWeightTags_[i].instance() << " ---->";
84 
85  double events_central = weightedSelectedEvents_[pdfStart_[i]];
86  edm::LogVerbatim("PDFAnalysis") << "\tEstimate for central PDF member: " << int(events_central) << " [events]";
87  double events2_central = weighted2SelectedEvents_[pdfStart_[i]];
88  edm::LogVerbatim("PDFAnalysis") << "\ti.e. [" << std::setprecision(4) << 100*(events_central-selectedEvents_)/selectedEvents_ << " +- " <<
89  100*sqrt(events2_central-events_central+selectedEvents_*(1-originalAcceptance))/selectedEvents_
90  << "] % relative variation with respect to original PDF";
91 
92  if (npairs>0) {
93  edm::LogVerbatim("PDFAnalysis") << "\tNumber of eigenvectors for uncertainty estimation: " << npairs;
94  double wplus = 0.;
95  double wminus = 0.;
96  unsigned int nplus = 0;
97  unsigned int nminus = 0;
98  for (unsigned int j=0; j<npairs; ++j) {
99  double wa = weightedSelectedEvents_[pdfStart_[i]+2*j+1]/events_central-1.;
100  double wb = weightedSelectedEvents_[pdfStart_[i]+2*j+2]/events_central-1.;
101  if (nnpdfFlag) {
102  if (wa>0.) {
103  wplus += wa*wa;
104  nplus++;
105  } else {
106  wminus += wa*wa;
107  nminus++;
108  }
109  if (wb>0.) {
110  wplus += wb*wb;
111  nplus++;
112  } else {
113  wminus += wb*wb;
114  nminus++;
115  }
116  } else {
117  if (wa>wb) {
118  if (wa<0.) wa = 0.;
119  if (wb>0.) wb = 0.;
120  wplus += wa*wa;
121  wminus += wb*wb;
122  } else {
123  if (wb<0.) wb = 0.;
124  if (wa>0.) wa = 0.;
125  wplus += wb*wb;
126  wminus += wa*wa;
127  }
128  }
129  }
130  if (wplus>0) wplus = sqrt(wplus);
131  if (wminus>0) wminus = sqrt(wminus);
132  if (nnpdfFlag) {
133  if (nplus>0) wplus /= sqrt(nplus);
134  if (nminus>0) wminus /= sqrt(nminus);
135  }
136  edm::LogVerbatim("PDFAnalysis") << "\tRelative uncertainty with respect to central member: +" << std::setprecision(4) << 100.*wplus << " / -" << std::setprecision(4) << 100.*wminus << " [%]";
137  } else {
138  edm::LogVerbatim("PDFAnalysis") << "\tNO eigenvectors for uncertainty estimation";
139  }
140  }
141 
142  edm::LogVerbatim("PDFAnalysis") << "\n>>>>> PDF UNCERTAINTIES ON ACCEPTANCE >>>>>>";
143  for (unsigned int i=0; i<pdfWeightTags_.size(); ++i) {
144  bool nnpdfFlag = (pdfWeightTags_[i].instance().substr(0,5)=="NNPDF");
145  unsigned int nmembers = weightedEvents_.size()-pdfStart_[i];
146  if (i<pdfWeightTags_.size()-1) nmembers = pdfStart_[i+1] - pdfStart_[i];
147  unsigned int npairs = (nmembers-1)/2;
148  edm::LogVerbatim("PDFAnalysis") << "ACCEPTANCE Results for PDF set " << pdfWeightTags_[i].instance() << " ---->";
149 
150  double acc_central = 0.;
151  double acc2_central = 0.;
152  if (weightedEvents_[pdfStart_[i]]>0) {
154  acc2_central = weighted2SelectedEvents_[pdfStart_[i]]/weightedEvents_[pdfStart_[i]];
155  }
156  double waverage = weightedEvents_[pdfStart_[i]]/originalEvents_;
157  edm::LogVerbatim("PDFAnalysis") << "\tEstimate for central PDF member acceptance: [" << acc_central*100 << " +- " <<
158  100*sqrt((acc2_central/waverage-acc_central*acc_central)/originalEvents_)
159  << "] %";
160  double xi = acc_central-originalAcceptance;
161  double deltaxi = (acc2_central-(originalAcceptance+2*xi+xi*xi))/originalEvents_;
162  if (deltaxi>0) deltaxi = sqrt(deltaxi); //else deltaxi = 0.;
163  edm::LogVerbatim("PDFAnalysis") << "\ti.e. [" << std::setprecision(4) << 100*xi/originalAcceptance << " +- " << std::setprecision(4) << 100*deltaxi/originalAcceptance << "] % relative variation with respect to the original PDF";
164 
165  if (npairs>0) {
166  edm::LogVerbatim("PDFAnalysis") << "\tNumber of eigenvectors for uncertainty estimation: " << npairs;
167  double wplus = 0.;
168  double wminus = 0.;
169  unsigned int nplus = 0;
170  unsigned int nminus = 0;
171  for (unsigned int j=0; j<npairs; ++j) {
172  double wa = 0.;
173  if (weightedEvents_[pdfStart_[i]+2*j+1]>0) wa = (weightedSelectedEvents_[pdfStart_[i]+2*j+1]/weightedEvents_[pdfStart_[i]+2*j+1])/acc_central-1.;
174  double wb = 0.;
175  if (weightedEvents_[pdfStart_[i]+2*j+2]>0) wb = (weightedSelectedEvents_[pdfStart_[i]+2*j+2]/weightedEvents_[pdfStart_[i]+2*j+2])/acc_central-1.;
176  if (nnpdfFlag) {
177  if (wa>0.) {
178  wplus += wa*wa;
179  nplus++;
180  } else {
181  wminus += wa*wa;
182  nminus++;
183  }
184  if (wb>0.) {
185  wplus += wb*wb;
186  nplus++;
187  } else {
188  wminus += wb*wb;
189  nminus++;
190  }
191  } else {
192  if (wa>wb) {
193  if (wa<0.) wa = 0.;
194  if (wb>0.) wb = 0.;
195  wplus += wa*wa;
196  wminus += wb*wb;
197  } else {
198  if (wb<0.) wb = 0.;
199  if (wa>0.) wa = 0.;
200  wplus += wb*wb;
201  wminus += wa*wa;
202  }
203  }
204  }
205  if (wplus>0) wplus = sqrt(wplus);
206  if (wminus>0) wminus = sqrt(wminus);
207  if (nnpdfFlag) {
208  if (nplus>0) wplus /= sqrt(nplus);
209  if (nminus>0) wminus /= sqrt(nminus);
210  }
211  edm::LogVerbatim("PDFAnalysis") << "\tRelative uncertainty with respect to central member: +" << std::setprecision(4) << 100.*wplus << " / -" << std::setprecision(4) << 100.*wminus << " [%]";
212  } else {
213  edm::LogVerbatim("PDFAnalysis") << "\tNO eigenvectors for uncertainty estimation";
214  }
215  }
216  edm::LogVerbatim("PDFAnalysis") << ">>>> End of PDF weight systematics summary >>>>";
217 
218 }
219 
222 
223  edm::Handle<std::vector<double> > weightHandle;
224  for (unsigned int i=0; i<pdfWeightTags_.size(); ++i) {
225  if (!ev.getByToken(pdfWeightTokens_[i], weightHandle)) {
226  if (originalEvents_==0) {
227  edm::LogError("PDFAnalysis") << ">>> WARNING: some weights not found!";
228  edm::LogError("PDFAnalysis") << ">>> But maybe OK, if you are prefiltering!";
229  edm::LogError("PDFAnalysis") << ">>> If things are OK, this warning should disappear after a while!";
230  }
231  return false;
232  }
233  }
234 
235  originalEvents_++;
236 
237  bool selectedEvent = false;
239  if (!ev.getByToken(triggerResultsToken_, triggerResults)) {
240  edm::LogError("PDFAnalysis") << ">>> TRIGGER collection does not exist !!!";
241  return false;
242  }
243 
244 
245  const edm::TriggerNames & trigNames = ev.triggerNames(*triggerResults);
246  unsigned int pathIndex = trigNames.triggerIndex(selectorPath_);
247  bool pathFound = (pathIndex<trigNames.size()); // pathIndex >= 0, since pathIndex is unsigned
248  if (pathFound) {
249  if (triggerResults->accept(pathIndex)) selectedEvent = true;
250  }
251  //edm::LogVerbatim("PDFAnalysis") << ">>>> Path Name: " << selectorPath_ << ", selected? " << selectedEvent;
252 
253  if (selectedEvent) selectedEvents_++;
254 
255  for (unsigned int i=0; i<pdfWeightTags_.size(); ++i) {
256  if (!ev.getByToken(pdfWeightTokens_[i], weightHandle)) return false;
257  std::vector<double> weights = (*weightHandle);
258  unsigned int nmembers = weights.size();
259  // Set up arrays the first time wieghts are read
260  if (pdfStart_[i]<0) {
261  pdfStart_[i] = weightedEvents_.size();
262  for (unsigned int j=0; j<nmembers; ++j) {
263  weightedEvents_.push_back(0.);
264  weightedSelectedEvents_.push_back(0.);
265  weighted2SelectedEvents_.push_back(0.);
266  }
267  }
268 
269  for (unsigned int j=0; j<nmembers; ++j) {
270  weightedEvents_[pdfStart_[i]+j] += weights[j];
271  if (selectedEvent) {
272  weightedSelectedEvents_[pdfStart_[i]+j] += weights[j];
273  weighted2SelectedEvents_[pdfStart_[i]+j] += weights[j]*weights[j];
274  }
275  }
276 
277  /*
278  printf("\n>>>>>>>>> Run %8d Event %d, members %3d PDF set %s : Weights >>>> \n", ev.id().run(), ev.id().event(), nmembers, pdfWeightTags_[i].instance().data());
279  for (unsigned int i=0; i<nmembers; i+=5) {
280  for (unsigned int j=0; ((j<5)&&(i+j<nmembers)); ++j) {
281  printf(" %2d: %7.4f", i+j, weights[i+j]);
282  }
283  safe_printf("\n");
284  }
285  */
286 
287  }
288 
289  return true;
290 }
291 
std::vector< edm::EDGetTokenT< std::vector< double > > > pdfWeightTokens_
std::vector< edm::InputTag > pdfWeightTags_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool accept() const
Has at least one path accepted the event?
PdfSystematicsAnalyzer(const edm::ParameterSet &pset)
std::vector< double > weightedSelectedEvents_
bool ev
Strings::size_type size() const
Definition: TriggerNames.cc:39
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
std::vector< double > weightedEvents_
bool filter(edm::Event &, const edm::EventSetup &) override
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:32
T sqrt(T t)
Definition: SSEVec.h:18
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
std::vector< double > weighted2SelectedEvents_
static std::string const triggerResults
Definition: EdmProvDump.cc:41
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:74
HLT enums.
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:301