CMS 3D CMS Logo

TrackTimeValueMapProducer.cc
Go to the documentation of this file.
1 // This producer assigns vertex times (with a specified resolution) to tracks.
2 // The times are produced as valuemaps associated to tracks, so the track
3 // dataformat doesn't need to be modified.
4 
7 
10 
12 
16 
32 
33 #include <memory>
34 #include <random>
35 
36 #include "CLHEP/Units/SystemOfUnits.h"
40 
42 public:
45 
46  void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
47 
48 private:
49  // inputs
55  // tracking particle associators by order of preference
56  const std::vector<edm::EDGetTokenT<reco::TrackToTrackingParticleAssociator>> associators_;
57  // eta bounds
59  // options
60  std::vector<std::unique_ptr<const ResolutionModel>> resolutions_;
61  // functions
62  float extractTrackVertexTime(const TrackingParticle &, const reco::TransientTrack &) const;
63 };
64 
66 
67 namespace {
68  constexpr float m_pion = 139.57061e-3;
69  const std::string resolution("Resolution");
70 
71  template <typename ParticleType, typename T>
72  void writeValueMap(edm::Event &iEvent,
74  const std::vector<T> &values,
75  const std::string &label) {
76  std::unique_ptr<edm::ValueMap<T>> valMap(new edm::ValueMap<T>());
77  typename edm::ValueMap<T>::Filler filler(*valMap);
78  filler.insert(handle, values.begin(), values.end());
79  filler.fill();
80  iEvent.put(std::move(valMap), label);
81  }
82 } // namespace
83 
85  : tracks_(consumes<edm::View<reco::Track>>(conf.getParameter<edm::InputTag>("trackSrc"))),
86  tracksName_(conf.getParameter<edm::InputTag>("trackSrc").label()),
87  trackingParticles_(consumes<TrackingParticleCollection>(conf.getParameter<edm::InputTag>("trackingParticleSrc"))),
88  trackingVertices_(consumes<TrackingVertexCollection>(conf.getParameter<edm::InputTag>("trackingVertexSrc"))),
89  pileupSummaryInfo_(
90  consumes<std::vector<PileupSummaryInfo>>(conf.getParameter<edm::InputTag>("pileupSummaryInfo"))),
91  associators_(edm::vector_transform(
92  conf.getParameter<std::vector<edm::InputTag>>("associators"),
93  [this](const edm::InputTag &tag) { return this->consumes<reco::TrackToTrackingParticleAssociator>(tag); })),
94  etaMin_(conf.getParameter<double>("etaMin")),
95  etaMax_(conf.getParameter<double>("etaMax")),
96  ptMin_(conf.getParameter<double>("ptMin")),
97  pMin_(conf.getParameter<double>("pMin")),
98  etaMaxForPtThreshold_(conf.getParameter<double>("etaMaxForPtThreshold")) {
99  // setup resolution models
100  const std::vector<edm::ParameterSet> &resos = conf.getParameterSetVector("resolutionModels");
101  for (const auto &reso : resos) {
102  const std::string &name = reso.getParameter<std::string>("modelName");
103  resolutions_.emplace_back(ResolutionModelFactory::get()->create(name, reso));
104 
105  // times and time resolutions for general tracks
106  produces<edm::ValueMap<float>>(tracksName_ + name);
107  produces<edm::ValueMap<float>>(tracksName_ + name + resolution);
108  }
109 }
110 
112  // get sim track associators
113  std::vector<edm::Handle<reco::TrackToTrackingParticleAssociator>> associators;
114  for (const auto &token : associators_) {
115  associators.emplace_back();
116  auto &back = associators.back();
117  evt.getByToken(token, back);
118  }
119 
120  std::vector<float> generalTrackTimes;
121 
122  // get track collections
123  edm::Handle<edm::View<reco::Track>> TrackCollectionH;
124  evt.getByToken(tracks_, TrackCollectionH);
125  const edm::View<reco::Track> &TrackCollection = *TrackCollectionH;
126 
127  // get tracking particle collections
129  evt.getByToken(trackingParticles_, TPCollectionH);
130 
132  evt.getByToken(pileupSummaryInfo_, pileupSummaryH);
133 
134  // transient track builder
136  es.get<TransientTrackRecord>().get("TransientTrackBuilder", theB);
137 
138  // associate the reco tracks / gsf Tracks
139  std::vector<reco::RecoToSimCollection> associatedTracks;
140  associatedTracks.reserve(associators.size());
141  for (const auto &associator : associators) {
142  associatedTracks.emplace_back(associator->associateRecoToSim(TrackCollectionH, TPCollectionH));
143  }
144 
145  double sumSimTime = 0.;
146  double sumSimTimeSq = 0.;
147  int nsim = 0;
148  for (const PileupSummaryInfo &puinfo : *pileupSummaryH) {
149  if (puinfo.getBunchCrossing() == 0) {
150  for (const float &time : puinfo.getPU_times()) {
151  double simtime = time;
152  sumSimTime += simtime;
153  sumSimTimeSq += simtime * simtime;
154  ++nsim;
155  }
156  break;
157  }
158  }
159 
160  double meanSimTime = sumSimTime / double(nsim);
161  double varSimTime = sumSimTimeSq / double(nsim) - meanSimTime * meanSimTime;
162  double rmsSimTime = std::sqrt(std::max(0.1 * 0.1, varSimTime));
163  std::normal_distribution<float> gausSimTime(meanSimTime, rmsSimTime);
164 
165  // get event-based seed for RNG
166  unsigned int runNum_uint = static_cast<unsigned int>(evt.id().run());
167  unsigned int lumiNum_uint = static_cast<unsigned int>(evt.id().luminosityBlock());
168  unsigned int evNum_uint = static_cast<unsigned int>(evt.id().event());
169  unsigned int tkChi2_uint = uint32_t(TrackCollection.empty() ? 0 : TrackCollection.refAt(0)->chi2() / 0.01);
170  std::uint32_t seed = tkChi2_uint + (lumiNum_uint << 10) + (runNum_uint << 20) + evNum_uint;
171  std::mt19937 rng(seed);
172 
173  for (unsigned itk = 0; itk < TrackCollection.size(); ++itk) {
174  const auto tkref = TrackCollection.refAt(itk);
175  reco::RecoToSimCollection::const_iterator track_tps = associatedTracks.back().end();
176 
177  for (const auto &association : associatedTracks) {
178  track_tps = association.find(tkref);
179  if (track_tps != association.end())
180  break;
181  }
182 
183  if (track_tps != associatedTracks.back().end() && track_tps->val.size() == 1) {
184  reco::TransientTrack tt = theB->build(*tkref);
185  float time = extractTrackVertexTime(*track_tps->val[0].first, tt);
186  generalTrackTimes.push_back(time);
187  } else {
188  float rndtime = gausSimTime(rng);
189  generalTrackTimes.push_back(rndtime);
190  if (track_tps != associatedTracks.back().end() && track_tps->val.size() > 1) {
191  LogDebug("TooManyTracks") << "track matched to " << track_tps->val.size() << " tracking particles!"
192  << std::endl;
193  }
194  }
195  }
196 
197  for (const auto &reso : resolutions_) {
198  const std::string &name = reso->name();
199  std::vector<float> times, resos;
200 
201  times.reserve(TrackCollection.size());
202  resos.reserve(TrackCollection.size());
203 
204  for (unsigned i = 0; i < TrackCollection.size(); ++i) {
205  const reco::Track &tk = TrackCollection[i];
206  const float absEta = std::abs(tk.eta());
207  bool inAcceptance = absEta < etaMax_ && absEta >= etaMin_ && tk.p() > pMin_ &&
208  (absEta > etaMaxForPtThreshold_ || tk.pt() > ptMin_);
209  if (inAcceptance) {
210  const float resolution = reso->getTimeResolution(tk);
211  std::normal_distribution<float> gausGeneralTime(generalTrackTimes[i], resolution);
212  times.push_back(gausGeneralTime(rng));
213  resos.push_back(resolution);
214  } else {
215  times.push_back(0.0f);
216  resos.push_back(-1.);
217  }
218  }
219 
220  writeValueMap(evt, TrackCollectionH, times, tracksName_ + name);
221  writeValueMap(evt, TrackCollectionH, resos, tracksName_ + name + resolution);
222  }
223 }
224 
226  const reco::TransientTrack &tt) const {
227  int pdgid = tp.pdgId();
228  const auto &tvertex = tp.parentVertex();
229  math::XYZTLorentzVectorD result = tvertex->position();
230 
231  // account for secondary vertices...
232  if (tvertex->nSourceTracks() && tvertex->sourceTracks()[0]->pdgId() == pdgid) {
233  auto pvertex = tvertex->sourceTracks()[0]->parentVertex();
234  result = pvertex->position();
235  while (pvertex->nSourceTracks() && pvertex->sourceTracks()[0]->pdgId() == pdgid) {
236  pvertex = pvertex->sourceTracks()[0]->parentVertex();
237  result = pvertex->position();
238  }
239  }
240 
241  float time = result.T() * CLHEP::second;
242  // correct for time of flight from track reference position
243  GlobalPoint result_pos(result.x(), result.y(), result.z());
244  const auto &tkstate = tt.trajectoryStateClosestToPoint(result_pos);
245  float tkphi = tkstate.momentum().phi();
246  float tkz = tkstate.position().z();
247  float dphi = reco::deltaPhi(tkphi, tt.track().phi());
248  float dz = tkz - tt.track().vz();
249 
250  float radius = 100. * tt.track().pt() / (0.3 * tt.field()->inTesla(GlobalPoint(0, 0, 0)).z());
251  float pathlengthrphi = tt.track().charge() * dphi * radius;
252 
253  float pathlength = std::sqrt(pathlengthrphi * pathlengthrphi + dz * dz);
254  float p = tt.track().p();
255 
256  float speed = std::sqrt(1. / (1. + m_pion / p)) * CLHEP::c_light / CLHEP::cm; // speed in cm/ns
257  float dt = pathlength / speed;
258 
259  return time - dt;
260 }
edm::StreamID
Definition: StreamID.h:30
PileupSummaryInfo.h
TrackTimeValueMapProducer::pMin_
const float pMin_
Definition: TrackTimeValueMapProducer.cc:58
mps_fire.i
i
Definition: mps_fire.py:428
TrackTimeValueMapProducer::~TrackTimeValueMapProducer
~TrackTimeValueMapProducer() override
Definition: TrackTimeValueMapProducer.cc:44
MessageLogger.h
TrackTimeValueMapProducer::resolutions_
std::vector< std::unique_ptr< const ResolutionModel > > resolutions_
Definition: TrackTimeValueMapProducer.cc:60
groupFilesInBlocks.tt
int tt
Definition: groupFilesInBlocks.py:144
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
ResolutionModel.h
sistrip::View
View
Definition: ConstantsForView.h:26
TrackTimeValueMapProducer::TrackTimeValueMapProducer
TrackTimeValueMapProducer(const edm::ParameterSet &)
Definition: TrackTimeValueMapProducer.cc:84
reco::TrackBase::p
double p() const
momentum vector magnitude
Definition: TrackBase.h:631
patZpeak.handle
handle
Definition: patZpeak.py:23
reco::deltaPhi
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
deltaPhi.h
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
simPFProducer_cfi.associators
associators
Definition: simPFProducer_cfi.py:16
math::XYZTLorentzVectorD
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89287
hgcal::association
std::tuple< layerClusterToCaloParticle, caloParticleToLayerCluster > association
Definition: LayerClusterAssociatorByEnergyScoreImpl.h:44
TrackTimeValueMapProducer::etaMaxForPtThreshold_
const float etaMaxForPtThreshold_
Definition: TrackTimeValueMapProducer.cc:58
TrackTimeValueMapProducer::pileupSummaryInfo_
const edm::EDGetTokenT< std::vector< PileupSummaryInfo > > pileupSummaryInfo_
Definition: TrackTimeValueMapProducer.cc:54
TrackingVertexCollection
std::vector< TrackingVertex > TrackingVertexCollection
Definition: TrackingVertexContainer.h:8
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
TrackTimeValueMapProducer::etaMax_
const float etaMax_
Definition: TrackTimeValueMapProducer.cc:58
TrackingVertex.h
TrackerHitAssociator.h
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
edm::EventID::luminosityBlock
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:39
watchdog.const
const
Definition: watchdog.py:83
edm::Handle
Definition: AssociativeIterator.h:50
TrackTimeValueMapProducer::extractTrackVertexTime
float extractTrackVertexTime(const TrackingParticle &, const reco::TransientTrack &) const
Definition: TrackTimeValueMapProducer.cc:225
dt
float dt
Definition: AMPTWrapper.h:136
fileCollector.seed
seed
Definition: fileCollector.py:127
reco::TrackBase::pt
double pt() const
track transverse momentum
Definition: TrackBase.h:637
MakerMacros.h
Track.h
edm::EventSetup::get
T get() const
Definition: EventSetup.h:80
TrackFwd.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrackingVertexContainer.h
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
TrackingParticle
Monte Carlo truth information used for tracking validation.
Definition: TrackingParticle.h:29
TrackTimeValueMapProducer::associators_
const std::vector< edm::EDGetTokenT< reco::TrackToTrackingParticleAssociator > > associators_
Definition: TrackTimeValueMapProducer.cc:56
TransientTrackRecord
Definition: TransientTrackRecord.h:11
reco::Track
Definition: Track.h:27
edm::ESHandle< TransientTrackBuilder >
L1TObjectsTimingClient_cff.resolution
resolution
Definition: L1TObjectsTimingClient_cff.py:52
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:531
GlobalPoint
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
ctfWithMaterialTrackMCMatch_cfi.associator
associator
Definition: ctfWithMaterialTrackMCMatch_cfi.py:7
TrackTimeValueMapProducer::trackingVertices_
const edm::EDGetTokenT< TrackingVertexCollection > trackingVertices_
Definition: TrackTimeValueMapProducer.cc:53
Point3DBase< float, GlobalTag >
cmsswSequenceInfo.tp
tp
Definition: cmsswSequenceInfo.py:17
edm::EventID::run
RunNumber_t run() const
Definition: EventID.h:38
edm::global::EDProducer
Definition: EDProducer.h:32
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::vector_transform
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
TrackTimeValueMapProducer::produce
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Definition: TrackTimeValueMapProducer.cc:111
edm::AssociationMap< edm::OneToManyWithQualityGeneric< edm::View< reco::Track >, TrackingParticleCollection, double > >::const_iterator
friend struct const_iterator
Definition: AssociationMap.h:274
edm::View
Definition: CaloClusterFwd.h:14
TrackToTrackingParticleAssociator.h
TransientTrackBuilder.h
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:223
edm::ParameterSet
Definition: ParameterSet.h:47
TrackTimeValueMapProducer
Definition: TrackTimeValueMapProducer.cc:41
Event.h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
TrackTimeValueMapProducer::tracksName_
const std::string tracksName_
Definition: TrackTimeValueMapProducer.cc:51
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
reco::TrackBase::eta
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:652
edm::EventID::event
EventNumber_t event() const
Definition: EventID.h:40
iEvent
int iEvent
Definition: GenABIO.cc:224
GsfTrack.h
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
MagneticField.h
edm::EventSetup
Definition: EventSetup.h:57
HLTSiStripMonitoring_cff.speed
speed
Definition: HLTSiStripMonitoring_cff.py:22
TrackTimeValueMapProducer::trackingParticles_
const edm::EDGetTokenT< TrackingParticleCollection > trackingParticles_
Definition: TrackTimeValueMapProducer.cc:52
TransientTrackRecord.h
get
#define get
TrackTimeValueMapProducer::etaMin_
const float etaMin_
Definition: TrackTimeValueMapProducer.cc:58
ValueMap.h
TrackingParticle.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
GsfTrackFwd.h
reco::TransientTrack
Definition: TransientTrack.h:19
isFinite.h
PVValHelper::dz
Definition: PVValidationHelpers.h:50
Frameworkfwd.h
transform.h
edm::ValueMap
Definition: ValueMap.h:107
edm::EventBase::id
edm::EventID id() const
Definition: EventBase.h:59
TrackTimeValueMapProducer::ptMin_
const float ptMin_
Definition: TrackTimeValueMapProducer.cc:58
CosmicsPD_Skims.radius
radius
Definition: CosmicsPD_Skims.py:135
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
TrackingParticleCollection
std::vector< TrackingParticle > TrackingParticleCollection
Definition: TrackingParticleFwd.h:8
TrackTimeValueMapProducer::tracks_
const edm::EDGetTokenT< edm::View< reco::Track > > tracks_
Definition: TrackTimeValueMapProducer.cc:50
TransientTrackBuilder::build
reco::TransientTrack build(const reco::Track *p) const
Definition: TransientTrackBuilder.cc:20
mps_fire.result
result
Definition: mps_fire.py:311
edm::helper::Filler
Definition: ValueMap.h:22
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
View.h
EDProducer.h
ntuplemaker.time
time
Definition: ntuplemaker.py:310
edm::Event
Definition: Event.h:73
EgammaValidation_cff.pdgid
pdgid
Definition: EgammaValidation_cff.py:30
SimTrackContainer.h
reco::TrackCollection
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
label
const char * label
Definition: PFTauDecayModeTools.cc:11
SimVertexContainer.h
PileupSummaryInfo
Definition: PileupSummaryInfo.h:22
unpackBuffers-CaloStage2.token
token
Definition: unpackBuffers-CaloStage2.py:318