7 #include "CLHEP/Random/RandPoisson.h"
8 #include "CLHEP/Random/RandFlat.h"
33 constexpr
unsigned int n_different_matrices_max=6;
34 m_dimPerHist.resize(n_pattern_for_histogramming);
35 m_hist.resize(n_pattern_for_histogramming);
37 m_groupDefectHists.resize(n_pattern_for_histogramming);
39 for (
unsigned int pattern_i=0; pattern_i < n_pattern_for_histogramming; ++pattern_i) {
40 m_dimPerHist[pattern_i].reserve(n_different_matrices_max);
41 m_hist[pattern_i].reserve(n_different_matrices_max);
43 std::vector<std::string> hist_name {
44 "matrix_type_id_per_module;Matrix type ID per module",
45 "defect_module;Module is defect",
46 "defects_per_module;Defect cells per module"};
50 hist_name.push_back(
hname.str());
52 m_moduleHist.resize(n_pattern_for_histogramming);
53 unsigned int max_y_axis = (((wafer_hash_max+99)/100+9)/10)*10;
55 for (
unsigned int pattern_i=0; pattern_i<m_moduleHist.size(); ++pattern_i) {
56 m_moduleHist.at(pattern_i).resize(hist_name.size(),
nullptr);
57 for (
unsigned int hist_i=0; hist_i<hist_name.size(); ++hist_i) {
59 std::string::size_type
pos = hist_name.at(hist_i).find(
";");
61 a_name << hist_name[hist_i].substr(0, (
pos != std::string::npos ?
pos : hist_name[hist_i].
size())) <<
"_" << pattern_i;
63 a_title << hist_name[hist_i].substr((
pos != std::string::npos ?
pos+1 : 0), hist_name[hist_i].size())
64 <<
" ( module pattern " << pattern_i <<
")";
67 m_moduleHist.at(pattern_i).at(hist_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
69 max_y_axis, -0.5, max_y_axis-0.5);
71 m_moduleHist[pattern_i][hist_i]->GetXaxis()->SetTitle(
"ID hash % 100");
72 m_moduleHist[pattern_i][hist_i]->GetYaxis()->SetTitle(
"ID hash / 100");
73 ATH_MSG_VERBOSE(
"Create histogram pattern " << pattern_i <<
" hist " << hist_i <<
" name " << a_name.
str().c_str()
74 <<
" -> " << m_moduleHist[pattern_i][hist_i]->GetName());
76 m_moduleHist[pattern_i][hist_i]).isFailure() ) {
77 return StatusCode::FAILURE;
83 for (
unsigned int pos_i=0; pos_i<m_defectModuleEtaPhiHist.size(); ++pos_i) {
84 m_defectModuleEtaPhiHist.at(pos_i).resize(n_eta_phi_histograms,
nullptr);
85 m_defectModuleEtaLayerHist.at(pos_i).resize(n_eta_phi_histograms,
nullptr);
87 std::array<std::string_view, kNPosHists> pos_hist_name {
89 "modules_with_defects"
91 std::array<std::string_view, kNPosHists> pos_hist_title {
93 "Modules with defects"
95 for (
unsigned pattern_i=0; pattern_i< n_eta_phi_histograms; ++pattern_i) {
96 for (
unsigned int pos_i=0; pos_i<m_defectModuleEtaPhiHist.size(); ++pos_i) {
99 a_name << pos_hist_name.at(pos_i) <<
"_zr" <<
"_" << pattern_i;
101 a_title << pos_hist_title.at(pos_i) <<
" vs global zr" <<
" ( module pattern " << pattern_i <<
")";
104 m_defectModuleEtaPhiHist.at(pos_i).at(pattern_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
110 m_defectModuleEtaPhiHist.at(pos_i).at(pattern_i)).isFailure() ) {
111 return StatusCode::FAILURE;
116 a_name << pos_hist_name.at(pos_i) <<
"_xy" <<
"_" << pattern_i;
118 a_title << pos_hist_title.at(pos_i) <<
" vs global xy" <<
" ( module pattern " << pattern_i <<
")";
121 m_defectModuleEtaLayerHist.at(pos_i).at(pattern_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
127 m_defectModuleEtaLayerHist.at(pos_i).at(pattern_i)).isFailure() ) {
128 return StatusCode::FAILURE;
137 for (
unsigned int mask_i=0; mask_i<n_masks-1; ++mask_i) {
143 ATH_MSG_FATAL(
"m_groupDefectHistNames does not contain a name per mask starting from the second mask "
144 "(the first mask must be the full matrix)");
145 return StatusCode::FAILURE;
148 ATH_MSG_FATAL(
"m_maxNGroupDefects does not contain a upper bin value for number of expected defects "
149 "per mask starting from the second mask (the first mask must be the full matrix)");
150 return StatusCode::FAILURE;
155 return StatusCode::SUCCESS;
162 && ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities))) {
168 ATH_MSG_ERROR(
"Order problem in modulePattern : " << ret <<
"?");
170 ret = ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities);
172 ATH_MSG_ERROR(
"Size problem in defectProbability : " << ret <<
" ( n_prob" << n_probabilities <<
")?");
177 && ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities)
178 ? StatusCode::SUCCESS : StatusCode::FAILURE;
185 return StatusCode::SUCCESS;
190 const std::vector<unsigned int> &module_pattern_idx,
191 unsigned int n_masks,
192 unsigned int n_cells,
193 std::vector<unsigned int> &n_mask_defects)
const
195 n_mask_defects.clear();
196 n_mask_defects.resize(n_masks, 0
u);
200 std::vector<bool> has(n_masks,
false);
201 for (
unsigned int mask_i=n_masks; mask_i-->1; ) {
202 for (
unsigned int match_i: module_pattern_idx) {
211 for (
unsigned int mask_i=n_masks; mask_i-->1; ) {
213 float prob = !has.at(mask_i) ? 1. : CLHEP::RandFlat::shoot(rndmEngine,1.);
215 for (
unsigned int match_i: module_pattern_idx) {
219 n_mask_defects[mask_i] = n_mask_defects_idx+1;
224 if (
prob<=0.
f)
break;
228 n_mask_defects[0]=
static_cast<unsigned int>(
std::max(0,
static_cast<int>(
229 CLHEP::RandPoisson::shoot(rndmEngine,
230 n_cells * defect_prob))));
235 unsigned int n_error,
236 unsigned int n_defects_total,
237 const std::vector<std::array<unsigned int,kNCounts> >&counts)
const {
238 assert(counts.size() == n_masks+1 );
239 msg(MSG::INFO) <<
"Total cells: " << counts[0][
kNElements] <<
", module design of wrong type: " << n_error <<
", defects "
240 << n_defects_total <<
".\n";
241 for (
unsigned int mask_i=0; mask_i < counts.size(); ++mask_i) {
260 if (mask_i+1<counts.size()) {
268 unsigned int key=(n_rows << 16) | n_cols;
269 assert( pattern_i < m_dimPerHist.size());
270 assert( pattern_i < m_hist.size());
271 unsigned int matrix_type_id;
273 std::vector<unsigned int>::const_iterator global_iter =
std::find(m_matrixTypeId.begin(), m_matrixTypeId.end(),
key );
274 if (global_iter != m_matrixTypeId.end()) {
275 matrix_type_id = global_iter - m_matrixTypeId.begin();
278 matrix_type_id =m_matrixTypeId.size();
279 m_matrixTypeId.push_back(
key);
283 std::vector<unsigned int>::const_iterator iter =
std::find(m_dimPerHist[pattern_i].
begin(), m_dimPerHist[pattern_i].
end(),
key );
284 if (iter == m_dimPerHist[pattern_i].
end()) {
286 name <<
"defects_" << pattern_i <<
"_" << (matrix_type_id) <<
"_" << n_rows <<
"_" << n_cols;
288 title <<
"Defects for " << n_rows <<
"(rows) #times " << n_cols <<
" (columns) ID " << (matrix_type_id) <<
", pattern " << pattern_i;
291 m_hist[pattern_i].push_back(
new TH2F(
name.str().c_str(),
title.str().c_str(),
292 n_cols, -0.5, n_cols-0.5,
293 n_rows, -0.5, n_rows-0.5
296 m_hist[pattern_i].back()->GetXaxis()->SetTitle(
"offline column");
297 m_hist[pattern_i].back()->GetYaxis()->SetTitle(
"offline row");
299 throw std::runtime_error(
"Failed to register histogram.");
301 m_dimPerHist[pattern_i].push_back(
key);
303 if (!m_groupDefectHists.empty()) {
304 assert( pattern_i < m_groupDefectHists.size());
306 m_groupDefectHists[pattern_i].emplace_back();
311 hname <<
"n_" <<
group_name <<
"_" << pattern_i <<
"_" << matrix_type_id <<
"_" << n_rows <<
"_" << n_cols;
313 htitle <<
"Number of " <<
group_name <<
" defects for " << n_rows <<
"(rows) #times " << n_cols <<
" (columns) ID "
314 << (matrix_type_id) <<
", pattern " << pattern_i;
318 assert(group_i < m_groupDefectHists[pattern_i].back().
size());
319 m_groupDefectHists[pattern_i].back()[group_i]=
new TH1F(
hname.str().c_str(), htitle.
str().c_str(),
323 m_groupDefectHists[pattern_i].back()[group_i]).isFailure() ) {
324 throw std::runtime_error(
"Failed to register histogram.");
328 return std::make_pair(
static_cast<unsigned int>(m_hist[pattern_i].
size()-1), matrix_type_id);
331 return std::make_pair(
static_cast<unsigned int>(iter-m_dimPerHist[pattern_i].
begin()), matrix_type_id);
347 ATH_MSG_ERROR(
"The number of fraction lists per pattern does not match the number of module patterns: "
349 return StatusCode::FAILURE;
352 ATH_MSG_ERROR(
"The number of probability lists per pattern does not match the number of module patterns: "
354 return StatusCode::FAILURE;
361 ATH_MSG_ERROR(
"There should be one probability for the module to be defect, one probability for a pixel/strip etc. "
362 "to be defect and one probability for each group to be defect i.e. "
365 return StatusCode::FAILURE;
374 <<
" for pattern " << pattern_i);
375 return StatusCode::FAILURE;
389 ATH_MSG_ERROR(
"Empty fraction list or fractions do not add up to 1: "
392 <<
" for pattern " << pattern_i <<
", mask " << mask_i
394 return StatusCode::FAILURE;
396 ATH_MSG_DEBUG(
"Fractions for pattern " << pattern_i <<
" mask " << mask_i
404 ATH_MSG_DEBUG(
"Probabilities for pattern " << pattern_i <<
" mask " << mask_i
417 ATH_MSG_ERROR(
"Expected exactly one set of corner defect parameters per module pattern but the numbers disagree "
419 return StatusCode::FAILURE;
422 ATH_MSG_ERROR(
"Expected exactly one set of n corner fractions per module pattern but the numbers disagree "
424 return StatusCode::FAILURE;
432 return StatusCode::FAILURE;
435 ATH_MSG_ERROR(
"Too many Fractions for corner defects for pattern " << pattern_i <<
438 return StatusCode::FAILURE;
443 if (std::abs(
scale-1.)>1
e-3) {
445 <<
" corner(s) for pattern " << pattern_i <<
" do not add up to ~1 but " <<
scale);
446 return StatusCode::FAILURE;
452 total += fraction *
scale;
457 return StatusCode::SUCCESS;
462 unsigned int hist_pattern_i,
463 unsigned int id_hash,
465 assert( hist_pattern_i < m_moduleHist.size() );
466 unsigned int ids_per_col =
static_cast<unsigned int>(m_moduleHist[hist_pattern_i][
kDefectModule]->GetNbinsX());
467 unsigned int bin_i=m_moduleHist[hist_pattern_i][
kDefectModule]->GetBin( id_hash%ids_per_col+1, id_hash/ids_per_col+1);
468 m_moduleHist[hist_pattern_i][
kDefectModule]->SetBinContent(bin_i,1);
472 m_defectModuleEtaPhiHist[
kDefectModulePos][eta_phi_pattern_i]->Fill(center.z(), center.perp());
473 m_defectModuleEtaLayerHist[
kDefectModulePos][eta_phi_pattern_i]->Fill(center.x(), center.y());
477 unsigned int pattern_hist_i,
478 unsigned int matrix_histogram_index,
479 unsigned int matrix_index,
480 unsigned int module_i,
481 unsigned int n_masks,
482 const std::vector<unsigned int> &n_mask_defects,
487 assert( pattern_hist_i < m_moduleHist.size());
490 unsigned int ids_per_col =
static_cast<unsigned int>(m_moduleHist[pattern_hist_i][
kDefectModule]->GetNbinsX());
491 unsigned int bin_i=m_moduleHist[pattern_hist_i][
kDefectModule]->GetBin( module_i%ids_per_col+1, module_i/ids_per_col+1);
492 unsigned int mask_i=n_masks;
493 for (; --mask_i>0; ) {
494 assert( mask_i < n_mask_defects.size() && mask_i>0);
495 if (!m_groupDefectHists.empty()) {
496 m_groupDefectHists[pattern_hist_i][matrix_histogram_index][mask_i-1]->Fill(n_mask_defects[mask_i]);
499 m_moduleHist[pattern_hist_i][
kDefectCell+mask_i]->SetBinContent(bin_i, n_mask_defects[mask_i]);
502 m_moduleHist[pattern_hist_i][
kDefectCell+mask_i]->SetBinContent(bin_i, n_mask_defects[mask_i]);
503 m_moduleHist[pattern_hist_i][
kMatrixTypeId]->SetBinContent(bin_i, matrix_index+1 );
504 unsigned int n_defects_total =
std::accumulate(n_mask_defects.begin(),n_mask_defects.end(), 0
u);
505 if (n_defects_total>0) {
510 m_defectModuleEtaLayerHist[
kModulesWithDefectsPos][eta_phi_pattern_i]->Fill(center.x(), center.y(),n_defects_total);