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