-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathEDM4hepUtil.cpp
319 lines (255 loc) · 11.4 KB
/
EDM4hepUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "ActsExamples/Io/EDM4hep/EDM4hepUtil.hpp"
#include "Acts/Definitions/Common.hpp"
#include "Acts/Definitions/Units.hpp"
#include "Acts/EventData/Charge.hpp"
#include "Acts/EventData/MultiTrajectory.hpp"
#include "Acts/EventData/MultiTrajectoryHelpers.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Plugins/EDM4hep/EDM4hepUtil.hpp"
#include "ActsExamples/Digitization/MeasurementCreation.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/IndexSourceLink.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/Validation/TrackClassification.hpp"
#include "edm4hep/TrackState.h"
using namespace Acts::UnitLiterals;
namespace ActsExamples {
ActsFatras::Particle EDM4hepUtil::readParticle(
const edm4hep::MCParticle& from, const MapParticleIdFrom& particleMapper) {
ActsFatras::Barcode particleId = particleMapper(from);
ActsFatras::Particle to(particleId,
static_cast<Acts::PdgParticle>(from.getPDG()),
from.getCharge() * Acts::UnitConstants::e,
from.getMass() * Acts::UnitConstants::GeV);
// TODO do we have that in EDM4hep?
// particle.setProcess(static_cast<ActsFatras::ProcessType>(data.process));
to.setPosition4(from.getVertex()[0] * Acts::UnitConstants::mm,
from.getVertex()[1] * Acts::UnitConstants::mm,
from.getVertex()[2] * Acts::UnitConstants::mm,
from.getTime() * Acts::UnitConstants::ns);
// Only used for direction; normalization/units do not matter
Acts::Vector3 momentum = {from.getMomentum()[0], from.getMomentum()[1],
from.getMomentum()[2]};
to.setDirection(momentum.normalized());
to.setAbsoluteMomentum(momentum.norm() * 1_GeV);
return to;
}
void EDM4hepUtil::writeParticle(const ActsFatras::Particle& from,
edm4hep::MutableMCParticle to) {
// TODO what about particleId?
to.setPDG(from.pdg());
to.setCharge(from.charge() / Acts::UnitConstants::e);
to.setMass(from.mass() / Acts::UnitConstants::GeV);
to.setVertex({from.position().x(), from.position().y(), from.position().z()});
to.setMomentum({static_cast<float>(from.fourMomentum().x()),
static_cast<float>(from.fourMomentum().y()),
static_cast<float>(from.fourMomentum().z())});
}
ActsFatras::Hit EDM4hepUtil::readSimHit(
const edm4hep::SimTrackerHit& from, const MapParticleIdFrom& particleMapper,
const MapGeometryIdFrom& geometryMapper) {
ActsFatras::Barcode particleId = particleMapper(from.getMCParticle());
const auto mass = from.getMCParticle().getMass() * 1_GeV;
const Acts::Vector3 momentum{
from.getMomentum().x * 1_GeV,
from.getMomentum().y * 1_GeV,
from.getMomentum().z * 1_GeV,
};
const auto energy = std::hypot(momentum.norm(), mass);
Acts::Vector4 pos4{
from.getPosition().x * 1_mm,
from.getPosition().y * 1_mm,
from.getPosition().z * 1_mm,
from.getTime() * 1_ns,
};
Acts::Vector4 mom4{
momentum.x(),
momentum.y(),
momentum.z(),
energy,
};
Acts::Vector4 delta4 = Acts::Vector4::Zero();
delta4[Acts::eEnergy] = -from.getEDep() * Acts::UnitConstants::GeV;
Acts::GeometryIdentifier geometryId = geometryMapper(from.getCellID());
// Can extract from time, but we need a complete picture of the trajectory
// first
std::int32_t index = -1;
return ActsFatras::Hit(geometryId, particleId, pos4, mom4, mom4 + delta4,
index);
}
void EDM4hepUtil::writeSimHit(const ActsFatras::Hit& from,
edm4hep::MutableSimTrackerHit to,
const MapParticleIdTo& particleMapper,
const MapGeometryIdTo& geometryMapper) {
const Acts::Vector4& globalPos4 = from.fourPosition();
const Acts::Vector4& momentum4Before = from.momentum4Before();
const auto delta4 = from.momentum4After() - momentum4Before;
if (particleMapper) {
to.setMCParticle(particleMapper(from.particleId()));
}
if (geometryMapper) {
// TODO what about the digitization?
to.setCellID(geometryMapper(from.geometryId()));
}
to.setTime(globalPos4[Acts::eTime] / Acts::UnitConstants::ns);
to.setPosition({
globalPos4[Acts::ePos0] / Acts::UnitConstants::mm,
globalPos4[Acts::ePos1] / Acts::UnitConstants::mm,
globalPos4[Acts::ePos2] / Acts::UnitConstants::mm,
});
to.setMomentum({
static_cast<float>(momentum4Before[Acts::eMom0] /
Acts::UnitConstants::GeV),
static_cast<float>(momentum4Before[Acts::eMom1] /
Acts::UnitConstants::GeV),
static_cast<float>(momentum4Before[Acts::eMom2] /
Acts::UnitConstants::GeV),
});
to.setEDep(-delta4[Acts::eEnergy] / Acts::UnitConstants::GeV);
}
VariableBoundMeasurementProxy EDM4hepUtil::readMeasurement(
MeasurementContainer& container, const edm4hep::TrackerHitPlane& from,
const edm4hep::TrackerHitCollection* fromClusters, Cluster* toCluster,
const MapGeometryIdFrom& geometryMapper) {
// no need for digitization as we only want to identify the sensor
Acts::GeometryIdentifier geometryId = geometryMapper(from.getCellID());
auto pos = from.getPosition();
auto cov = from.getCovMatrix();
DigitizedParameters dParameters;
dParameters.indices.push_back(Acts::eBoundLoc0);
dParameters.values.push_back(pos.x);
dParameters.variances.push_back(cov[0]);
// TODO cut this out for 1D
dParameters.indices.push_back(Acts::eBoundLoc1);
dParameters.values.push_back(pos.y);
dParameters.variances.push_back(cov[2]);
dParameters.indices.push_back(Acts::eBoundTime);
dParameters.values.push_back(pos.z);
dParameters.variances.push_back(cov[5]);
auto to = createMeasurement(container, geometryId, dParameters);
if (fromClusters != nullptr) {
for (const auto objectId : from.getRawHits()) {
const auto& c = fromClusters->at(objectId.index);
// TODO get EDM4hep fixed
// misusing some fields to store ACTS specific information
// don't ask ...
ActsFatras::Segmentizer::Bin2D bin{
static_cast<unsigned int>(c.getType()),
static_cast<unsigned int>(c.getQuality())};
ActsFatras::Segmentizer::Segment2D path2D{
{Acts::Vector2::Zero(), Acts::Vector2::Zero()}};
double activation = c.getTime();
ActsFatras::Segmentizer::ChannelSegment cell{bin, path2D, activation};
toCluster->channels.push_back(cell);
}
}
return to;
}
void EDM4hepUtil::writeMeasurement(
const ConstVariableBoundMeasurementProxy& from,
edm4hep::MutableTrackerHitPlane to, const Cluster* fromCluster,
edm4hep::TrackerHitCollection& toClusters,
const MapGeometryIdTo& geometryMapper) {
Acts::GeometryIdentifier geoId = from.geometryId();
if (geometryMapper) {
// no need for digitization as we only want to identify the sensor
to.setCellID(geometryMapper(geoId));
}
const auto& parameters = from.fullParameters();
const auto& covariance = from.fullCovariance();
to.setTime(parameters[Acts::eBoundTime] / Acts::UnitConstants::ns);
to.setType(Acts::EDM4hepUtil::EDM4HEP_ACTS_POSITION_TYPE);
// TODO set uv (which are in global spherical coordinates with r=1)
to.setPosition({parameters[Acts::eBoundLoc0], parameters[Acts::eBoundLoc1],
parameters[Acts::eBoundTime]});
to.setCovMatrix({
static_cast<float>(covariance(Acts::eBoundLoc0, Acts::eBoundLoc0)),
static_cast<float>(covariance(Acts::eBoundLoc1, Acts::eBoundLoc0)),
static_cast<float>(covariance(Acts::eBoundLoc1, Acts::eBoundLoc1)),
0,
0,
0,
});
if (fromCluster != nullptr) {
for (const auto& c : fromCluster->channels) {
auto toChannel = toClusters.create();
to.addToRawHits(toChannel.getObjectID());
// TODO digitization channel
// TODO get EDM4hep fixed
// misusing some fields to store ACTS specific information
// don't ask ...
toChannel.setType(c.bin[0]);
toChannel.setQuality(c.bin[1]);
toChannel.setTime(c.activation);
}
}
}
void EDM4hepUtil::writeTrajectory(
const Acts::GeometryContext& gctx, double Bz, const Trajectories& from,
edm4hep::MutableTrack to, std::size_t fromIndex,
const Acts::ParticleHypothesis& particleHypothesis,
const IndexMultimap<ActsFatras::Barcode>& hitParticlesMap) {
const auto& multiTrajectory = from.multiTrajectory();
auto trajectoryState =
Acts::MultiTrajectoryHelpers::trajectoryState(multiTrajectory, fromIndex);
std::vector<ParticleHitCount> particleHitCount;
identifyContributingParticles(hitParticlesMap, from, fromIndex,
particleHitCount);
// TODO use particles
// TODO write track params
// auto trackParameters = from.trackParameters(fromIndex);
to.setChi2(trajectoryState.chi2Sum / trajectoryState.NDF);
to.setNdf(trajectoryState.NDF);
multiTrajectory.visitBackwards(fromIndex, [&](const auto& state) {
// we only fill the track states with non-outlier measurement
auto typeFlags = state.typeFlags();
if (!typeFlags.test(Acts::TrackStateFlag::MeasurementFlag)) {
return true;
}
edm4hep::TrackState trackState;
Acts::BoundTrackParameters parObj{state.referenceSurface().getSharedPtr(),
state.parameters(), state.covariance(),
particleHypothesis};
// Convert to LCIO track parametrization expected by EDM4hep
// This will create an ad-hoc perigee surface if the input parameters are
// not bound on a perigee surface already
Acts::EDM4hepUtil::detail::Parameters converted =
Acts::EDM4hepUtil::detail::convertTrackParametersToEdm4hep(gctx, Bz,
parObj);
trackState.D0 = converted.values[0];
trackState.Z0 = converted.values[1];
trackState.phi = converted.values[2];
trackState.tanLambda = converted.values[3];
trackState.omega = converted.values[4];
trackState.time = converted.values[5];
// Converted parameters are relative to an ad-hoc perigee surface created at
// the hit location
auto center = converted.surface->center(gctx);
trackState.referencePoint.x = center.x();
trackState.referencePoint.y = center.y();
trackState.referencePoint.z = center.z();
if (converted.covariance) {
const auto& c = converted.covariance.value();
trackState.covMatrix = {
static_cast<float>(c(0, 0)), static_cast<float>(c(1, 0)),
static_cast<float>(c(1, 1)), static_cast<float>(c(2, 0)),
static_cast<float>(c(2, 1)), static_cast<float>(c(2, 2)),
static_cast<float>(c(3, 0)), static_cast<float>(c(3, 1)),
static_cast<float>(c(3, 2)), static_cast<float>(c(3, 3)),
static_cast<float>(c(4, 0)), static_cast<float>(c(4, 1)),
static_cast<float>(c(4, 2)), static_cast<float>(c(4, 3)),
static_cast<float>(c(4, 4))};
}
to.addToTrackStates(trackState);
return true;
});
}
} // namespace ActsExamples