ATLAS Offline Software
MDTTDCOfflineSpectrum.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include <dqm_core/AlgorithmConfig.h>
9 #include <TF1.h>
10 #include <TClass.h>
11 #include <ers/ers.h>
12 
13 #include <iostream>
14 
15 #include <dqm_core/AlgorithmManager.h>
16 
17 namespace
18 {
19  dqm_algorithms::MDTTDCOfflineSpectrum MDTTDCOfflineSpectrum( "AlgMDTTDCOfflineSpectrum" );
20 }
21 
22 
24  : m_name( name )
25 {
26  dqm_core::AlgorithmManager::instance().registerAlgorithm(name, this);
27 }
28 
31 {
32 
33  return new MDTTDCOfflineSpectrum( m_name );
34 }
35 
36 
39  const TObject & object,
40  const dqm_core::AlgorithmConfig & config )
41 {
42 
43 
44  if (!object.IsA()->InheritsFrom("TH1")) {
45  throw dqm_core::BadConfig(ERS_HERE, name, "does not inherit from TH1");
46  }
47  std::unique_ptr<TH1> histogram(static_cast<TH1 *>(object.Clone())); // we just checked that this is really a TH1, so we can safely type-cast the pointer
48  if (histogram->GetDimension() > 2) {
49  throw dqm_core::BadConfig(ERS_HERE, name, "dimension > 2");
50  }
51 
52  const double minstat = dqm_algorithms::tools::GetFirstFromMap( "MinStat", config.getParameters(), -1);
53 
54  if (histogram->GetEntries() < minstat ) {
56  result->tags_["InsufficientEntries"] = histogram->GetEntries();
57  return result;
58  }
59 
60  double t0_low_warning;
61  double t0_low_error;
62  double t0_high_warning;
63  double t0_high_error;
64  double tmax_low_warning;
65  double tmax_low_error;
66  double tmax_high_warning;
67  double tmax_high_error;
68  try {
69  t0_low_warning = dqm_algorithms::tools::GetFirstFromMap( "t0_low_Warning", config.getParameters() );
70  t0_low_error = dqm_algorithms::tools::GetFirstFromMap( "t0_low_Error", config.getParameters() );
71  t0_high_warning = dqm_algorithms::tools::GetFirstFromMap( "t0_high_Warning", config.getParameters() );
72  t0_high_error = dqm_algorithms::tools::GetFirstFromMap( "t0_high_Error", config.getParameters() );
73  tmax_low_warning = dqm_algorithms::tools::GetFirstFromMap( "tMax_low_Warning", config.getParameters() );
74  tmax_low_error = dqm_algorithms::tools::GetFirstFromMap( "tMax_low_Error", config.getParameters() );
75  tmax_high_warning = dqm_algorithms::tools::GetFirstFromMap( "tMax_high_Warning", config.getParameters() );
76  tmax_high_error = dqm_algorithms::tools::GetFirstFromMap( "tMax_high_Error", config.getParameters() );
77  }
78  catch ( dqm_core::Exception & ex ) {
79  throw dqm_core::BadConfig( ERS_HERE, name, ex.what(), ex );
80  }
81 
82 
84 
85  double t0;
86  double tmax;
87  double tdrift;
88  double t0Err;
89  double tmaxErr;
90 
91  TF1* t0Fit = histogram->GetFunction("func1");
92  TF1* tmaxFit = histogram->GetFunction("func2");
93 
94  if(!t0Fit || !tmaxFit){
95  MDTFitTDC(histogram.get(), t0, t0Err, tmax, tmaxErr);
96  t0Fit = histogram->GetFunction("func1");
97  tmaxFit = histogram->GetFunction("func2");
98  tdrift = tmax - t0;
99  if(!t0Fit || !tmaxFit) throw dqm_core::BadConfig( ERS_HERE, name, "TH1 has no TF1" );
100  }else{
101  t0 = t0Fit->GetParameter(1);
102  tmax = tmaxFit->GetParameter(1);
103  tdrift = tmax - t0;
104  t0Err = t0Fit->GetParameter(2);
105  tmaxErr = tmaxFit->GetParameter(2);
106  }
107 
108  ERS_DEBUG(1, m_name << " TDrift " << " is " << tdrift );
109  ERS_DEBUG(1,"Green threshold: "<< t0_low_warning << " < t0 < "<< t0_high_warning << " && " << tmax_low_warning <<" < tmax < " << tmax_high_warning <<
110  " ; Red threshold : t0 < " << t0_low_error << "\n" <<
111  "t0 > " << t0_high_error << "\n" <<
112  "tmax > " << tmax_high_error << "\n" <<
113  "tmax < " << tmax_low_error
114  );
115 
116  std::map<std::string,double> tags;
117 
118  if (t0 > t0_low_warning && t0 < t0_high_warning && tmax < tmax_high_warning && tmax > tmax_low_warning && std::abs(tdrift) < 1000) {
119  result->status_ = dqm_core::Result::Green;
120  }
121  else if (t0 > t0_low_error && t0 < t0_high_error && tmax < tmax_high_error && tmax > tmax_low_error && std::abs(tdrift) < 1200) {
122  if(t0 < t0_low_warning) tags["t0_low_Warning"] = t0;
123  else if(t0 > t0_high_warning) tags["t0_high_Warning"] = t0;
124  else tags["t0"] = t0;
125  if(tmax > tmax_high_warning) tags["tMax_high_Warning"] = tmax;
126  else if(tmax < tmax_low_warning) tags["tMax_low_Warning"] = tmax;
127  else tags["tMax"] = tmax;
128  if( std::abs(tdrift) > 1200 ) tags["tDrift_Warning"] = tdrift;
129  else tags["tdrift"] = tdrift;
130  result->status_ = dqm_core::Result::Yellow;
131  }
132  else {
133  result->status_ = dqm_core::Result::Red;
134  if(t0 < t0_low_error) tags["t0_low_Error"] = t0;
135  else if(t0 > t0_high_error) tags["t0_high_Error"] = t0;
136  else tags["t0"] = t0;
137  if(tmax > tmax_high_error) tags["tMax_high_Error"] = tmax;
138  else if(tmax < tmax_low_error) tags["tMax_low_Error"] = tmax;
139  else tags["tMax"] = tmax;
140  if( std::abs(tdrift) > 1400 ) tags["tDrift_Error"] = tdrift;
141  else tags["tdrift"] = tdrift;
142  }
143 
144  if(tags.size()==0) {
145  tags["t0"] = t0;
146  tags["tmax"] = tmax;
147  tags["tdrift"] = tdrift;
148  }
149  if(t0Err > 2.*t0){
150  tags["t0_FitError"] = t0Err;
151  result->status_ = dqm_core::Result::Red;
152  }
153  if(tmaxErr > 2.*tmax) {
154  tags["tmax_FitError"] = tmaxErr;
155  result->status_ = dqm_core::Result::Red;
156  }
157 
158  result->tags_ = tags;
159 
160  return result;
161 
162 }
163 
164 void
166 {
167 
168  out << "Analyze pre-fitted MDTTDC spectra histograms" << std::endl;
169  out << "Required Parameters: name " << m_name << ", t0Warning threshold" << ", tMaxWarning threshold" << std::endl;
170  out << "Required parameters: t0Error" << ", tMaxError " << std::endl;
171  out << "Returns yellow if t0 < t0Warning or t0 > 800 or if tMax > tMaxWarning or tMax < 800" << std::endl;
172  out << "Returns red if t0 < t0Error or t0 > 1000 or if tMax > tMaxError or tMax < 700" << std::endl;
173  out << "Returns red if t0Err > 2.*t0 or tMaxErr > 2.*tMax" << std::endl;
174  out << "Requires that a histogram has a function named \"func1\" for the t0 fit and \"func2\" for the tMax fit" << std::endl;
175 
176 }
177 
178 void dqm_algorithms::MDTTDCOfflineSpectrum::MDTFitTDC(TH1* h, double &t0, double &t0err, double &tmax, double &tmaxerr)
179 {
180  t0 = tmax = 0;
181  t0err = tmaxerr = 0;
182  double up = h->GetBinCenter(h->GetMaximumBin()+1);
183  if( up > 200 ) up = 200;
184  double down = up + 650;
185  if( up < 50 ) up = 50;
186  double parESD0 = h->GetBinContent(h->GetMinimumBin());
187  double parESD1 = up;
188  double parESD2 = 20;
189  double parESD3 = h->GetBinContent(h->GetMaximumBin()) - h->GetBinContent(h->GetMinimumBin());
190  std::unique_ptr<TF1> func1 = std::make_unique<TF1>("func1", "[0]+([3]/(1+(TMath::Exp((-x+[1])/[2]))))", 0, up); // tzero
191  func1->SetParameters(parESD0, parESD1, parESD2, parESD3);
192  if(h->GetEntries()>100){
193  h->Fit("func1","RQ");
194  t0 = func1->GetParameter(1) ;
195  t0err = func1->GetParError(1);
196  double binAtT0 = (double)h->GetBinContent(h->FindBin(t0));
197  if(binAtT0<1) binAtT0 = 1;
198  t0err += 10.0 * func1->GetChisquare() / (0.01*binAtT0*binAtT0*(double)func1->GetNumberFitPoints()); // to additionally account for bad fits
199  }
200 
201  parESD0 = h->GetBinContent(h->GetMinimumBin());
202  parESD1 = down;
203  parESD2 = 50;
204  parESD3 = (h->GetBinContent(h->GetMaximumBin())-h->GetBinContent(h->GetMinimumBin()))/10.;
205  std::unique_ptr<TF1> func2 = std::make_unique<TF1>("func2", "[0]+([3]/(1+(TMath::Exp((x-[1])/[2]))))", down-135, down+135); // tmax
206  func2->SetParameters(parESD0,parESD1,parESD2,parESD3);
207  if(h->GetEntries()>100){
208  func2->SetParLimits(0, parESD0, 2.0*parESD0+1);
209  func2->SetParLimits(2, 5, 90);
210  func2->SetParLimits(3, 0.2*parESD3, 7*parESD3);
211  h->Fit("func2","WWRQ+");
212  tmax = func2->GetParameter(1);
213  tmaxerr = func2->GetParError(1);
214  double binAtTmax = (double)h->GetBinContent(h->FindBin(tmax));
215  if(binAtTmax<1) binAtTmax = 1;
216  tmaxerr += 10.0 * func2->GetChisquare() / (0.01*binAtTmax*binAtTmax*(double)func2->GetNumberFitPoints()); // to additionally account for bad fits
217  }
218 
219 }
Undefined
@ Undefined
Definition: MaterialTypes.h:8
get_generator_info.result
result
Definition: get_generator_info.py:21
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
ALFA_EventTPCnv_Dict::t0
std::vector< ALFA_RawData_p1 > t0
Definition: ALFA_EventTPCnvDict.h:42
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
dqm_algorithms::MDTTDCOfflineSpectrum::MDTTDCOfflineSpectrum
MDTTDCOfflineSpectrum(const std::string &name)
Definition: MDTTDCOfflineSpectrum.cxx:23
tags
std::vector< std::string > tags
Definition: hcg.cxx:102
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
dqm_algorithms::MDTTDCOfflineSpectrum::MDTFitTDC
void MDTFitTDC(TH1 *h, double &t0, double &t0err, double &tmax, double &tmaxerr)
Definition: MDTTDCOfflineSpectrum.cxx:178
CalibCoolCompareRT.up
up
Definition: CalibCoolCompareRT.py:109
MDTTDCOfflineSpectrum.h
python.handimod.Green
int Green
Definition: handimod.py:524
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
python.handimod.Red
Red
Definition: handimod.py:551
dqm_algorithms::MDTTDCOfflineSpectrum::execute
dqm_core::Result * execute(const std::string &, const TObject &, const dqm_core::AlgorithmConfig &)
Definition: MDTTDCOfflineSpectrum.cxx:38
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
dqm_algorithms::MDTTDCOfflineSpectrum::clone
MDTTDCOfflineSpectrum * clone()
Definition: MDTTDCOfflineSpectrum.cxx:30
h
MdtMonUtils.MDTFitTDC
def MDTFitTDC(h)
Definition: MdtMonUtils.py:546
TH1
Definition: rootspy.cxx:268
AlgorithmHelper.h
dqm_algorithms::MDTTDCOfflineSpectrum
Definition: MDTTDCOfflineSpectrum.h:22
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339
dqm_algorithms::MDTTDCOfflineSpectrum::printDescription
void printDescription(std::ostream &out)
Definition: MDTTDCOfflineSpectrum.cxx:165
histogram
std::string histogram
Definition: chains.cxx:52