CMS 3D CMS Logo

MEtoMEComparitor.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MEtoMEComparitor
4 // Class: MEtoMEComparitor
5 //
13 //
14 // Original Author: jean-roch Vlimant,40 3-A28,+41227671209,
15 // Created: Tue Nov 30 18:55:50 CET 2010
16 //
17 //
18 
19 #include <iostream>
20 #include "MEtoMEComparitor.h"
21 
22 #include "classlib/utils/StringList.h"
23 #include "classlib/utils/StringOps.h"
26 
28 
29 {
30  _moduleLabel = iConfig.getParameter<std::string>("MEtoEDMLabel");
31 
32  _lumiInstance = iConfig.getParameter<std::string>("lumiInstance");
33  _runInstance = iConfig.getParameter<std::string>("runInstance");
34 
35  _process_ref = iConfig.getParameter<std::string>("processRef");
36  _process_new = iConfig.getParameter<std::string>("processNew");
37 
38  _autoProcess=iConfig.getParameter<bool>("autoProcess");
39 
40  _KSgoodness = iConfig.getParameter<double>("KSgoodness");
41  _diffgoodness = iConfig.getParameter<double>("Diffgoodness");
42  _dirDepth = iConfig.getParameter<unsigned int>("dirDepth");
43  _overallgoodness = iConfig.getParameter<double>("OverAllgoodness");
44 
46 
47 
48 }
49 
50 
52 {
53 
54 }
55 
56 void
58 {
59 
60  compare<edm::LuminosityBlock,TH1S>(iLumi,_lumiInstance);
61  compare<edm::LuminosityBlock,TH1F>(iLumi,_lumiInstance);
62  compare<edm::LuminosityBlock,TH1D>(iLumi,_lumiInstance);
63 }
64 
65 void
67 {
68  if (_autoProcess)
69  {
70  const edm::ProcessHistory& iHistory=iRun.processHistory();
71 
73  _process_new=hi->processName();
74  hi++;
75  _process_ref=hi->processName();
76  std::cout<<_process_ref<<" vs "<<_process_new<<std::endl;
77  }
78 }
79 
80 void
82 {
83 
84  compare<edm::Run,TH1S>(iRun,_runInstance);
85  compare<edm::Run,TH1F>(iRun,_runInstance);
86  compare<edm::Run,TH1D>(iRun,_runInstance);
87 
88 }
89 
90 
91 template <class T>
93  _dbe->setCurrentFolder(type);
94  std::type_info const & tp = typeid(*h);
95  if (tp == typeid(TH1S))
96  _dbe->book1S(h->GetName(),dynamic_cast<TH1S*>(const_cast<T*>(h)));
97  else if (tp == typeid(TH1F))
98  _dbe->book1D(h->GetName(),dynamic_cast<TH1F*>(const_cast<T*>(h)));
99  else if (tp == typeid(TH1D))
100  _dbe->book1DD(h->GetName(),dynamic_cast<TH1D*>(const_cast<T*>(h)));
101 }
102 template <class T>
103 void MEtoMEComparitor::keepBadHistograms(const std::string & directory, const T * h_new, const T * h_ref){
104  //put it in a collection rather.
105 
106 
107  std::string d_n(h_new->GetName());
108  d_n+="_diff";
109  T * difference = new T(d_n.c_str(),
110  h_new->GetTitle(),
111  h_new->GetNbinsX(),
112  h_new->GetXaxis()->GetXmin(),
113  h_new->GetXaxis()->GetXmax());
114  difference->Add(h_new);
115  difference->Add(h_ref,-1.);
116 
117  book(directory,"Ref",h_ref);
118  book(directory,"New",h_new);
119  book(directory,"Diff",difference);
120  delete difference;
121 
122 }
123 
124 
125 template <class W,
126  //class Wto,
127  class T>
128 void MEtoMEComparitor::compare(const W& where,const std::string & instance){
129 
130  edm::Handle<MEtoEDM<T> > metoedm_ref;
131  edm::Handle<MEtoEDM<T> > metoedm_new;
132  where.getByLabel(edm::InputTag(_moduleLabel,
133  instance,
134  _process_ref),
135  metoedm_ref);
136  where.getByLabel(edm::InputTag(_moduleLabel,
137  instance,
138  _process_new),
139  metoedm_new);
140 
141  if (metoedm_ref.failedToGet() || metoedm_new.failedToGet()){
142  edm::LogError("ProductNotFound")<<"MEtoMEComparitor did not find his products.";
143  return;
144  }
145 
146  typedef typename MEtoEDM<T>::MEtoEDMObject MEtoEDMObject;
147 
148  const std::vector<MEtoEDMObject> & metoedmobject_ref = metoedm_ref->getMEtoEdmObject();
149  const std::vector<MEtoEDMObject> & metoedmobject_new = metoedm_new->getMEtoEdmObject();
150 
151  typedef std::map<std::string, std::pair<const MEtoEDMObject*, const MEtoEDMObject*> > Mapping;
152  typedef typename std::map<std::string, std::pair<const MEtoEDMObject*, const MEtoEDMObject*> >::iterator Mapping_iterator;
153 
154  Mapping mapping;
155 
156  LogDebug("MEtoMEComparitor")<<"going to do the mapping from "<<metoedmobject_ref.size()<<" x "<<metoedmobject_new.size();
157  unsigned int countMe=0;
158  for (unsigned int i_new=0; i_new!= metoedmobject_new.size(); ++i_new){
159  const std::string & pathname = metoedmobject_new[i_new].name;
160  if (metoedmobject_new[i_new].object.GetEntries()==0 ||
161  metoedmobject_new[i_new].object.Integral()==0){
162  countMe--;
163  continue;
164  }
165  mapping[pathname]=std::make_pair(&metoedmobject_new[i_new],(const MEtoEDMObject*)nullptr);
166  }
167  for (unsigned int i_ref=0; i_ref!= metoedmobject_ref.size() ; ++i_ref){
168  const std::string & pathname = metoedmobject_ref[i_ref].name;
169  Mapping_iterator there = mapping.find(pathname);
170  if (there != mapping.end()){
171  there->second.second = &metoedmobject_ref[i_ref];
172  }
173  }
174 
175  LogDebug("MEtoMEComparitor")<<"found "<<mapping.size()<<" pairs of plots";
176  countMe=0;
177 
178  unsigned int nNoMatch=0;
179  unsigned int nEmpty=0;
180  unsigned int nHollow=0;
181  unsigned int nGoodKS=0;
182  unsigned int nBadKS=0;
183  unsigned int nBadDiff=0;
184  unsigned int nGoodDiff=0;
185 
186  typedef std::map<std::string, std::pair<unsigned int,unsigned int> > Subs;
187  Subs subSystems;
188 
189  for (Mapping_iterator it = mapping.begin();
190  it!=mapping.end();
191  ++it){
192  if (!it->second.second){
193  //this is expected by how the map was created
194  nNoMatch++;
195  continue;
196  }
197  const T * h_ref = &it->second.second->object;
198  const T * h_new = &it->second.first->object;
199 
200  lat::StringList dir = lat::StringOps::split(it->second.second->name,"/");
201  std::string subsystem = dir[0];
202  if (dir.size()>=_dirDepth)
203  for (unsigned int iD=1;iD!=_dirDepth;++iD) subsystem+="/"+dir[iD];
204  subSystems[subsystem].first++;
205 
206  if (h_ref->GetEntries()!=0 && h_ref->Integral()!=0){
207  double KS=0;
208  bool cannotComputeKS=false;
209  try {
210  KS = h_new->KolmogorovTest(h_ref);
211  }
212  catch( cms::Exception& exception ){
213  cannotComputeKS=true;
214  }
215  if (KS<_KSgoodness){
216 
217  unsigned int total_ref=0;
218  unsigned int absdiff=0;
219  for (unsigned int iBin=0;
220  iBin!=(unsigned int)h_new->GetNbinsX()+1 ;
221  ++iBin){
222  total_ref+=h_ref->GetBinContent(iBin);
223  absdiff=std::abs(h_new->GetBinContent(iBin) - h_ref->GetBinContent(iBin));
224  }
225  double relativediff=1;
226  if (total_ref!=0){
227  relativediff=absdiff / (double) total_ref;
228  }
229  if (relativediff > _diffgoodness ){
230  edm::LogWarning("MEtoMEComparitor")<<"for "<<h_new->GetName()
231  <<" in "<<it->first
232  <<" the KS is "<<KS*100.<<" %"
233  <<" and the relative diff is: "<<relativediff*100.<<" %"
234  <<" KS"<<((cannotComputeKS)?" not valid":" is valid");
235  //std::string(" KolmogorovTest is not happy on : ")+h_new->GetName() : "";
236  //there you want to output the plots somewhere
237  keepBadHistograms(subsystem,h_new,h_ref);
238 
239  nBadDiff++;
240  subSystems[subsystem].second++;
241  }else{
242  nGoodDiff++;
243  }
244  nBadKS++;
245  }
246  else
247  nGoodKS++;
248  }
249  else{
250  if (h_ref->GetEntries()==0)
251  nEmpty++;
252  else
253  nHollow++;
254  LogDebug("MEtoMEComparitor")<<h_new->GetName() <<" in "<<it->first <<" is empty";
255  countMe--;
256  }
257 
258  }
259 
260  if (!mapping.empty()){
261  std::stringstream summary;
262  summary<<" Summary :"
263  <<"\n not matched : "<<nNoMatch
264  <<"\n empty : "<<nEmpty
265  <<"\n integral zero : "<<nHollow
266  <<"\n good KS : "<<nGoodKS
267  <<"\n bad KS : "<<nBadKS
268  <<"\n bad diff : "<<nBadDiff
269  <<"\n godd diff : "<<nGoodDiff;
270  bool tell=false;
271  for (Subs::iterator iSub=subSystems.begin();
272  iSub!=subSystems.end();++iSub){
273  double fraction = 1-(iSub->second.second / (double)iSub->second.first);
274  summary<<std::endl<<"Subsytem: "<<iSub->first<<" has "<< fraction*100<<" % goodness";
275  if (fraction < _overallgoodness)
276  tell=true;
277  }
278  if (tell)
279  edm::LogWarning("MEtoMEComparitor")<<summary.str();
280  else
281  edm::LogInfo("MEtoMEComparitor")<<summary.str();
282  }
283 
284 }
285 
286 
287 // ------------ method called once each job just before starting event loop ------------
288 void
290 {
291 }
292 
293 // ------------ method called once each job just after ending the event loop ------------
294 void
296 }
297 
298 
#define LogDebug(id)
const_reverse_iterator rbegin() const
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
std::string _lumiInstance
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
collection_type::const_reverse_iterator const_reverse_iterator
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:1035
static PFTauRenderPlugin instance
void endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) override
void endRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
MEtoMEComparitor(const edm::ParameterSet &)
MonitorElement * book1DD(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1S histogram.
Definition: DQMStore.cc:1067
void compare(const W &where, const std::string &instance)
void endJob() override
void beginJob() override
void book(const std::string &directory, const std::string &type, const T *h)
std::string _process_ref
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ProcessHistory const & processHistory() const
Definition: Run.cc:120
std::string _process_new
~MEtoMEComparitor() override
bool failedToGet() const
Definition: HandleBase.h:78
void keepBadHistograms(const std::string &directory, const T *h_new, const T *h_ref)
void beginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
unsigned int _dirDepth
std::string _runInstance
dbl *** dir
Definition: mlp_gen.cc:35
long double T
double split
Definition: MVATrainer.cc:139
MonitorElement * book1S(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1S histogram.
Definition: DQMStore.cc:1051
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:741
Definition: Run.h:43
std::string _moduleLabel