ATLAS Offline Software
AFP_ToFSiTCorrCheck.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 #include <dqm_core/AlgorithmManager.h>
9 #include "dqm_core/AlgorithmConfig.h"
10 #include <dqm_core/exceptions.h>
11 
12 #include <TDirectory.h>
13 #include <TH1.h>
14 #include <TH2.h>
15 #include <TFile.h>
16 #include <numeric>
17 #include <string_view>
18 #include <system_error>
19 
20 namespace {
22 }
23 
25  dqm_core::AlgorithmManager::instance().registerAlgorithm( "AFP_ToFSiTCorrCheck", this );
26 }
27 
28 
31  return new AFP_ToFSiTCorrCheck();
32 }
33 
36  const TObject& object,
37  const dqm_core::AlgorithmConfig& config ) {
38  if ( !object.IsA()->InheritsFrom( "TH2" ) ) {
39  throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH2" );
40  }
41 
42  auto histogram = static_cast<const TH2*>( &object );
43 
44  if ( histogram->GetDimension() > 2 ) {
45  throw dqm_core::BadConfig( ERS_HERE, name, "histogram has more than 2 dimensions" );
46  }
47 
48  auto gthreshold_tr = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadTrains", config.getGreenThresholds() ) );
49  auto rthreshold_tr = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadTrains", config.getRedThresholds() ) );
50  auto gthreshold_st = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadStairs", config.getGreenThresholds() ) );
51  auto rthreshold_st = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadStairs", config.getRedThresholds() ) );
52  auto pronounciation_level_down = static_cast<double>( dqm_algorithms::tools::GetFirstFromMap( "pronounciation_level_down", config.getParameters() ) );
53  auto pronounciation_level_up = static_cast<int>( dqm_algorithms::tools::GetFirstFromMap( "pronounciation_level_up", config.getParameters() ) );
54  auto run_num = dqm_algorithms::tools::GetFirstFromMap( "run_number", config.getParameters(), 0 );
55  const std::string RANGES_22 = dqm_algorithms::tools::GetFirstFromMap( "RANGES_22", config.getGenericParameters() );
56  const std::string RANGES_24 = dqm_algorithms::tools::GetFirstFromMap( "RANGES_24", config.getGenericParameters() );
57 
58  //from string of ranges to int array
59  int y_bins[8] = {};
60  std::string_view s = RANGES_22;
61  if (run_num > 470000)
62  s = RANGES_24;
63  s.remove_prefix(1);
64  s.remove_suffix(1);
65  std::string delimiter = ",";
66  uint32_t pos = 0;
67  bool valid_arg = 1;
68  for (uint32_t i = 0; i < 8; ++i)
69  {
70  pos = s.find(delimiter);
71  auto result = std::from_chars(s.data(), s.data() + s.size(), y_bins[i]);
72  if (result.ec == std::errc::invalid_argument || result.ec == std::errc::result_out_of_range) {valid_arg = 0;}
73  s.remove_prefix(pos + delimiter.length());
74  }
75 
76  uint32_t false_bins = 0;
77  uint32_t false_bins_devided_tr = 0;
78  uint32_t status_bins = 0;
79  uint32_t status_bins_devided = 0;
80  if (valid_arg)
81  {
82  //from 2D hist to projections of each train and get bin content
83  const std::string trains[4] = {"0", "1", "2", "3"};
84  auto projections_tr = std::vector<TH1D *>(4);
85  int max_bins[4] = {};
86  float max_bins_content[4] = {};
87  float all_bins_content[4][4] = {};
88  for (uint32_t i = 0; i < 4; ++i)
89  {
90  projections_tr[i] = new TH1D(("projections_tr"+trains[i]).c_str(),("projections_tr"+trains[i]).c_str(), 16, 0, 4);
91  projections_tr[i] = histogram->ProjectionX(("projections_tr"+trains[i]).c_str(), y_bins[i*2], y_bins[i*2+1], "cutg");
92  //check if any bars in trains are off
93  int num_bars_off[4] = {};
94  std::vector<int> bar_off = {};
95  for (uint32_t j = 5; j <= 20; ++j)
96  {
97  if (projections_tr[i]->GetBinContent(j) < 1)
98  {
99  int train = ( (j - 5) / 4 );
100  num_bars_off[train]++;
101  bar_off.push_back(j);
102  }
103  }
104  int sum_bars_off = 0;
105  sum_bars_off = std::accumulate(num_bars_off, num_bars_off+4, sum_bars_off);
106  //if yes, artificially add mean value of train to this bar to not break checks
107  if (sum_bars_off > 0)
108  {
109  for (int k = 0; k < sum_bars_off; ++k)
110  {
111  float mean = 0;
112  int train = ( bar_off[k] - 5 ) / 4;
113  int bin = train * 4 + 5;
114  for (int j = bin; j < bin + 4; ++j)
115  mean = mean + float(projections_tr[i]->GetBinContent(j));
116  mean = mean / (4 - num_bars_off[train]);
117  projections_tr[i]->SetBinContent(bar_off[k],mean);
118  }
119  }
120  projections_tr[i]->Rebin(4);
121  max_bins[i] = (projections_tr[i]->GetMaximumBin())-2;
122  max_bins_content[i] = (projections_tr[i]->GetBinContent(max_bins[i]+2));
123  for (size_t j = 0; j < 4; j++)
124  all_bins_content[i][j] = (projections_tr[i]->GetBinContent(j+2));
125  delete projections_tr[i];
126  }
127 
128  //check if any train is not max
129  for (int i = 0; i < 4; ++i)
130  {
131  if (max_bins[i] != i)
132  false_bins++;
133  }
134 
135  if (false_bins == 0)
136  status_bins = 1;
137  else if (false_bins == gthreshold_tr)
138  status_bins = 2;
139  else if (false_bins >= rthreshold_tr)
140  status_bins = 3;
141 
142  //check on how good pronounce stairs are
143  float check_devided_bins[4][4] = {};
144  int false_bins_devided[4] = {};
145  for (uint32_t i = 0; i < 4; ++i)
146  {
147  for (uint32_t j = 0; j < 4; ++j)
148  if (i != j)
149  {
150  check_devided_bins[i][j] = max_bins_content[i]/all_bins_content[i][j];
151  if ((check_devided_bins[i][j] < pronounciation_level_down) || (check_devided_bins[i][j] > pronounciation_level_up))
152  false_bins_devided[i]++;
153  }
154  if (false_bins_devided[i] > 1)
155  false_bins_devided_tr++;
156  }
157 
158  if (false_bins_devided_tr == 0)
159  status_bins_devided = 1;
160  else if ((false_bins_devided_tr >= gthreshold_st) && (false_bins_devided_tr < rthreshold_st))
161  status_bins_devided = 2;
162  else if (false_bins_devided_tr >= rthreshold_st)
163  status_bins_devided = 3;
164  }
165 
166  //get general status
167  uint32_t status_general = 0;
168  if (status_bins_devided > status_bins) {status_general = status_bins_devided;}
169  else if (status_bins_devided <= status_bins) {status_general = status_bins;}
170 
171  auto result = new dqm_core::Result();
172 
173  // publish problematic bins
174  result->tags_[ "N Trains on wrong positions/not existing" ] = false_bins;
175  result->tags_[ "N Stairs not pronounced" ] = false_bins_devided_tr;
176 
177  if ( status_general == 0 )
179  else if ( status_general == 3 )
180  result->status_ = dqm_core::Result::Red;
181  else if ( status_general == 2 )
182  result->status_ = dqm_core::Result::Yellow;
183  else if ( status_general == 1 )
184  result->status_ = dqm_core::Result::Green;
185 
186  return result;
187 }
188 
190  out << "AFP_ToFSiTCorrCheck: Print out how many stairs are not on their places and how many are not pronounced\n"
191  << "Required Parameter: pronounciation_level_down: how pronounced the stair is (down limit)\n"
192  << "Required Parameter: pronounciation_level_up: how pronounced the stair is (upper limit)\n"
193  << "Required Parameter: RANGES: ranges of SiT, corresponding to ToF trains, in bins of hist"<< std::endl;
194 }
dqm_algorithms::AFP_ToFSiTCorrCheck::AFP_ToFSiTCorrCheck
AFP_ToFSiTCorrCheck()
Definition: AFP_ToFSiTCorrCheck.cxx:24
Undefined
@ Undefined
Definition: MaterialTypes.h:8
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
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
dqm_algorithms::AFP_ToFSiTCorrCheck::execute
dqm_core::Result * execute(const std::string &name, const TObject &object, const dqm_core::AlgorithmConfig &config) override
Definition: AFP_ToFSiTCorrCheck.cxx:35
get_generator_info.result
result
Definition: get_generator_info.py:21
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
bin
Definition: BinsDiffFromStripMedian.h:43
dqm_algorithms::AFP_ToFSiTCorrCheck
Definition: AFP_ToFSiTCorrCheck.h:21
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
instance
std::map< std::string, double > instance
Definition: Run_To_Get_Tags.h:8
python.AthDsoLogger.delimiter
delimiter
Definition: AthDsoLogger.py:71
lumiFormat.i
int i
Definition: lumiFormat.py:85
dqm_algorithms::AFP_ToFSiTCorrCheck::clone
AFP_ToFSiTCorrCheck * clone() override
Definition: AFP_ToFSiTCorrCheck.cxx:30
Result
ICscStripFitter::Result Result
Definition: CalibCscStripFitter.cxx:13
python.handimod.Green
int Green
Definition: handimod.py:524
python.handimod.Red
Red
Definition: handimod.py:551
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
AlgorithmHelper.h
dqm_algorithms::tools::GetFromMap
const T & GetFromMap(const std::string &pname, const std::map< std::string, T > &params)
Definition: AlgorithmHelper.h:114
pickleTool.object
object
Definition: pickleTool.py:30
AFP_ToFSiTCorrCheck.h
dqm_algorithms::tools::GetFirstFromMap
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
Definition: AlgorithmHelper.cxx:339
dqm_algorithms::AFP_ToFSiTCorrCheck::printDescriptionTo
void printDescriptionTo(std::ostream &out) override
Definition: AFP_ToFSiTCorrCheck.cxx:189
histogram
std::string histogram
Definition: chains.cxx:52
fitman.k
k
Definition: fitman.py:528