CMS 3D CMS Logo

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