98 Adding parameters whose values are randomly generated from the original file to the output file.
102 from neuland_add_random_hitpar import NeulandRandHitParAdder as ParAdder
104 NUMBER_OF_BARS = 1300
105 par_adder = ParAdder(NUMBER_OF_BARS)
109 # Use normal set function
110 par_adder.effective_speed.set(mode = Mode.COSINE, offset = 10., amp = 5., period = COS_PERIOD, err = 0.5)
111 par_adder.t_sync.set(mode = Mode.UNIFORM, offset = 15., amp = 5, period = COS_PERIOD, err = 0.5)
113 # Use alias set_uniform function. The range of the uniform distribution is [-3., 3.]
114 par_adder.t_diff.set_uniform( offset = 0., amp = 3., err = 0.5)
116 # Use alias set_cos function
117 par_adder.light_attenuation_length.set_cos( offset = 0.008, amp = 0.004, period = COS_PERIOD, err = 0.0005)
118 par_adder.energy_gain.set_cos( offset = 15., amp = 5., period = COS_PERIOD, err = 0.5)
119 par_adder.pedestal.set_cos( offset = 25., amp = 5., period = COS_PERIOD, err = 0.5)
120 par_adder.pmt_thresh.set_cos( offset = 0.75, amp = 0.25, period = COS_PERIOD, err = 0.5)
121 par_adder.saturation_coefficient.set_cos( offset = 0.5, amp = 0.4, period = COS_PERIOD, err = 0.5)
123 input_filename = "sim.par.root.0"
124 output_filename = "added_sim.par.root.0"
126 # Add a new parameter with random values with other paramters from the input file.
127 # After the call, the output parameter file will be created.
128 par_adder.insert_parameters(input_filename, output_filename);
136 @param module_num The number of bars
171 Generating a random value according to the module id and the given random generator parameters.
173 @param module_id The module ID (0-based number) of the bar
175 noise = random.uniform(-par.err, par.err)
177 if par.mode == Mode.COSINE:
178 value = par.offset + par.amp * math.cos(
179 module_id * 2 * math.pi / par.period
181 elif par.mode == Mode.CLOCK:
182 value = square(2 * math.pi * module_id / par.period) * par.amp + par.offset
184 value = random.uniform(par.offset - par.amp, par.offset + par.amp)
185 return round(value + noise, 6)
188 """Set the run id in the parameter file
194 def __assign_random_values(self, module_id: int):
196 Create a HitModulePar with random values.
198 @param module_id The module ID (0-based number) of the bar
200 one_module_par = ROOT.R3B.Neuland.HitModulePar()
201 one_module_par.module_num = module_id
220 one_module_par.effective_speed.value = effective_speed
221 one_module_par.effective_speed.error = 0
222 one_module_par.t_sync.value = t_sync
223 one_module_par.t_sync.error = 0
224 one_module_par.t_diff.value = t_diff
225 one_module_par.t_diff.error = 0
227 one_module_par.light_attenuation_length.value = light_attenuation_length
228 one_module_par.light_attenuation_length.error = 0
230 one_module_par.energy_gain.set_left(ROOT.R3B.ValueErrorD(energy_gain, 0))
231 one_module_par.energy_gain.set_right(ROOT.R3B.ValueErrorD(energy_gain, 0))
232 one_module_par.pedestal.set_left(ROOT.R3B.ValueErrorD(pedestal, 0))
233 one_module_par.pedestal.set_right(ROOT.R3B.ValueErrorD(pedestal, 0))
234 one_module_par.pmt_threshold.set_left(ROOT.R3B.ValueErrorD(pmt_thresh, 0.0))
235 one_module_par.pmt_threshold.set_right(ROOT.R3B.ValueErrorD(pmt_thresh, 0.0))
236 one_module_par.pmt_saturation.set_left(
237 ROOT.R3B.ValueErrorD(saturation_coefficient, 0.0)
239 one_module_par.pmt_saturation.set_right(
240 ROOT.R3B.ValueErrorD(saturation_coefficient, 0.0)
242 return one_module_par
244 def __check_if_all_parameters_set(self):
246 Check if all random generation parameters are set for each calibration parameter.
248 for member_str
in dir(self):
249 if not member_str.startswith(
"_")
and not callable(
250 getattr(self, member_str)
252 if not getattr(self, member_str).is_set():
253 raise RuntimeError(f
"parameter {member_str} is not set!")
257 Adding the parameters with random values to the output file
259 @param original_filepath The original input file to be added
260 @param output_filepath The output file containing the new parameter
262 input_par_file = ROOT.TFile(original_filepath,
"read")
263 output_file = ROOT.TFile(output_filepath,
"recreate")
267 for key
in input_par_file.GetListOfKeys():
268 if key.GetName()
in [f
"{self.__run_id}",
"ProcessID0"]:
271 output_file.WriteObject(var, var.GetName(),
"update")
274 par_run = input_par_file.Get(f
"{self.__run_id}")
275 cal_to_hit_par = ROOT.R3B.Neuland.Cal2HitPar(
"NeulandHitPar")
279 cal_to_hit_par.AddModulePar(one_module_par)
282 cal_to_hit_par_version = ROOT.FairParVersion(
"NeulandHitPar")
283 cal_to_hit_par_version.setRootVersion(1)
284 par_run.addParVersion(cal_to_hit_par_version)
287 output_file.WriteObject(par_run, par_run.GetName(),
"update")
288 output_file.WriteObject(cal_to_hit_par, cal_to_hit_par.GetName(),
"override")
set(self, Mode mode, float offset, float amp, int period=100, float err=0.5)
Set all parameters of random value generations with different mode and make the object valid.