ATLAS Offline Software
StripDefectsEmulatorPostInclude.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 # can be added as a post include to a Reco_tf
4 # --postInclude "StripDefectsEmulatorPostInclude.emulateITkStripDefectsDefault"
5 # or (with noise 1e-3)
6 # --postExec "from InDetDefectsEmulation.StripDefectsEmulatorPostInclude import emulateITkStripDefectsDefault;
7 # emulateITkStripDefectsDefault(flags,cfg,HistogramFileName='itk_strip_defects.root',StripDefectProb=1e-2,ModuleDefectProb=1e-2,NoiseProb=1e-3,PropagateDefectsToStatus=True);"
8 #
9 # NOTES: - Will only work with the athena MT scheduler e.g. --threads=n with n>=1 otherwise the algorithms will not be scheduled in the
10 # correct order by this post include
11 # - accidentally using pixel defect input files for strips may result in an obscure range exception.
12 #
13 # Extra arguments:
14 # disable histogramming: HistogramFileName=None
15 # decorrelate random numbers: RngPerDefectType=True will use one random generator per defect type, however some correlation is still
16 # present because for example strip defects are not created for dead modules.
17 # persistify defects conditions data:
18 # DefectsOutputFile='defects.root' Depending on the extension .root or .json, will write all defects into a root RNTuple or json file.
19 # These files may get big. ".json" is not recommended unless there are only module defects.
20 # read defects from input files:
21 # DefectsInputFiles=['moduleDefects.root','cellDefects.root'] Read one or multiple defect files and merge defects. The file can be
22 # Either a root RNTuple or a .json file, which can be created by the
23 # conditions algorithm by specifying the option e.g.
24 # DefectsOutputFile='defects.root'
25 # Reading defects from input file(s) will not disable the generation of
26 # random defects. The latter will be added on top unless the corresponding
27 # probabilities are set to zero.
28 # For efficiency files should be ordered by the significance of the
29 # defects i.e. files with more module defects should precede files
30 # with mostly single strip defects
31 
33  cfg,
34  StripDefectProb: float=1e-2,
35  ModuleDefectProb: float=1e-2,
36  NoiseProb: float=0.,
37  MaxRandomPositionAttempts: int=10,
38  RngPerDefectType=False,
39  DefectsInputFiles=[],
40  DefectsOutputFile=None,
41  FillHistogramsPerPattern: bool=True,
42  FillEtaPhiHistogramsPerPattern: bool=True,
43  HistogramGroupName: str="ITkStripDefects",
44  HistogramFileName: str=None,
45  PropagateDefectsToStatus=True) :
46  """
47  Schedule algorithms to create ITk strip defect conditions data and to emulate
48  defects by dropping ITk strip RDOs overlapping with such defects. ModuleDefectProb controls the number of
49  defect strip modules and DefectProb the number of defect strips. If a strip group is overlapping with
50  a single strip defect, the strip group will be split.
51  """
52 
53  from InDetDefectsEmulation.StripDefectsEmulatorConfig import (
54  ITkStripDefectsEmulatorCondAlgCfg,
55  ITkStripDefectsEmulatorAlgCfg,
56  ITkStripDefectsEmulatorToDetectorElementStatusCondAlgCfg,
57  DefectsHistSvcCfg,
58  moduleDefect,
59  combineModuleDefects
60  )
61  from AthenaCommon.Constants import INFO
62 
63  if HistogramFileName is None or len(HistogramFileName) ==0 :
64  HistogramGroupName = None
65  if HistogramGroupName is not None :
66  cfg.merge( DefectsHistSvcCfg(flags, HistogramGroup=HistogramGroupName, FileName=HistogramFileName))
67 
68  # dummy "fractions" for strips, module pattern are just examples, and currently all
69  # modules get the same defect probabilities irrespectively of e.g. strip length.
70  module_pattern, module_defect_prob, fractions, ignore_NoiseProbability,ignore_NoiseShape, cornerDefectParam, cornerDefectFractions = combineModuleDefects([
71  moduleDefect(bec=[-2,-2],layer=[0,99], phi_range=[-99,99],eta_range=[-99,99], # select all endcap A modules
72  side_range=[0,0,1,1], # randomly mark sides as defect. With [0,1] Both sides are marked as defect
73  all_rows=False, # if True all rows of the same un-split module are marked as defect
74  probability=[ModuleDefectProb, # probability of a module to be defect
75  StripDefectProb # probability of a strip to be defect
76  ]),
77  moduleDefect(bec=[-1,1],layer=[0,1], phi_range=[-99,99],eta_range=[-99,99], # select all barrel, layer 0,1 modules
78  side_range=[0,0,1,1], # randomly mark sides as defect. With [0,1] Both sides are marked as defect
79  all_rows=False, # if True all rows of the same un-split module are marked as defect
80  probability=[ModuleDefectProb, # probability of a module to be defect
81  StripDefectProb # probability of a strip to be defect
82  ]),
83  moduleDefect(bec=[-1,1],layer=[2,99], phi_range=[-99,99],eta_range=[-99,99], # select all other barrel layers
84  side_range=[0,0,1,1], # randomly mark sides as defect. With [0,1] Both sides are marked as defect
85  all_rows=False, # if True all rows of the same un-split module are marked as defect
86  probability=[ModuleDefectProb, # probability of a module to be defect
87  StripDefectProb # probability of a strip to be defect
88  ]),
89  moduleDefect(bec=[2,2],layer=[0,99], phi_range=[-99,99],eta_range=[-99,99], # select all endcap C modules
90  side_range=[0,0,1,1], # randomly mark sides as defect. With [0,1] Both sides are marked as defect
91  all_rows=False, # if True all rows of the same un-split module are marked as defect
92  probability=[ModuleDefectProb, # probability of a module to be defect
93  StripDefectProb # probability of a strip to be defect
94  ])])
95 
96  if NoiseProb > 0. :
97  # strip noise
98  import math
99  def norm(dist) :
100  scale = sum(dist)
101  return [elm/scale if scale > 0. else 0. for elm in dist]
102  def gaussDist(mean, width, n) :
103  scale = -1./(2*width)
104  return norm([ math.exp( scale *math.pow(i - mean,2.) ) for i in range(0,n) ])
105  # Gaussian distribution between 1 and 2
106  time_bin_dist = gaussDist(1.51,0.03,3)
107 
108  # different noise per module of different strip length
109  defects=[]
110  for length in [15093,18093,18981,23981,24092,24180,26223,27093,28980,30807,30808,31981,32223,40161,48360,54580,54581,60162] :
111  # assume noise scales with strip length but there is a constant noise
112  defects += [ moduleDefect(column_or_strip_length=[length-1,length+1],
113  probability=[],
114  noiseProbability=NoiseProb * 0.5*( 1 + length / 37627.5 ),
115  noiseShape=time_bin_dist)]
116  module_pattern_noise, ignore_module_defect_prob, ignore_fractions, NoiseProbability,NoiseShape,cornerDefectParam,cornerDefectFractions = combineModuleDefects(defects)
117  else :
118  module_pattern_noise=[]
119  NoiseProbability=[]
120  NoiseShape=[]
121 
122  # schedule custom defects generating conditions alg
123  cfg.merge( ITkStripDefectsEmulatorCondAlgCfg(flags,
124  # to enable histogramming:
125  HistogramGroupName=f"/{HistogramGroupName}/StripEmulatedDefects/" if HistogramGroupName is not None else "",
126  MaxRandomPositionAttempts=MaxRandomPositionAttempts,
127  ModulePatterns=module_pattern,
128  DefectProbabilities=module_defect_prob,
129  CornerDefectParamsPerPattern=[],
130  NCornerDefectFractionsPerPattern=[],
131  RngPerDefectType=RngPerDefectType,
132  DefectsInputFiles=DefectsInputFiles,
133  DefectsOutputFile=DefectsOutputFile,
134  FillHistogramsPerPattern=FillHistogramsPerPattern,
135  FillEtaPhiHistogramsPerPattern=FillEtaPhiHistogramsPerPattern,
136  WriteKey="ITkStripEmulatedDefects", # the default should match the key below
137  ))
138 
139  # schedule the algorithm which drops elements from the ITk Pixel RDO collection
140  cfg.merge( ITkStripDefectsEmulatorAlgCfg(flags,
141  # prevent default cond alg from being scheduled :
142  EmulatedDefectsKey="ITkStripEmulatedDefects",
143  ModulePatterns=module_pattern_noise,
144  NoiseProbability = NoiseProbability,
145  NoiseShape = NoiseShape,
146  # to enable histogramming:
147  HistogramGroupName=f"/{HistogramGroupName}/StripRejectedRDOs/" if HistogramGroupName is not None else "",
148  OutputLevel=INFO))
149 
150  if PropagateDefectsToStatus :
151  # propagate defects to detector element status
153  name="ITkStripDefectsEmulatorToDetectorElementStatusCondAlg",
154  EmulatedDefectsKey="ITkStripEmulatedDefects",
155  WriteKey="ITkStripDetectorElementStatusFromEmulatedDefects"))
156  strip_det_el_status_cond_alg=cfg.getCondAlgo("ITkStripDetectorElementStatusCondAlgNoByteStreamErrors")
157  strip_det_el_status_cond_alg.ConditionsSummaryTool.SCTDetElStatusCondDataBaseKey="ITkStripDetectorElementStatusFromEmulatedDefects"
158 
160  cfg,
161  StripDefectProb: float=1e-2,
162  ModuleDefectProb: float=1e-2,
163  NoiseProb: float=0.,
164  RngPerDefectType=False,
165  DefectsInputFiles=[],
166  DefectsOutputFile=None,
167  HistogramFileName: str=None,
168  PropagateDefectsToStatus=True) :
169  emulateITkStripDefects(flags,cfg,
170  StripDefectProb=StripDefectProb,
171  ModuleDefectProb=ModuleDefectProb,
172  NoiseProb=NoiseProb,
173  RngPerDefectType=RngPerDefectType,
174  DefectsInputFiles=DefectsInputFiles,
175  DefectsOutputFile=DefectsOutputFile,
176  HistogramFileName=HistogramFileName,
177  PropagateDefectsToStatus=PropagateDefectsToStatus)
PixelDefectsEmulatorConfig.DefectsHistSvcCfg
ComponentAccumulator DefectsHistSvcCfg(flags, str HistogramGroup="PixelDefects", str FileName='pixel_defects.root')
Definition: PixelDefectsEmulatorConfig.py:23
StripDefectsEmulatorConfig.moduleDefect
def moduleDefect(bec=[-2, 2], layer=[0, 8], eta_range=[-99, 99], phi_range=[-99, 99], columns_or_strips=[0, 2000], column_or_strip_length=[0, 1000000], side_range=[0, 1], all_rows=True, probability=[1e-2], fractionsOfNDefects=[], noiseProbability=None, noiseShape=[], cornerDefectParam=None, cornerDefectNCornerFractions=None)
Definition: StripDefectsEmulatorConfig.py:50
PlotCalibFromCool.norm
norm
Definition: PlotCalibFromCool.py:100
StripDefectsEmulatorPostInclude.emulateITkStripDefectsDefault
def emulateITkStripDefectsDefault(flags, cfg, float StripDefectProb=1e-2, float ModuleDefectProb=1e-2, float NoiseProb=0., RngPerDefectType=False, DefectsInputFiles=[], DefectsOutputFile=None, str HistogramFileName=None, PropagateDefectsToStatus=True)
Definition: StripDefectsEmulatorPostInclude.py:159
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
StripDefectsEmulatorConfig.combineModuleDefects
def combineModuleDefects(defects)
Definition: StripDefectsEmulatorConfig.py:138
StripDefectsEmulatorConfig.ITkStripDefectsEmulatorCondAlgCfg
ComponentAccumulator ITkStripDefectsEmulatorCondAlgCfg(flags, str name="ITkStripDefectsEmulatorCondAlg", **dict kwargs)
Definition: StripDefectsEmulatorConfig.py:204
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
Constants
some useful constants -------------------------------------------------—
StripDefectsEmulatorConfig.ITkStripDefectsEmulatorToDetectorElementStatusCondAlgCfg
ComponentAccumulator ITkStripDefectsEmulatorToDetectorElementStatusCondAlgCfg(flags, str name="ITkStripDefectsEmulatorToDetectorElementStatusCondAlgCfg", **dict kwargs)
Definition: StripDefectsEmulatorConfig.py:274
StripDefectsEmulatorPostInclude.emulateITkStripDefects
def emulateITkStripDefects(flags, cfg, float StripDefectProb=1e-2, float ModuleDefectProb=1e-2, float NoiseProb=0., int MaxRandomPositionAttempts=10, RngPerDefectType=False, DefectsInputFiles=[], DefectsOutputFile=None, bool FillHistogramsPerPattern=True, bool FillEtaPhiHistogramsPerPattern=True, str HistogramGroupName="ITkStripDefects", str HistogramFileName=None, PropagateDefectsToStatus=True)
Definition: StripDefectsEmulatorPostInclude.py:32
StripDefectsEmulatorConfig.ITkStripDefectsEmulatorAlgCfg
ComponentAccumulator ITkStripDefectsEmulatorAlgCfg(flags, str name="ITkStripDefectsEmulatorAlg", **dict kwargs)
Definition: StripDefectsEmulatorConfig.py:250