ATLAS Offline Software
AveragePrint.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 #include <TH1.h>
10 #include <TProfile.h>
11 #include <TClass.h>
12 
13 #include "dqm_core/exceptions.h"
14 #include "dqm_core/AlgorithmManager.h"
15 #include "dqm_core/AlgorithmConfig.h"
16 #include "dqm_core/Result.h"
17 #include <ostream>
18 
19 static dqm_algorithms::AveragePrint staticInstance;
20 
21 namespace dqm_algorithms {
22 
23  // *********************************************************************
24  // Public Methods
25  // *********************************************************************
26 
27  void
29  printDescription(std::ostream& out)
30  {
31  std::string message;
32  message += "\n";
33  message += "Algorithm: \"" + m_name + "\"\n";
34  message += "Description: Prints out the average of the histogram or profile bins\n";
35  message += " In the case of a TProfile each bin is weighted by its fraction of the entries\n";
36  message += " Overflow (and Underflow) bins are not included\n";
37  message += "\n";
38 
39  out << message;
40  }
41 
44  : m_name("AveragePrint")
45  {
46  dqm_core::AlgorithmManager::instance().registerAlgorithm( m_name, this );
47  }
48 
49 
52  {
53  }
54 
55 
58  clone()
59  {
60  return new AveragePrint(*this);
61  }
62 
63 
66  execute( const std::string& name, const TObject& data, const dqm_core::AlgorithmConfig& /*config*/)
67  {
68  //No status flags are set
71 
72  if (!data.IsA()->InheritsFrom("TH1")) {
73  throw dqm_core::BadConfig(ERS_HERE, name, "does not inherit from TH1");
74  }
75  const TH1* h = static_cast<const TH1*>(&data); // h cannot be null
76  if (h->GetDimension() > 2) {
77  throw dqm_core::BadConfig(ERS_HERE, name, "dimension > 2 ");
78  }
79 
80  //**********
81  // Profile case
82  //**********
83  if ( data.IsA()->InheritsFrom("TProfile") ) {
84  const TProfile* hp = static_cast<const TProfile*>(&data);
85  //ASSUME: dimension = 1
86  double Average_value = 0.;
87  double Total_entries = 0;
88  int NbinsX = h->GetNbinsX();
89  for(int binX = 0; binX < NbinsX; binX++) {
90  double Bin_entries = hp->GetBinEntries(binX + 1);
91  double Bin_value = Bin_entries * (hp->GetBinContent(binX + 1));
92  Total_entries += Bin_entries;
93  Average_value += Bin_value;
94  }
95  Average_value = Average_value / Total_entries;
96  std::string Average_name = Form("%s_Average", name.c_str());
97  result->tags_[Average_name.c_str()] = Average_value;
98  }
99 
100  //**********
101  // 1D Histogram case
102  //**********
103  if((! data.IsA()->InheritsFrom("TProfile")) && h->GetDimension() == 1) {
104  double Average_value = 0.;
105  int NbinsX = h->GetNbinsX();
106  for(int binX = 0; binX < NbinsX; binX++) {
107  Average_value += h->GetBinContent(binX + 1);
108  }
109  std::string Average_name = Form("%s_Average", name.c_str());
110  result->tags_[Average_name.c_str()] = Average_value;
111  }
112 
113  //**********
114  // 2D Histogram case
115  //**********
116  if((! data.IsA()->InheritsFrom("TProfile")) && h->GetDimension() == 2) {
117  double Average_value = 0.;
118  int NbinsX = h->GetNbinsX();
119  int NbinsY = h->GetNbinsY();
120  for(int binX = 0; binX < NbinsX; binX++) {
121  for(int binY = 0; binY < NbinsY; binY++) {
122  Average_value += h->GetBinContent(binY + 1, binY + 1);
123  }
124  }
125  std::string Average_name = Form("%s_Average", name.c_str());
126  result->tags_[Average_name.c_str()] = Average_value;
127  }
128 
129  return result;
130  }
131 
132 }
dqm_algorithms::AveragePrint::printDescription
virtual void printDescription(std::ostream &out)
Definition: AveragePrint.cxx:29
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
dqm_algorithms::AveragePrint::~AveragePrint
virtual ~AveragePrint()
Definition: AveragePrint.cxx:51
dqm_algorithms::AveragePrint::clone
virtual dqm_core::Algorithm * clone()
Definition: AveragePrint.cxx:58
python.FakeAthena.Algorithm
def Algorithm(name)
Definition: FakeAthena.py:41
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
ReweightUtils.message
message
Definition: ReweightUtils.py:15
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
Trk::binY
@ binY
Definition: BinningType.h:48
AveragePrint.h
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
Trk::binX
@ binX
Definition: BinningType.h:47
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dqm_algorithms::AveragePrint
Definition: AveragePrint.h:16
TProfile
Definition: rootspy.cxx:515
dqm_algorithms::AveragePrint::execute
virtual dqm_core::Result * execute(const std::string &name, const TObject &data, const dqm_core::AlgorithmConfig &config)
Definition: AveragePrint.cxx:66
dqm_algorithms
Definition: AddReference.h:17
h
dqm_algorithms::AveragePrint::AveragePrint
AveragePrint()
Definition: AveragePrint.cxx:43
TH1
Definition: rootspy.cxx:268
TProfile::GetBinContent
double GetBinContent(int) const
Definition: rootspy.cxx:527
dqm_algorithms::AveragePrint::m_name
std::string m_name
Definition: AveragePrint.h:31