ATLAS Offline Software
Loading...
Searching...
No Matches
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
8
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
20
22{
23 dqm_core::AlgorithmManager::instance().registerAlgorithm("TRTWeightedAverage", this);
24}
25
29
34
35dqm_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) {
58 dqm_core::Result *result = new dqm_core::Result(dqm_core::Result::Undefined);
59 result->tags_["InsufficientEntries"] = histogram->GetEntries();
60 return result;
61 }
62
63 if (firstbin>lastbin){
64 dqm_core::Result *result = new dqm_core::Result(dqm_core::Result::Undefined);
65 result->tags_["Range_is_not_correct "] = 1;
66 return result;
67 }
68
69 if (firstbin<0){
70 dqm_core::Result *result = new dqm_core::Result(dqm_core::Result::Undefined);
71 result->tags_["Range_is_not_correct "] = 2;
72 return result;
73 }
74
75 if (lastbin > histogram->GetNbinsX()){
76 dqm_core::Result *result = new dqm_core::Result(dqm_core::Result::Undefined);
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){
118 dqm_core::Result *result = new dqm_core::Result(dqm_core::Result::Undefined);
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}
static dqm_algorithms::BinContentComp myInstance
std::string histogram
Definition chains.cxx:52
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="")
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
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)
virtual void printDescription(std::ostream &out)
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
#define IsA
Declare the TObject style functions.