CMS 3D CMS Logo

SummaryGenerator.cc
Go to the documentation of this file.
6 #include <iostream>
7 #include <sstream>
8 #include <cmath>
9 #include "TH2F.h"
10 #include "TProfile.h"
11 
12 using namespace sistrip;
13 
14 // -----------------------------------------------------------------------------
15 //
17  : map_(), entries_(-1.), max_(-1. * sistrip::invalid_), min_(1. * sistrip::invalid_), label_(""), myName_(name) {
18  // TH1::SetDefaultSumw2(true); // use square of weights to calc error
19 }
20 
21 // -----------------------------------------------------------------------------
22 //
24  SummaryGenerator* generator = nullptr;
25  if (view == sistrip::CONTROL_VIEW) {
26  generator = new SummaryGeneratorControlView();
27  } else if (view == sistrip::READOUT_VIEW) {
28  generator = new SummaryGeneratorReadoutView();
29  } else {
30  generator = nullptr;
31  }
32 
33  if (generator) {
34  LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
35  << " Built \"" << generator->myName() << "\" object!";
36  } else {
37  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
38  << " Unexpected view: \"" << SiStripEnumsAndStrings::view(view)
39  << "\" Unable to build Generator!"
40  << " Returning NULL pointer!";
41  }
42 
43  return generator;
44 }
45 
46 // -----------------------------------------------------------------------------
47 //
50  const sistrip::Presentation& pres,
51  const sistrip::View& view,
52  const std::string& directory) {
53  std::stringstream ss;
59 
60  //LogTrace(mlSummaryPlots_)
61  //<< "[SummaryGenerator::" << __func__ << "]"
62  //<< " Histogram name: \"" << ss.str() << "\"";
63 
64  return ss.str();
65 }
66 
67 // -----------------------------------------------------------------------------
68 //
69 /*
70  fix nbins for 1D distribution? to 1024? then change within summary
71  methods with SetBins() methods? but must limit nbins to < 1024!!!
72 
73 */
74 TH1* SummaryGenerator::histogram(const sistrip::Presentation& pres, const uint32_t& xbins) {
75  if (!xbins) {
76  return nullptr;
77  }
78 
79  TH1* summary = nullptr;
80  if (pres == sistrip::HISTO_1D) {
81  summary = new TH1F("", "", 1024, 0., static_cast<float>(1024));
82  } else if (pres == sistrip::HISTO_2D_SUM) {
83  summary = new TH1F("", "", xbins, 0., static_cast<float>(xbins));
84  } else if (pres == sistrip::HISTO_2D_SCATTER) {
85  summary = new TH2F("", "", 100 * xbins, 0., static_cast<float>(100 * xbins), 1025, 0., 1025.);
86  } else if (pres == sistrip::PROFILE_1D) {
87  summary = new TProfile("", "", xbins, 0., static_cast<float>(xbins), 0., 1025.);
88  } else {
89  summary = nullptr;
90  }
91 
92  if (summary) {
93  LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
94  << " Histogram name: \"" << summary->GetName() << "\"";
95  } else {
96  edm::LogVerbatim(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
97  << " Unexpected presentation: \"" << SiStripEnumsAndStrings::presentation(pres)
98  << "\" Unable to build summary plot!"
99  << " Returning NULL pointer!";
100  }
101 
102  return summary;
103 }
104 
105 // -----------------------------------------------------------------------------
106 //
108  const sistrip::Monitorable& mon,
109  const sistrip::Presentation& pres,
110  const sistrip::View& view,
111  const std::string& directory,
112  const sistrip::Granularity& gran,
113  TH1& summary_histo) {
114  // Set name, title and entries
115  //std::stringstream ss;
116  //std::string name = SummaryGenerator::name( run_type, mon, pres, view, directory );
117  //summary_histo.SetName( name.c_str() );
118  //summary_histo.SetTitle( name.c_str() );
119  if (entries_ >= 0.) {
120  summary_histo.SetEntries(entries_);
121  }
122 
123  // X axis
124  summary_histo.GetXaxis()->SetLabelSize(0.03);
125  summary_histo.GetXaxis()->SetTitleSize(0.03);
126  summary_histo.GetXaxis()->SetTitleOffset(3.5);
127  //gPad->SetBottomMargin(0.2);
128 
129  // Y axis
130  summary_histo.GetYaxis()->SetLabelSize(0.03);
131  summary_histo.GetYaxis()->SetTitleSize(0.03);
132  summary_histo.GetYaxis()->SetTitleOffset(1.5);
133  //gPad->SetLeftMargin(0.2);
134 
135  // Axis label
136  if (pres == sistrip::HISTO_1D) {
137  std::string xtitle = label_ + " (for " + directory + ")";
138  summary_histo.GetXaxis()->SetTitle(xtitle.c_str());
139  summary_histo.GetYaxis()->SetTitle("Frequency");
140  summary_histo.GetXaxis()->SetTitleOffset(1.5); //@@ override value set above
141  } else {
143  summary_histo.GetXaxis()->SetTitle(xtitle.c_str());
144  summary_histo.GetYaxis()->SetTitle(label_.c_str());
145  //summary_histo.GetXaxis()->SetTitleOffset(1.5); //@@ override value set above (3.5?)
146  }
147 
148  // Formatting for 2D plots
149  if (pres == sistrip::HISTO_2D_SCATTER) {
150  // Markers (open circles)
151  summary_histo.SetMarkerStyle(2);
152  summary_histo.SetMarkerSize(0.6);
153  }
154 
155  // Semi-generic formatting
156  if (pres == sistrip::HISTO_2D_SUM || pres == sistrip::HISTO_2D_SCATTER || pres == sistrip::PROFILE_1D) {
157  /*
158  //put solid and dotted lines on summary to separate top- and
159  //2nd-from-top- level bin groups.
160 
161  uint16_t topLevel = 0, topLevelOld = 0, secondLevel = 0, secondLevelOld = 0;
162  std::string::size_type pos = 0;
163  for ( HistoData::iterator ibin = map_.begin(); ibin != map_.end(); ibin++) {
164 
165  //draw line if top and second level numbers change.
166  pos = ibin->first.find(sistrip::dot_,0);
167  if (pos != std::string::npos) {
168  if ((topLevel=atoi(std::string(ibin->first,0,pos).c_str())) != topLevelOld) {
169  topLevel = topLevelOld;
170  //
171  }
172  else if (ibin->first.find(sistrip::dot_,pos+1) != std::string::npos) {
173  if ((secondLevelOld=atoi(std::string(ibin->first,pos+1,(ibin->first.find(sistrip::dot_,pos+1)- (pos+1))).c_str())) != secondLevel) {
174  secondLevel = secondLevelOld;
175  //
176  }}}
177  }
178  */
179  }
180 }
181 
182 // -----------------------------------------------------------------------------
183 //
185  HistoData::iterator iter = map_.begin();
186  for (; iter != map_.end(); iter++) {
187  iter->second.clear();
188  }
189  map_.clear();
190  entries_ = -1.;
191  max_ = -1. * sistrip::invalid_;
192  min_ = 1. * sistrip::invalid_;
193 }
194 
195 // -----------------------------------------------------------------------------
196 //
198  std::stringstream ss;
199  ss << "[SummaryGenerator::" << __func__ << "]"
200  << " Printing contents of map: " << std::endl;
201 
202  HistoData::iterator iter = map_.begin();
203  for (; iter != map_.end(); iter++) {
204  ss << " bin/entries: " << iter->first << "/" << iter->second.size() << " ";
205  if (!iter->second.empty()) {
206  ss << " value/error: ";
207  std::vector<Data>::const_iterator jter = iter->second.begin();
208  for (; jter != iter->second.end(); jter++) {
209  ss << jter->first << "/" << jter->second << " ";
210  }
211  }
212  ss << std::endl;
213  }
214 
215  ss << " Max value: " << max_ << std::endl << " Min value: " << min_ << std::endl;
216 
217  LogTrace(mlSummaryPlots_) << ss.str();
218 }
219 
220 // -----------------------------------------------------------------------------
221 //
222 void SummaryGenerator::fillMap(const std::string& top_level_dir,
223  const sistrip::Granularity& gran,
224  const uint32_t& device_key,
225  const float& value,
226  const float& error) {
227  // Check if value is valid
228  if (value > 1. * sistrip::valid_) {
229  return;
230  }
231 
232  // Calculate maximum and minimum values in std::map
233  if (value > max_) {
234  max_ = value;
235  }
236  if (value < min_) {
237  min_ = value;
238  }
239 
240  // Check if error is valid
241  if (error < 1. * sistrip::valid_) {
242  fill(top_level_dir, gran, device_key, value, error);
243  } else {
244  fill(top_level_dir, gran, device_key, value, 0.);
245  }
246 }
247 
248 // -----------------------------------------------------------------------------
249 //
250 void SummaryGenerator::fill(const std::string& top_level_dir,
251  const sistrip::Granularity& gran,
252  const uint32_t& device_key,
253  const float& value,
254  const float& error) {
255  LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
256  << " Derived implementation does not exist!...";
257 }
258 
259 //------------------------------------------------------------------------------
260 //
262  // Check number of entries in map
263  if (map_.empty()) {
264  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
265  << " No contents in std::map to histogram!";
266  return;
267  }
268 
269  // Retrieve histogram
270  TH1F* histo = dynamic_cast<TH1F*>(&his);
271  if (!histo) {
272  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
273  << " NULL pointer to TH1F histogram!";
274  return;
275  }
276 
277  // Calculate bin range
278  int32_t high = static_cast<int32_t>(fabs(max_) > 20. ? max_ + 0.05 * fabs(max_) : max_ + 1.);
279  int32_t low = static_cast<int32_t>(fabs(min_) > 20. ? min_ - 0.05 * fabs(min_) : min_ - 1.);
280  int32_t range = high - low;
281 
282  // increase number of bins for floats
283  // if ( max_ - static_cast<int32_t>(max_) > 1.e-6 &&
284  // min_ - static_cast<int32_t>(min_) > 1.e-6 ) {
285  // range = 100 * range;
286  // }
287 
288  // Set histogram binning
289  histo->SetBins(range, static_cast<float>(low), static_cast<float>(high));
290 
291  // Iterate through std::map, set bin labels and fill histogram
292  entries_ = 0.;
293  HistoData::const_iterator ibin = map_.begin();
294  for (; ibin != map_.end(); ibin++) {
295  if (ibin->second.empty()) {
296  continue;
297  }
298  BinData::const_iterator ii = ibin->second.begin();
299  for (; ii != ibin->second.end(); ii++) {
300  // bin (value) and weight (error)
301  histo->Fill(ii->first); //, ii->second );
302  entries_++;
303  }
304  }
305 }
306 
307 //------------------------------------------------------------------------------
308 //
310  // Check number of entries in map
311  if (map_.empty()) {
312  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
313  << " No contents in std::map to histogram!";
314  return;
315  }
316 
317  // Retrieve histogram
318  TH1F* histo = dynamic_cast<TH1F*>(&his);
319  if (!histo) {
320  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
321  << " NULL pointer to TH1F histogram!";
322  return;
323  }
324 
325  // Iterate through map, set bin labels and fill histogram
326  entries_ = 0.;
327  uint16_t bin = 0;
328  HistoData::const_iterator ibin = map_.begin();
329  for (; ibin != map_.end(); ibin++) {
330  bin++;
331  histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
332  if (ibin->second.empty()) {
333  continue;
334  }
335  BinData::const_iterator ii = ibin->second.begin();
336  for (; ii != ibin->second.end(); ii++) {
337  // x (bin), y (value) and weight (error)
338  histo->Fill(static_cast<Double_t>(bin - 0.5),
339  static_cast<Double_t>(ii->first)); //, ii->second );
340  entries_ += 1. * ii->first;
341  }
342  }
343 }
344 
345 //------------------------------------------------------------------------------
346 //
348  // Check number of entries in map
349  if (map_.empty()) {
350  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
351  << " No contents in std::map to histogram!";
352  return;
353  }
354 
355  // Retrieve histogram
356  TH2F* histo = dynamic_cast<TH2F*>(&his);
357  if (!histo) {
358  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
359  << " NULL pointer to TH2F histogram!";
360  return;
361  }
362 
363  // Iterate through std::map, set bin labels and fill histogram
364  entries_ = 0.;
365  uint16_t bin = 0;
366  HistoData::const_iterator ibin = map_.begin();
367  for (; ibin != map_.end(); ibin++) {
368  bin++;
369  histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
370  if (ibin->second.empty()) {
371  continue;
372  }
373  BinData::const_iterator ii = ibin->second.begin();
374  for (; ii != ibin->second.end(); ii++) {
375  // x (bin), y (value) and weight (error)
376  histo->Fill(static_cast<Double_t>(bin - 0.5),
377  static_cast<Double_t>(ii->first)); // , ii->second );
378  entries_++;
379  }
380  }
381 }
382 
383 //------------------------------------------------------------------------------
384 //
386  // Check number of entries in map
387  if (map_.empty()) {
388  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
389  << " No contents in std::map to histogram!";
390  return;
391  }
392 
393  // Retrieve histogram
394  TProfile* histo = dynamic_cast<TProfile*>(&his);
395  if (!histo) {
396  edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
397  << " NULL pointer to TProfile histogram!";
398  return;
399  }
400 
401  // Iterate through std::map, set bin labels and fill histogram
402  entries_ = 0.;
403  uint16_t bin = 0;
404  HistoData::const_iterator ibin = map_.begin();
405  for (; ibin != map_.end(); ibin++) {
406  bin++;
407  histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
408  if (ibin->second.empty()) {
409  continue;
410  }
411  BinData::const_iterator ii = ibin->second.begin();
412  for (; ii != ibin->second.end(); ii++) {
413  // x (bin), y (value) and weight (error)
414  histo->Fill(static_cast<Double_t>(bin - .5),
415  static_cast<Double_t>(ii->first)); //, ii->second );
416  entries_++;
417  }
418  }
419 }
static std::string name(const sistrip::RunType &, const sistrip::Monitorable &, const sistrip::Presentation &, const sistrip::View &, const std::string &directory)
static std::string monitorable(const sistrip::Monitorable &)
static const char summaryHisto_[]
void histo2DSum(TH1 &)
const double xbins[]
static std::string granularity(const sistrip::Granularity &)
static SummaryGenerator * instance(const sistrip::View &)
: Fills summary histograms.
SummaryGenerator(std::string name)
void histo2DScatter(TH1 &)
void clear()
Definition: HistoData.h:95
static const uint16_t valid_
Definition: Constants.h:17
sistrip classes
static std::string view(const sistrip::View &)
static std::string runType(const sistrip::RunType &)
static const char sep_[]
void fillMap(const std::string &top_level_dir, const sistrip::Granularity &, const uint32_t &key, const float &value, const float &error=0.)
virtual void fill(const std::string &top_level_dir, const sistrip::Granularity &, const uint32_t &key, const float &value, const float &error)
Fills "summary histograms" in FEC or "control" view.
Definition: value.py:1
#define LogTrace(id)
ii
Definition: cuy.py:590
void format(const sistrip::RunType &, const sistrip::Monitorable &, const sistrip::Presentation &, const sistrip::View &, const std::string &directory, const sistrip::Granularity &, TH1 &)
static const char mlSummaryPlots_[]
const std::string & myName() const
static std::string presentation(const sistrip::Presentation &)
Fills "summary histograms" in FED or "readout" view.
static const uint16_t invalid_
Definition: Constants.h:16
static TH1 * histogram(const sistrip::Presentation &, const uint32_t &xbins)