7 #include "CLHEP/Random/RandPoisson.h"
8 #include "CLHEP/Random/RandFlat.h"
16 bool hasExtensions(
const std::string &
name,
const std::string_view &
ext) {
33 for (
unsigned int mask_i=0; mask_i<n_masks; ++mask_i) {
34 std::stringstream rng_name;
35 rng_name <<
name() <<
"RngMaskDefect" << mask_i;
47 return StatusCode::FAILURE;
52 return StatusCode::FAILURE;
62 constexpr
unsigned int n_different_matrices_max=6;
63 m_dimPerHist.resize(n_pattern_for_histogramming);
64 m_hist.resize(n_pattern_for_histogramming);
66 m_groupDefectHists.resize(n_pattern_for_histogramming);
68 for (
unsigned int pattern_i=0; pattern_i < n_pattern_for_histogramming; ++pattern_i) {
69 m_dimPerHist[pattern_i].reserve(n_different_matrices_max);
70 m_hist[pattern_i].reserve(n_different_matrices_max);
72 std::vector<std::string> hist_name {
73 "matrix_type_id_per_module;Matrix type ID per module",
74 "defect_module;Module is defect",
75 "defects_per_module;Defect cells per module"};
79 hist_name.push_back(
hname.str());
81 m_moduleHist.resize(n_pattern_for_histogramming);
82 unsigned int max_y_axis = (((wafer_hash_max+99)/100+9)/10)*10;
84 for (
unsigned int pattern_i=0; pattern_i<m_moduleHist.size(); ++pattern_i) {
85 m_moduleHist.at(pattern_i).resize(hist_name.size(),
nullptr);
86 for (
unsigned int hist_i=0; hist_i<hist_name.size(); ++hist_i) {
88 std::string::size_type
pos = hist_name.at(hist_i).find(
";");
90 a_name << hist_name[hist_i].substr(0, (
pos != std::string::npos ?
pos : hist_name[hist_i].
size())) <<
"_" << pattern_i;
92 a_title << hist_name[hist_i].substr((
pos != std::string::npos ?
pos+1 : 0), hist_name[hist_i].size())
93 <<
" ( module pattern " << pattern_i <<
")";
96 m_moduleHist.at(pattern_i).at(hist_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
98 max_y_axis, -0.5, max_y_axis-0.5);
100 m_moduleHist[pattern_i][hist_i]->GetXaxis()->SetTitle(
"ID hash % 100");
101 m_moduleHist[pattern_i][hist_i]->GetYaxis()->SetTitle(
"ID hash / 100");
102 ATH_MSG_VERBOSE(
"Create histogram pattern " << pattern_i <<
" hist " << hist_i <<
" name " << a_name.
str().c_str()
103 <<
" -> " << m_moduleHist[pattern_i][hist_i]->GetName());
105 m_moduleHist[pattern_i][hist_i]).isFailure() ) {
106 return StatusCode::FAILURE;
112 for (
unsigned int pos_i=0; pos_i<m_defectModuleEtaPhiHist.size(); ++pos_i) {
113 m_defectModuleEtaPhiHist.at(pos_i).resize(n_eta_phi_histograms,
nullptr);
114 m_defectModuleEtaLayerHist.at(pos_i).resize(n_eta_phi_histograms,
nullptr);
116 std::array<std::string_view, kNPosHists> pos_hist_name {
118 "modules_with_defects"
120 std::array<std::string_view, kNPosHists> pos_hist_title {
122 "Modules with defects"
124 for (
unsigned pattern_i=0; pattern_i< n_eta_phi_histograms; ++pattern_i) {
125 for (
unsigned int pos_i=0; pos_i<m_defectModuleEtaPhiHist.size(); ++pos_i) {
128 a_name << pos_hist_name.at(pos_i) <<
"_zr" <<
"_" << pattern_i;
130 a_title << pos_hist_title.at(pos_i) <<
" vs global zr" <<
" ( module pattern " << pattern_i <<
")";
133 m_defectModuleEtaPhiHist.at(pos_i).at(pattern_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
139 m_defectModuleEtaPhiHist.at(pos_i).at(pattern_i)).isFailure() ) {
140 return StatusCode::FAILURE;
145 a_name << pos_hist_name.at(pos_i) <<
"_xy" <<
"_" << pattern_i;
147 a_title << pos_hist_title.at(pos_i) <<
" vs global xy" <<
" ( module pattern " << pattern_i <<
")";
150 m_defectModuleEtaLayerHist.at(pos_i).at(pattern_i) =
new TH2F(a_name.
str().c_str(), a_title.
str().c_str(),
156 m_defectModuleEtaLayerHist.at(pos_i).at(pattern_i)).isFailure() ) {
157 return StatusCode::FAILURE;
166 for (
unsigned int mask_i=0; mask_i<n_masks-1; ++mask_i) {
172 ATH_MSG_FATAL(
"m_groupDefectHistNames does not contain a name per mask starting from the second mask "
173 "(the first mask must be the full matrix)");
174 return StatusCode::FAILURE;
177 ATH_MSG_FATAL(
"m_maxNGroupDefects does not contain a upper bin value for number of expected defects "
178 "per mask starting from the second mask (the first mask must be the full matrix)");
179 return StatusCode::FAILURE;
184 return StatusCode::SUCCESS;
191 && ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities))) {
197 ATH_MSG_ERROR(
"Order problem in modulePattern : " << ret <<
"?");
199 ret = ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities);
201 ATH_MSG_ERROR(
"Size problem in defectProbability : " << ret <<
" ( n_prob" << n_probabilities <<
")?");
206 && ModuleIdentifierMatchUtil::verifyElementCount<double>(
m_defectProbability.value(), n_probabilities)
207 ? StatusCode::SUCCESS : StatusCode::FAILURE;
214 return StatusCode::SUCCESS;
219 const std::vector<unsigned int> &module_pattern_idx,
220 unsigned int n_masks,
221 unsigned int n_cells,
222 std::vector<unsigned int> &n_mask_defects)
const
224 n_mask_defects.clear();
225 n_mask_defects.resize(n_masks, 0
u);
229 std::vector<bool> has(n_masks,
false);
230 for (
unsigned int mask_i=n_masks; mask_i-->1; ) {
231 for (
unsigned int match_i: module_pattern_idx) {
240 for (
unsigned int mask_i=n_masks; mask_i-->1; ) {
241 assert(mask_i>0 && mask_i<rndmEngine.size());
242 float prob = !has.at(mask_i) ? 1. : CLHEP::RandFlat::shoot(rndmEngine[mask_i],1.);
244 for (
unsigned int match_i: module_pattern_idx) {
248 n_mask_defects[mask_i] = n_mask_defects_idx+1;
253 if (
prob<=0.
f)
break;
257 n_mask_defects[0]=
static_cast<unsigned int>(
std::max(0,
static_cast<int>(
258 CLHEP::RandPoisson::shoot(rndmEngine[0],
259 n_cells * defect_prob))));
264 unsigned int n_error,
265 unsigned int n_defects_total,
266 const std::vector<std::array<unsigned int,kNCounts> >&
counts)
const {
267 assert(
counts.size() == n_masks+1 );
269 << n_defects_total <<
".\n";
270 for (
unsigned int mask_i=0; mask_i <
counts.size(); ++mask_i) {
289 if (mask_i+1<
counts.size()) {
297 unsigned int key=(n_rows << 16) | n_cols;
298 assert( pattern_i < m_dimPerHist.size());
299 assert( pattern_i < m_hist.size());
300 unsigned int matrix_type_id;
302 std::vector<unsigned int>::const_iterator global_iter =
std::find(m_matrixTypeId.begin(), m_matrixTypeId.end(),
key );
303 if (global_iter != m_matrixTypeId.end()) {
304 matrix_type_id = global_iter - m_matrixTypeId.begin();
307 matrix_type_id =m_matrixTypeId.size();
308 m_matrixTypeId.push_back(
key);
312 std::vector<unsigned int>::const_iterator
iter =
std::find(m_dimPerHist[pattern_i].
begin(), m_dimPerHist[pattern_i].
end(),
key );
313 if (
iter == m_dimPerHist[pattern_i].
end()) {
315 name <<
"defects_" << pattern_i <<
"_" << (matrix_type_id) <<
"_" << n_rows <<
"_" << n_cols;
317 title <<
"Defects for " << n_rows <<
"(rows) #times " << n_cols <<
" (columns) ID " << (matrix_type_id) <<
", pattern " << pattern_i;
320 m_hist[pattern_i].push_back(
new TH2F(
name.str().c_str(),
title.str().c_str(),
321 n_cols, -0.5, n_cols-0.5,
322 n_rows, -0.5, n_rows-0.5
325 m_hist[pattern_i].back()->GetXaxis()->SetTitle(
"offline column");
326 m_hist[pattern_i].back()->GetYaxis()->SetTitle(
"offline row");
328 throw std::runtime_error(
"Failed to register histogram.");
330 m_dimPerHist[pattern_i].push_back(
key);
332 if (!m_groupDefectHists.empty()) {
333 assert( pattern_i < m_groupDefectHists.size());
335 m_groupDefectHists[pattern_i].emplace_back();
340 hname <<
"n_" <<
group_name <<
"_" << pattern_i <<
"_" << matrix_type_id <<
"_" << n_rows <<
"_" << n_cols;
342 htitle <<
"Number of " <<
group_name <<
" defects for " << n_rows <<
"(rows) #times " << n_cols <<
" (columns) ID "
343 << (matrix_type_id) <<
", pattern " << pattern_i;
347 assert(group_i < m_groupDefectHists[pattern_i].back().
size());
348 m_groupDefectHists[pattern_i].back()[group_i]=
new TH1F(
hname.str().c_str(), htitle.
str().c_str(),
352 m_groupDefectHists[pattern_i].back()[group_i]).isFailure() ) {
353 throw std::runtime_error(
"Failed to register histogram.");
357 return std::make_pair(
static_cast<unsigned int>(m_hist[pattern_i].
size()-1), matrix_type_id);
360 return std::make_pair(
static_cast<unsigned int>(
iter-m_dimPerHist[pattern_i].
begin()), matrix_type_id);
376 ATH_MSG_ERROR(
"The number of fraction lists per pattern does not match the number of module patterns: "
378 return StatusCode::FAILURE;
381 ATH_MSG_ERROR(
"The number of probability lists per pattern does not match the number of module patterns: "
383 return StatusCode::FAILURE;
390 ATH_MSG_ERROR(
"There should be one probability for the module to be defect, one probability for a pixel/strip etc. "
391 "to be defect and one probability for each group to be defect i.e. "
394 return StatusCode::FAILURE;
403 <<
" for pattern " << pattern_i);
404 return StatusCode::FAILURE;
418 ATH_MSG_ERROR(
"Empty fraction list or fractions do not add up to 1: "
421 <<
" for pattern " << pattern_i <<
", mask " << mask_i
423 return StatusCode::FAILURE;
425 ATH_MSG_DEBUG(
"Fractions for pattern " << pattern_i <<
" mask " << mask_i
433 ATH_MSG_DEBUG(
"Probabilities for pattern " << pattern_i <<
" mask " << mask_i
446 ATH_MSG_ERROR(
"Expected exactly one set of corner defect parameters per module pattern but the numbers disagree "
448 return StatusCode::FAILURE;
451 ATH_MSG_ERROR(
"Expected exactly one set of n corner fractions per module pattern but the numbers disagree "
453 return StatusCode::FAILURE;
461 return StatusCode::FAILURE;
464 ATH_MSG_ERROR(
"Too many Fractions for corner defects for pattern " << pattern_i <<
467 return StatusCode::FAILURE;
472 if (std::abs(
scale-1.)>1
e-3) {
474 <<
" corner(s) for pattern " << pattern_i <<
" do not add up to ~1 but " <<
scale);
475 return StatusCode::FAILURE;
481 total += fraction *
scale;
486 return StatusCode::SUCCESS;
491 unsigned int hist_pattern_i,
492 unsigned int id_hash,
494 assert( hist_pattern_i < m_moduleHist.size() );
495 unsigned int ids_per_col =
static_cast<unsigned int>(m_moduleHist[hist_pattern_i][
kDefectModule]->GetNbinsX());
496 unsigned int bin_i=m_moduleHist[hist_pattern_i][
kDefectModule]->GetBin( id_hash%ids_per_col+1, id_hash/ids_per_col+1);
497 m_moduleHist[hist_pattern_i][
kDefectModule]->SetBinContent(bin_i,1);
501 m_defectModuleEtaPhiHist[
kDefectModulePos][eta_phi_pattern_i]->Fill(center.z(), center.perp());
502 m_defectModuleEtaLayerHist[
kDefectModulePos][eta_phi_pattern_i]->Fill(center.x(), center.y());
506 unsigned int pattern_hist_i,
507 unsigned int matrix_histogram_index,
508 unsigned int matrix_index,
509 unsigned int module_i,
510 unsigned int n_masks,
511 const std::vector<unsigned int> &n_mask_defects,
516 assert( pattern_hist_i < m_moduleHist.size());
519 unsigned int ids_per_col =
static_cast<unsigned int>(m_moduleHist[pattern_hist_i][
kDefectModule]->GetNbinsX());
520 unsigned int bin_i=m_moduleHist[pattern_hist_i][
kDefectModule]->GetBin( module_i%ids_per_col+1, module_i/ids_per_col+1);
521 unsigned int mask_i=n_masks;
522 for (; --mask_i>0; ) {
523 assert( mask_i < n_mask_defects.size() && mask_i>0);
524 if (!m_groupDefectHists.empty()) {
525 m_groupDefectHists[pattern_hist_i][matrix_histogram_index][mask_i-1]->Fill(n_mask_defects[mask_i]);
528 m_moduleHist[pattern_hist_i][
kDefectCell+mask_i]->SetBinContent(bin_i, n_mask_defects[mask_i]);
531 m_moduleHist[pattern_hist_i][
kDefectCell+mask_i]->SetBinContent(bin_i, n_mask_defects[mask_i]);
532 m_moduleHist[pattern_hist_i][
kMatrixTypeId]->SetBinContent(bin_i, matrix_index+1 );
533 unsigned int n_defects_total =
std::accumulate(n_mask_defects.begin(),n_mask_defects.end(), 0
u);
534 if (n_defects_total>0) {
539 m_defectModuleEtaLayerHist[
kModulesWithDefectsPos][eta_phi_pattern_i]->Fill(center.x(), center.y(),n_defects_total);