118{
119
120
121
122
124 if (
object.
IsA()->InheritsFrom(
"TH2"))
125 {
128 {
129 throw dqm_core::BadConfig( ERS_HERE, name, "dimension of histogram < 2");
130 }
131 }
132 else
133 {
134 throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH2" );
135 }
136
137
139
140
141 std::vector<std::vector<float>> output_data;
142 std::vector<int64_t> input_dims = {1, 3};
143
144 Ort::MemoryInfo memory_info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
145 const char* input_names_cstr[] = {
input_names[0].c_str() };
146 const char* output_names_cstr[] = {
output_names[0].c_str() };
147 for (auto& input_data : hist_data) {
148
149 Ort::Value input_tensor = Ort::Value::CreateTensor<float>(memory_info, input_data.data(), input_data.size(), input_dims.data(), input_dims.size());
150
151
152 auto output_tensors =
session->Run(Ort::RunOptions{
nullptr}, input_names_cstr, &input_tensor, 1, output_names_cstr, 1);
153
154
155 if (output_tensors.empty() || output_tensors[0].HasValue())
156 {
157 throw dqm_core::BadConfig(ERS_HERE, name, "Output tensor does not have values or does not exist");
158 }
159
160
161 const size_t tensor_size = output_tensors[0].GetTensorTypeAndShapeInfo().GetElementCount();
162
163 if (tensor_size < 3)
164 {
165 throw dqm_core::BadConfig(ERS_HERE, name, "Output tensor < 3 elements");
166 }
167
168
169 float* raw_output = output_tensors[0].GetTensorMutableData<float>();
170 std::vector<float> output_point(raw_output, raw_output + 3);
171 output_data.push_back(output_point);
172 }
173
174
175
176 std::vector<float> reconstruction_errors;
177 for (
size_t i = 0;
i < hist_data.size(); ++
i) {
179 for (
size_t j = 0;
j < 3; ++
j) {
180 error += std::pow(hist_data[i][j] - output_data[i][j], 2);
181 }
182 reconstruction_errors.push_back(std::sqrt(error));
183 }
184
185
186
187 std::nth_element(reconstruction_errors.begin(), reconstruction_errors.begin() + std::roundl(reconstruction_errors.size() * (99. / 100.)), reconstruction_errors.end());
188 float threshold = reconstruction_errors[reconstruction_errors.size() * 99 / 100];
189
190
191
192 std::vector<bool> anomalies;
193 for (const auto& error : reconstruction_errors) {
194 anomalies.push_back(error > threshold);
195 }
196
197
198
199
200
201
202
203
204
205 TH2* resulthisto;
207 {
208 resulthisto=
static_cast<TH2*
>(
histogram->Clone());
209 }
210 else
211 {
212 throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH2" );
213 }
214 resulthisto->Reset();
215
216
217 for (
size_t i = 0;
i < anomalies.size(); ++
i) {
218 if (anomalies[i])
219 {
220
221 resulthisto->SetBinContent(hist_data[i][0], hist_data[i][1], hist_data[i][2]);
222 }
223 }
224
225
226 dqm_core::Result*
result =
new dqm_core::Result();
227
228
229 result->tags_[
"NBins"] = anomalies.size();
230
231
232 result->object_ = boost::shared_ptr<TObject>(resulthisto);
233
234
235 double gthreshold = 5000;
236 double rthreshold = 10000;
237
238
239 if (anomalies.size() <= gthreshold)
240 {
241 result->status_ = dqm_core::Result::Green;
242 }
243 else if (anomalies.size() < rthreshold)
244 {
245 result->status_ = dqm_core::Result::Yellow;
246 }
247 else
248 {
249 result->status_ = dqm_core::Result::Red;
250 }
251
252
253
255}
float j(const xAOD::IParticle &, const xAOD::TrackMeasurementValidation &hit, const Eigen::Matrix3d &jab_inv)
std::vector< std::vector< float > > extract_histogram_data(const TH2 *hist)
#define IsA
Declare the TObject style functions.