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