CMS 3D CMS Logo

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