CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UpdateTProfile.cc
Go to the documentation of this file.
2 #include "TProfile.h"
3 #include <iostream>
4 #include <math.h>
5 
6 
7 // -----------------------------------------------------------------------------
10 
11 // -----------------------------------------------------------------------------
14 
15 // -----------------------------------------------------------------------------
17 void UpdateTProfile::setBinContents( TProfile* const prof,
18  const uint32_t& bin,
19  const double& num_of_entries,
20  const double& sum_of_contents,
21  const double& sum_of_squares ) {
22  // std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
23 
24  double mean = 0.;
25  double spread = 0.;
26  if ( num_of_entries ) {
27  mean = sum_of_contents / num_of_entries;
28  spread = sqrt( sum_of_squares/ num_of_entries - mean * mean );
29  }
30 
31 // LogTrace("TEST")
32 // << "[UpdateTProfile::setBinContents]"
33 // << " bin: " << bin
34 // << " entries: " << num_of_entries
35 // << " contents: " << sum_of_contents
36 // << " squared: " << sum_of_squares
37 // << " mean: " << mean
38 // << " spread: " << spread;
39 
40  UpdateTProfile::setBinContent( prof, bin, num_of_entries, mean, spread );
41 
42 }
43 
44 // -----------------------------------------------------------------------------
82 void UpdateTProfile::setBinContent( TProfile* const prof,
83  const uint32_t& bin,
84  const double& num_of_entries,
85  const double& mean,
86  const double& spread ) {
87  // std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
88 
89  // Check histo exists
90  if ( !prof ) {
91  std::cerr << "[UpdateTProfile::setBinContents]"
92  << " NULL pointer to TProfile object!" << std::endl;
93  return;
94  }
95 
96  // Check bin number is valid
97  if ( bin == 0 || bin > static_cast<uint32_t>(prof->GetNbinsX()) ) {
98  std::cerr << "[UpdateTProfile::setBinContents]"
99  << " Unexpected bin number!" << std::endl;
100  return;
101  }
102 
103  // Check entries are present
104  if ( num_of_entries <= 0. ) { return; } //@@ what about negative weights???
105  double entries = num_of_entries;
106 
107  // Check error option
108  const char* spread_option = "s";
109  const char* default_option = "";
110  // const char* option = prof->GetErrorOption();
111  // if ( option[0] != spread_option[0] ) {
112  // std::cout << "[UpdateTProfile::setBinContents]"
113  // << " Setting error option for TProfile to 's'!" << std::endl;
114  // prof->SetErrorOption( "s" );
115  // }
116  const char* error_option = prof->GetErrorOption();
117 
118  // Calculate "weight" used for SetBinError() method
119  double weight;
120  if ( error_option[0] == spread_option[0] ) {
121  weight = sqrt( mean*mean*entries + spread*spread*entries );
122  } else if (error_option[0] == default_option[0] ) {
123  weight = sqrt( mean*mean*entries + spread*spread*entries*entries );
124  } else {
125  std::cerr << "[UpdateTProfile::setBinContents]"
126  << " Unexpected error option for TProfile!" << std::endl;
127  weight = 0.;
128  }
129 
130  // Set bin entries, contents and error
131  prof->SetBinEntries( bin, entries );
132  prof->SetBinContent( bin, mean * entries );
133  prof->SetBinError( bin, weight );
134 
135 // LogTrace("TEST")
136 // << "[UpdateTProfile::" << __func__ << "]"
137 // << " bin/entries/mean/content/error: "
138 // << bin << "/"
139 // << entries << "/"
140 // << mean << "/"
141 // << mean*entries << "/"
142 // << weight;
143 
144  // Set total number of entries
145  if ( bin == 1 ) { prof->SetEntries( entries ); }
146  else { prof->SetEntries( prof->GetEntries() + entries ); }
147 
148 }
149 
150 
T sqrt(T t)
Definition: SSEVec.h:18
static void setBinContent(TProfile *const profile, const uint32_t &bin, const double &entries, const double &mean, const double &spread)
static void setBinContents(TProfile *const profile, const uint32_t &bin, const double &num_of_entries, const double &sum_of_contents, const double &sum_of_squares)
int weight
Definition: histoStyle.py:50