ATLAS Offline Software
TRTWeightedAverage.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
9 #include "dqm_core/AlgorithmManager.h"
10 #include "dqm_core/AlgorithmConfig.h"
13 #include "TH1.h"
14 #include "ers/ers.h"
15 
16 #include <string>
17 #include <sstream>
18 
19 static dqm_algorithms::TRTWeightedAverage myInstance;
20 
22 {
23  dqm_core::AlgorithmManager::instance().registerAlgorithm("TRTWeightedAverage", this);
24 }
25 
27 {
28 }
29 
31 {
32  return new TRTWeightedAverage();
33 }
34 
35 dqm_core::Result *dqm_algorithms::TRTWeightedAverage::execute(const std::string &name, const TObject &object, const dqm_core::AlgorithmConfig &config)
36 {
37  const TH1 * histogram;
38  const TH1 * refhist;
39 
40  if (object.IsA()->InheritsFrom("TH1")) {
41  histogram = static_cast<const TH1*>(&object);
42  if (histogram->GetDimension() > 1) {
43  throw dqm_core::BadConfig(ERS_HERE, name, "dimension > 1");
44  }
45  } else {
46  throw dqm_core::BadConfig(ERS_HERE, name, "does not inherit from TH1");
47  }
48 
49  const double minstat = dqm_algorithms::tools::GetFirstFromMap("MinStat", config.getParameters(), -1);
50  const double firstbin = dqm_algorithms::tools::GetFirstFromMap("FirstBin", config.getParameters(), -1);
51  const double lastbin = dqm_algorithms::tools::GetFirstFromMap("LastBin", config.getParameters(), -1);
52  const double expectedaverage = dqm_algorithms::tools::GetFirstFromMap("ExpectedAverage", config.getParameters(), -1);
53  const double p0 = dqm_algorithms::tools::GetFirstFromMap("P0", config.getParameters(), -1);
54  const double p1 = dqm_algorithms::tools::GetFirstFromMap("P1", config.getParameters(), -1);
55  const double correctusingbin1 = dqm_algorithms::tools::GetFirstFromMap("CorrectUsingBin1", config.getParameters(), -1);
56 
57  if (histogram->GetEntries() < minstat) {
59  result->tags_["InsufficientEntries"] = histogram->GetEntries();
60  return result;
61  }
62 
63  if (firstbin>lastbin){
65  result->tags_["Range_is_not_correct "] = 1;
66  return result;
67  }
68 
69  if (firstbin<0){
71  result->tags_["Range_is_not_correct "] = 2;
72  return result;
73  }
74 
75  if (lastbin > histogram->GetNbinsX()){
77  result->tags_["Range_is_not_correct "] = 3;
78  return result;
79  }
80 
81  try {
82  refhist = dynamic_cast<const TH1*>( config.getReference() );
83  }
84  catch ( dqm_core::Exception & ex ) {
85  throw dqm_core::BadRefHist(ERS_HERE,name," Could not retreive reference");
86  }
87 
88  if (!refhist) {
89  throw dqm_core::BadRefHist(ERS_HERE, name, "Reference is not a histogram");
90  }
91 
92  if (histogram->GetDimension() != refhist->GetDimension() ) {
93  throw dqm_core::BadRefHist( ERS_HERE, name, "Dimension" );
94  }
95 
96  if ((histogram->GetNbinsX() != refhist->GetNbinsX()) || (histogram->GetNbinsY() != refhist->GetNbinsY())) {
97  throw dqm_core::BadRefHist( ERS_HERE, name, "Non-matching number of bins of reference" );
98  }
99 
100  std::map<std::string, double> results; // you can set flagging thresholds on any of these result tags
101 
102  double mean = 0;
103  double sum = 0;
104  double sumcontent = 0;
105  double CorrectedMean= 0;
106 
107  for (int i = firstbin; i <= lastbin; ++i) {
108 
109  const double binContent = histogram->GetBinContent(i);
110  const double binCenter = histogram->GetXaxis()->GetBinCenter(i);
111 
112  sum += binContent*binCenter;
113  sumcontent += binContent;
114  }
115 
116 
117  if (sumcontent==0){
119  result->tags_["Content_is_zero "] = 1;
120  return result;
121  }
122 
123  mean = sum/sumcontent;
124 
125  if (correctusingbin1>0){
126  double bin0=histogram->GetBinContent(1);
127  double Nentries=histogram->GetEntries();
128  double correctionfactor=bin0/Nentries;
129  CorrectedMean= mean-(p0+p1*correctionfactor);
130  }
131 
132  results["Weighted_mean"] = mean;
133  results["Abs_Diff"] = std::fabs(mean-expectedaverage);
134  if (correctusingbin1>0){
135  results["Corrected_Weighted_mean"]= CorrectedMean+expectedaverage;
136  results["Corrected_Abs_Diff"]=std::fabs(CorrectedMean);
137  }else{
138  results["Corrected_Weighted_mean"]=0;
139  results["Corrected_Abs_Diff"]=0;
140  }
141  //return result;
142  return tools::MakeComparisons(results, config.getGreenThresholds(), config.getRedThresholds());
143 }
145 {
146  //Modify following part:
147  out << "TRTWeightedAverage: Calculates weighted average of the bins within the selected bin range. It is also possible to correct these results using the normalized value of the 0th bin. This is helpful for TRT drift time histogram. In the case of the correction algorithm expects the coefficients p0 and p1 such that (weighted_mean-p0-p1*normalized_firtbin_value) is 0 on the average. \n"
148  "Optional parameter: MinStat :\n"
149  "Optional parameter: FirstBin :\n"
150  "Optional parameter: LastBin :\n"
151  "Optional parameter: ExpectedAverage :\n"
152  "Optional parameter: P0 :\n"
153  "Optional parameter: P1 :\n"
154  "Optional parameter: CorrectUsing0Bin :\n"
155  "Returned values: \n"
156  " Weighted_mean: weighted average result for the selected bins. \n"
157  " Abs_Diff: Absolute value of the difference between Weighted_mean and ExpectedAverage, fabs(Weighted_mean-ExpectedAverage) \n"
158  " Corrected_Weighted_mean: Weighted_mean-(p0+p1*normalized_bin_0)+ExpectedAverage , 0 if CorrectUsing0Bin==0 \n"
159  " Corrected_Abs_Diff: fabs(Weighted_mean-(p0+p1*normalized_bin_0)), 0 if CorrectUsing0Bin==0 \n"
160  << std::endl;
161 }
dqm_algorithms::TRTWeightedAverage::~TRTWeightedAverage
~TRTWeightedAverage()
Definition: TRTWeightedAverage.cxx:26
Undefined
@ Undefined
Definition: MaterialTypes.h:8
mean
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:254
get_generator_info.result
result
Definition: get_generator_info.py:21
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
TRTWeightedAverage.h
dqm_algorithms::TRTWeightedAverage::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: TRTWeightedAverage.cxx:35
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
dqm_algorithms::TRTWeightedAverage::TRTWeightedAverage
TRTWeightedAverage()
Definition: TRTWeightedAverage.cxx:21
lumiFormat.i
int i
Definition: lumiFormat.py:92
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
dqm_algorithms::tools::MakeComparisons
dqm_core::Result * MakeComparisons(const std::map< std::string, double > &algparams, const std::map< std::string, double > &gthreshold, const std::map< std::string, double > &rthreshold)
Definition: AlgorithmHelper.cxx:60
dqm_algorithms::TRTWeightedAverage
Definition: TRTWeightedAverage.h:15
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
python.ami.results
def results
Definition: ami.py:386
TH1
Definition: rootspy.cxx:268
AlgorithmHelper.h
pickleTool.object
object
Definition: pickleTool.py:30
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339
dqm_algorithms::TRTWeightedAverage::printDescription
virtual void printDescription(std::ostream &out)
Definition: TRTWeightedAverage.cxx:144
dqm_algorithms::TRTWeightedAverage::clone
TRTWeightedAverage * clone()
Definition: TRTWeightedAverage.cxx:30
histogram
std::string histogram
Definition: chains.cxx:52