1 from tracking.validation.harvesting
import HarvestingModule
4 from ROOT
import Belle2
7 from tracking.validation.pr_side_module
import PRSideTrackingValidationModule
10 from tracking.ipython_tools.wrapper
import QueueHarvester
15 """ The base class with all static methods to use. """
19 """Apply a PXD selection to a PXDCluster or an SVD selection to an SVDCluster"""
20 cluster_type = cluster.__class__.__name__
21 if cluster_type ==
"Belle2::PXDCluster":
22 return pxd_function(cluster)
23 elif cluster_type ==
"Belle2::SVDCluster":
24 return svd_function(cluster)
26 raise TypeError(
"Unknown hit type")
30 """Return lists of charges and path lengths for the clusters associated with an MCParticle"""
36 momentum = mc_particle.getMomentum()
37 position = mc_particle.getProductionVertex()
38 charge = mc_particle.getCharge()
40 helix =
Belle2.Helix(position, momentum, charge, b_field)
42 for cluster
in clusters:
44 charge = tools.getCalibratedCharge(cluster)
45 path_length = tools.getPathLength(cluster, helix)
47 charge_list.append(calibrated_charge)
48 path_length_list.append(path_length)
50 return charge_list, path_length_list, list(np.divide(charge_list, path_length_list))
54 """Sort then truncate a list to all but the last 2 entries or the first 4 entries or the first 6 entries"""
55 sorted_list = sorted(charge_list)
56 if len(sorted_list) > 2:
57 return sorted_list[:-2], sorted_list[:4], sorted_list[:6]
59 return sorted_list, sorted_list[:4], sorted_list[:6]
64 """ A harvester to check for the positions of the track points in the MCParticleTrajectories"""
68 HarvestingModule.__init__(self, foreach=
"MCParticleTrajectorys", output_file_name=
"mc_trajectory.root")
70 def peel(self, mc_particle_trajectory):
71 """Aggregate track-point position information for the trajectory"""
72 for track_point
in mc_particle_trajectory:
73 yield {
"x": track_point.x,
"y": track_point.y,
"z": track_point.z,
"index": self.counter}
77 save_tree = refiners.SaveTreeRefiner()
82 """ A harvester to redo parts of the analysis in the Belle II Paper by Robert """
86 HarvestingModule.__init__(self, foreach=
"MCParticles", output_file_name=
"mc_particle.root")
88 def pick(self, mc_particle):
89 """Select the MCParticle if it is a primary pion and has some PXD and/or SVD clusters"""
90 pxd_clusters = mc_particle.getRelationsFrom(
"PXDClusters")
91 svd_clusters = mc_particle.getRelationsFrom(
"SVDClusters")
92 return (mc_particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle)
and
93 abs(mc_particle.getPDG()) == 211
and
94 len(pxd_clusters) + len(svd_clusters) > 0)
97 """Create a dictionary from the lists of charges, normalized charges, and path lengths of the
98 clusters associated with an MCParticle"""
101 truncated, first4, first6 = VXDMomentumEnergyEstimator.generate_truncated(normalized_charge_list)
103 result.update({
"sum_%s_charges" % name: sum(charge_list)})
104 result.update({
"mean_%s_charges" % name: np.mean(charge_list)})
105 result.update({
"sum_%s_normalized_charges" % name: sum(normalized_charge_list)})
106 result.update({
"mean_%s_normalized_charges" % name: np.mean(normalized_charge_list)})
108 result.update({
"sum_%s_normalized_charges_truncated" % name: sum(truncated)})
109 result.update({
"mean_%s_normalized_charges_truncated" % name: np.mean(truncated)})
110 result.update({
"sum_%s_normalized_charges_first4" % name: sum(first4)})
111 result.update({
"mean_%s_normalized_charges_first4" % name: np.mean(first4)})
112 result.update({
"sum_%s_normalized_charges_first6" % name: sum(first6)})
113 result.update({
"mean_%s_normalized_charges_first6" % name: np.mean(first6)})
118 """Aggregate the PXD and SVD cluster information for an MCParticle"""
120 pxd_clusters = mc_particle.getRelationsFrom(
"PXDClusters")
121 svd_clusters = mc_particle.getRelationsFrom(
"SVDClusters")
123 pxd_results = VXDMomentumEnergyEstimator.calculate_charges_and_path_lengths_for_one_type(pxd_clusters, mc_particle)
124 svd_results = VXDMomentumEnergyEstimator.calculate_charges_and_path_lengths_for_one_type(svd_clusters, mc_particle)
127 pxd_charges, pxd_path_length, pxd_normalized_charges = pxd_results
129 svd_charges, svd_path_length, svd_normalized_charges = svd_results
133 pxd_path_length + svd_path_length,
134 pxd_normalized_charges + svd_normalized_charges,
137 result.update(pxd_cluster_dicts)
138 result.update(svd_cluster_dicts)
139 result.update(combined_cluster_dicts)
145 save_tree = refiners.SaveTreeRefiner()
150 """ A base class for the VXD hitwise analysis. Collect dE/dX and the correct p of each hit of the MC particles. """
152 def __init__(self, clusters, detector, output_file_name, use_mc_info=True):
154 HarvestingModule.__init__(self, foreach=
"TrackCands", output_file_name=output_file_name)
170 """Determine if a cluster has an associated SpacePoint and is associated with a primary pion MCParticle"""
171 mc_particles = cluster.getRelationsTo(
"MCParticles")
173 space_point = cluster.getRelated(
"SpacePoints")
175 if space_point
is None:
178 for mc_particle
in mc_particles:
179 if (mc_particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle)
and
180 abs(mc_particle.getPDG()) == 211):
187 """Get the PXD tools for a PXD cluster or the SVD tools for an SVD cluster"""
188 return VXDMomentumEnergyEstimator.do_for_each_hit_type(
194 """Aggregate the cluster and MCParticle (for primary pion) information associated with the track candidate"""
195 vxd_hit_ids = track_cand.getHitIDs(self.
detector)
199 for vxd_hit_id
in vxd_hit_ids:
200 cluster = vxd_hits[vxd_hit_id]
207 mc_particles = cluster.getRelationsTo(
"MCParticles")
209 for mc_particle
in mc_particles:
210 if (mc_particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle)
and abs(mc_particle.getPDG()) == 211):
213 track_momentum = mc_particle.getMomentum()
214 track_position = mc_particle.getProductionVertex()
215 track_charge = mc_particle.getCharge()
217 track_momentum = track_cand.getMomSeed()
218 track_position = track_cand.getPosSeed()
219 track_charge = track_cand.getChargeSeed()
222 track_helix =
Belle2.Helix(track_position, track_momentum, int(track_charge), b_field)
224 cluster_charge = tools.getCalibratedCharge(cluster)
225 path_length = tools.getPathLength(cluster, track_helix)
226 cluster_thickness = tools.getThicknessOfCluster(cluster)
227 cluster_radius = tools.getRadiusOfCluster(cluster)
228 cluster_width = tools.getWidthOfCluster(cluster)
229 cluster_length = tools.getLengthOfCluster(cluster)
231 perp_s_at_cluster_entry = track_helix.getArcLength2DAtCylindricalR(cluster_radius)
232 perp_s_at_cluster_exit = track_helix.getArcLength2DAtCylindricalR(cluster_radius + cluster_thickness)
234 mc_momentum = tools.getEntryMomentumOfMCParticle(cluster)
235 mc_position = tools.getEntryPositionOfMCParticle(cluster)
238 mc_helix =
Belle2.Helix(mc_position, mc_momentum, int(track_charge), mc_b_field)
239 mc_path_length = tools.getPathLength(cluster, mc_helix)
241 cluster_is_u = VXDMomentumEnergyEstimator.do_for_each_hit_type(
243 lambda cluster: cluster.isUCluster(),
244 lambda cluster: np.NaN)
246 cluster_is_pxd = VXDMomentumEnergyEstimator.do_for_each_hit_type(
248 lambda cluster:
False,
249 lambda cluster:
True)
251 cluster_layer = tools.getLayerOfCluster(cluster)
252 cluster_segment = tools.getSegmentNumberOfCluster(cluster)
253 cluster_ladder = tools.getLadderOfCluster(cluster)
254 cluster_sensor = tools.getSensorNumberOfCluster(cluster)
256 cluster_dict = dict(cluster_charge=cluster_charge,
257 cluster_thickness=cluster_thickness,
258 cluster_radius=cluster_radius,
259 cluster_is_u=cluster_is_u,
260 cluster_is_pxd=cluster_is_pxd,
261 cluster_layer=cluster_layer,
262 cluster_segment=cluster_segment,
263 cluster_ladder=cluster_ladder,
264 cluster_width=cluster_width,
265 cluster_length=cluster_length,
266 cluster_sensor=cluster_sensor)
268 mc_at_hit_dict = dict(mc_helix_perigee_x=mc_helix.getPerigeeX(),
269 mc_helix_perigee_y=mc_helix.getPerigeeY(),
270 mc_helix_perigee_z=mc_helix.getPerigeeZ(),
271 mc_helix_momentum_x=mc_helix.getMomentumX(mc_b_field),
272 mc_helix_momentum_y=mc_helix.getMomentumY(mc_b_field),
273 mc_helix_momentum_z=mc_helix.getMomentumZ(mc_b_field),
274 mc_position=mc_position.Mag(),
275 mc_position_x=mc_position.X(),
276 mc_position_y=mc_position.Y(),
277 mc_position_z=mc_position.Z(),
278 mc_momentum=mc_momentum.Mag(),
279 mc_momentum_x=mc_momentum.X(),
280 mc_momentum_y=mc_momentum.Y(),
281 mc_momentum_z=mc_momentum.Z())
283 dedx_dict = dict(dedx=cluster_charge / path_length,
284 dedx_with_mc=cluster_charge / mc_path_length,
285 dedx_with_thickness=cluster_charge / cluster_thickness,
287 perp_s_at_cluster_entry=perp_s_at_cluster_entry,
288 perp_s_at_cluster_exit=perp_s_at_cluster_exit,
289 track_charge=track_charge,
290 path_length=path_length,
291 mc_path_length=mc_path_length,
292 p_origin=mc_particle.getMomentum().Mag())
294 track_dict = dict(track_helix_perigee_x=track_helix.getPerigeeX(),
295 track_helix_perigee_y=track_helix.getPerigeeY(),
296 track_helix_perigee_z=track_helix.getPerigeeZ(),
297 track_helix_momentum_x=track_helix.getMomentumX(b_field),
298 track_helix_momentum_y=track_helix.getMomentumY(b_field),
299 track_helix_momentum_z=track_helix.getMomentumZ(b_field))
302 result_dict.update(cluster_dict)
303 result_dict.update(mc_at_hit_dict)
304 result_dict.update(dedx_dict)
305 result_dict.update(track_dict)
311 save_tree = refiners.SaveTreeRefiner()
315 """Collect dE/dX and the correct p of each PXD hit of the MC particles. """
319 VXDHarvester.__init__(self, clusters=
"PXDClusters", detector=Belle2.Const.PXD, output_file_name=output_file_name,
320 use_mc_info=use_mc_info)
324 """Collect dE/dX and the correct p of each SVD hit of the MC particles. """
328 VXDHarvester.__init__(self, clusters=
"SVDClusters", detector=Belle2.Const.SVD, output_file_name=output_file_name,
329 use_mc_info=use_mc_info)
333 """Collect dE/dX and the correct p of each VXD hit associated with the fitted tracks. """
337 QueueHarvester.__init__(self, queue, foreach=
"TrackFitResults", output_file_name=output_file_name)
341 def pick(self, track_fit_result):
342 """Select a TrackFitResult if it is associated with exactly one MCParticle"""
343 mc_track_cands = track_fit_result.getRelationsFrom(
"TrackCands")
344 if len(mc_track_cands) != 1:
347 mc_track_cand = mc_track_cands[0]
348 mc_particles = self.
data_store.getRelationsFromObj(mc_track_cand,
"MCParticles")
350 return len(mc_particles) == 1
352 def peel(self, track_fit_result):
353 """Aggregate the track-fit information associated with a TrackFitResult"""
354 mc_track_cand = track_fit_result.getRelationsFrom(
"TrackCands")[0]
355 mc_particle = self.
data_store.getRelated(mc_track_cand,
"MCParticles")
357 fit_momentum = track_fit_result.getMomentum()
358 true_momentum = mc_particle.getMomentum()
360 related_reco_track = track_fit_result.getRelated(
"GF2Tracks")
361 cardinal_rep = related_reco_track.getCardinalRep()
362 kalman_fit_state = related_reco_track.getKalmanFitStatus()
364 number_of_measurements_in_total = 0
365 number_of_measurements_with_smaller_weight = 0
367 number_of_momentum_measurements_in_total = 0
368 number_of_momentum_measurements_with_smaller_weight = 0
370 for track_point_ID
in range(related_reco_track.getNumPointsWithMeasurement()):
371 track_point = related_reco_track.getPointWithMeasurement(track_point_ID)
373 is_momentum_measurement = track_point.getRawMeasurement().__class__.__name__ ==
"genfit::PlanarMomentumMeasurement"
375 if is_momentum_measurement:
376 number_of_momentum_measurements_in_total += 1
378 if track_point.hasFitterInfo(cardinal_rep):
379 fitter_info = track_point.getFitterInfo(cardinal_rep)
380 num_measurements = fitter_info.getNumMeasurements()
382 for measurement_id
in range(num_measurements):
383 number_of_measurements_in_total += 1
384 weight = fitter_info.getMeasurementOnPlane(measurement_id).getWeight()
386 number_of_measurements_with_smaller_weight += 1
388 if is_momentum_measurement:
389 number_of_momentum_measurements_with_smaller_weight += 1
391 return dict(fit_momentum_x=fit_momentum.X(),
392 fit_momentum_y=fit_momentum.Y(),
393 fit_momentum_z=fit_momentum.Z(),
394 p_value=kalman_fit_state.getForwardPVal(),
395 backward_p_value=kalman_fit_state.getBackwardPVal(),
396 true_momentum_x=true_momentum.X(),
397 true_momentum_y=true_momentum.Y(),
398 true_momentum_z=true_momentum.Z(),
399 number_of_measurements_in_total=number_of_measurements_in_total,
400 number_of_measurements_with_smaller_weight=number_of_measurements_with_smaller_weight,
401 number_of_momentum_measurements_in_total=number_of_momentum_measurements_in_total,
402 number_of_momentum_measurements_with_smaller_weight=number_of_momentum_measurements_with_smaller_weight)
406 save_tree = refiners.SaveTreeRefiner()