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 dataformat doesn't
3 // need to be modified.
4 
7 
10 
12 
15 
26 
28 
29 #include <memory>
30 
32 #include "CLHEP/Units/SystemOfUnits.h"
34 #include "CLHEP/Random/RandGauss.h"
37 
40 
42 public:
45 
46  virtual 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  // options
58  std::vector<std::unique_ptr<const ResolutionModel> > resolutions_;
59  // functions
61  const std::vector<reco::RecoToSimCollection>&,
62  std::vector<float>& ) const;
63  std::pair<float,float> extractTrackVertexTime(const std::vector<std::pair<TrackingParticleRef, double> >&) const;
64 };
65 
67 
68 namespace {
69  constexpr float fakeBeamSpotTimeWidth = 0.175f; // ns
70  static const std::string generalTracksName("generalTracks");
71  static const std::string gsfTracksName("gsfTracks");
72  static const std::string resolution("Resolution");
73 
74  template<typename ParticleType, typename T>
75  void writeValueMap(edm::Event &iEvent,
77  const std::vector<T> & values,
78  const std::string & label) {
79  std::unique_ptr<edm::ValueMap<T> > valMap(new edm::ValueMap<T>());
80  typename edm::ValueMap<T>::Filler filler(*valMap);
81  filler.insert(handle, values.begin(), values.end());
82  filler.fill();
83  iEvent.put(std::move(valMap), label);
84  }
85 }
86 
88  tracks_(consumes<edm::View<reco::Track> >( conf.getParameter<edm::InputTag>("trackSrc") ) ),
89  gsfTracks_(consumes<edm::View<reco::Track> >( conf.getParameter<edm::InputTag>("gsfTrackSrc") ) ),
90  trackingParticles_(consumes<TrackingParticleCollection>( conf.getParameter<edm::InputTag>("trackingParticleSrc") ) ),
91  trackingVertices_(consumes<TrackingVertexCollection>( conf.getParameter<edm::InputTag>("trackingVertexSrc") ) ),
92  associators_( edm::vector_transform( conf.getParameter<std::vector<edm::InputTag> >("associators"), [this](const edm::InputTag& tag){ return this->consumes<reco::TrackToTrackingParticleAssociator>(tag); } ) )
93 {
94  // setup resolution models
95  const std::vector<edm::ParameterSet>& resos = conf.getParameterSetVector("resolutionModels");
96  for( const auto& reso : resos ) {
97  const std::string& name = reso.getParameter<std::string>("modelName");
98  ResolutionModel* resomod = ResolutionModelFactory::get()->create(name,reso);
99  resolutions_.emplace_back( resomod );
100 
101  // times and time resolutions for general tracks
102  produces<edm::ValueMap<float> >(generalTracksName+name);
103  produces<edm::ValueMap<float> >(generalTracksName+name+resolution);
104 
105  //for gsf tracks
106  produces<edm::ValueMap<float> >(gsfTracksName+name);
107  produces<edm::ValueMap<float> >(gsfTracksName+name+resolution);
108  }
109  // get RNG engine
111  if (!rng.isAvailable()){
112  throw cms::Exception("Configuration")
113  << "TrackTimeValueMapProducer::TrackTimeValueMapProducer() - RandomNumberGeneratorService is not present in configuration file.\n"
114  << "Add the service in the configuration file or remove the modules that require it.";
115  }
116 
117 }
118 
120  // get RNG engine
122  auto rng_engine = &(rng->getEngine(sid));
123 
124  // get sim track associators
125  std::vector<edm::Handle<reco::TrackToTrackingParticleAssociator> > associators;
126  for( const auto& token : associators_ ) {
127  associators.emplace_back();
128  auto& back = associators.back();
129  evt.getByToken(token,back);
130  }
131 
132  std::vector<float> generalTrackTimes, gsfTrackTimes;
133 
134  //get track collections
135  edm::Handle<edm::View<reco::Track> > TrackCollectionH;
136  evt.getByToken(tracks_, TrackCollectionH);
137  const edm::View<reco::Track>& TrackCollection = *TrackCollectionH;
138 
139  edm::Handle<edm::View<reco::Track> > GsfTrackCollectionH;
140  evt.getByToken(gsfTracks_, GsfTrackCollectionH);
141  const edm::View<reco::Track>& GsfTrackCollection = *GsfTrackCollectionH;
142 
143  //get tracking particle collections
145  evt.getByToken(trackingParticles_, TPCollectionH);
146  //const TrackingParticleCollection& TPCollection = *TPCollectionH;
147 
148  // associate the reco tracks / gsf Tracks
149  std::vector<reco::RecoToSimCollection> associatedTracks, associatedTracksGsf;
150  for( auto associator : associators ) {
151  associatedTracks.emplace_back(associator->associateRecoToSim(TrackCollectionH, TPCollectionH));
152  associatedTracksGsf.emplace_back(associator->associateRecoToSim(GsfTrackCollectionH, TPCollectionH));
153  }
154 
155 
156  calculateTrackTimes(TrackCollection, associatedTracks, generalTrackTimes);
157  calculateTrackTimes(GsfTrackCollection, associatedTracksGsf, gsfTrackTimes);
158 
159  for( const auto& reso : resolutions_ ) {
160  const std::string& name = reso->name();
161  std::vector<float> times, resos;
162  std::vector<float> gsf_times, gsf_resos;
163 
164  times.reserve(TrackCollection.size());
165  resos.reserve(TrackCollection.size());
166  gsf_times.reserve(GsfTrackCollection.size());
167  gsf_resos.reserve(GsfTrackCollection.size());
168 
169  for( unsigned i = 0; i < TrackCollection.size(); ++i ) {
170  const reco::Track& tk = TrackCollection[i];
171  if( edm::isFinite( generalTrackTimes[i] ) && generalTrackTimes[i] != 0.f) {
172  const float resolution = reso->getTimeResolution(tk);
173  times.push_back( CLHEP::RandGauss::shoot(rng_engine, generalTrackTimes[i], resolution) );
174  resos.push_back( resolution );
175  } else {
176  times.push_back( generalTrackTimes[i] );
177  resos.push_back( fakeBeamSpotTimeWidth );
178  }
179  }
180 
181  for( unsigned i = 0; i < GsfTrackCollection.size(); ++i ) {
182  const reco::Track& tk = GsfTrackCollection[i];
183  if( edm::isFinite( gsfTrackTimes[i] ) && gsfTrackTimes[i] != 0.f ) {
184  const float resolution = reso->getTimeResolution(tk);
185  gsf_times.push_back( CLHEP::RandGauss::shoot(rng_engine, gsfTrackTimes[i], resolution) );
186  gsf_resos.push_back( resolution );
187  } else {
188  gsf_times.push_back( gsfTrackTimes[i] );
189  gsf_resos.push_back( fakeBeamSpotTimeWidth );
190  }
191  }
192 
193  writeValueMap( evt, TrackCollectionH, times, generalTracksName+name );
194  writeValueMap( evt, TrackCollectionH, resos, generalTracksName+name+resolution );
195  writeValueMap( evt, GsfTrackCollectionH, gsf_times, gsfTracksName+name );
196  writeValueMap( evt, GsfTrackCollectionH, gsf_resos, gsfTracksName+name+resolution );
197  }
198 }
199 
201  const std::vector<reco::RecoToSimCollection>& assocs,
202  std::vector<float>& tvals ) const {
203  constexpr float flt_max = std::numeric_limits<float>::quiet_NaN();
204 
205  for( unsigned itk = 0; itk < tkcoll.size(); ++itk ) {
206  const auto tkref = tkcoll.refAt(itk);
207  reco::RecoToSimCollection::const_iterator track_tps = assocs.back().end();
208  for( const auto& association : assocs ) {
209  track_tps = association.find(tkref);
210  if( track_tps != association.end() ) break;
211  }
212  if( track_tps != assocs.back().end() ) {
213  if( !track_tps->val.size() ) {
214  tvals.push_back(flt_max);
215  } else {
216  const std::pair<float,float> time_info = extractTrackVertexTime(track_tps->val);
217  tvals.push_back(time_info.first);
218  }
219  } else {
220  tvals.push_back(flt_max);
221  }
222  }
223 }
224 
225 std::pair<float,float> TrackTimeValueMapProducer::
226 extractTrackVertexTime( const std::vector<std::pair<TrackingParticleRef, double> >& tp_list ) const {
227  float result = 0.f;
228  float result_z = 0.f;
229  for( const auto& tpref : tp_list ) {
230  const auto& tvertex = tpref.first->parentVertex();
231  result = tvertex->position().T()*CLHEP::second; // convert into nano-seconds
232  result_z = tvertex->position().Z();
233  // account for secondary vertices...
234 
235  if( tvertex->nSourceTracks() ) {
236  auto pvertex = tvertex->sourceTracks()[0]->parentVertex();
237  result = pvertex->position().T()*CLHEP::second;
238  result_z = pvertex->position().Z();
239  while( pvertex->nSourceTracks() ) {
240  pvertex = pvertex->sourceTracks()[0]->parentVertex();
241  result = pvertex->position().T()*CLHEP::second;
242  result_z = pvertex->position().Z();
243  }
244  }
245  }
246  if( tp_list.size() > 1 ) LogDebug("TooManyTracks") << "track matched to " << tp_list.size() << " tracking particles!" << std::endl;
247  return std::make_pair(result,result_z);
248 }
#define LogDebug(id)
virtual void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
const edm::EDGetTokenT< TrackingVertexCollection > trackingVertices_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
std::vector< TrackingParticle > TrackingParticleCollection
friend struct const_iterator
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
size_type size() const
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
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
#define constexpr
std::vector< std::unique_ptr< const ResolutionModel > > resolutions_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
bool isFinite(T x)
RefToBase< value_type > refAt(size_type i) const
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:230
std::pair< float, float > extractTrackVertexTime(const std::vector< std::pair< TrackingParticleRef, double > > &) const
std::vector< GsfTrack > GsfTrackCollection
collection of GsfTracks
Definition: GsfTrackFwd.h:9
const edm::EDGetTokenT< edm::HepMCProduct > hepMCProduct_
TrackTimeValueMapProducer(const edm::ParameterSet &)
const edm::EDGetTokenT< TrackingParticleCollection > trackingParticles_
std::vector< TrackingVertex > TrackingVertexCollection
const std::vector< edm::EDGetTokenT< reco::TrackToTrackingParticleAssociator > > associators_
fixed size matrix
HLT enums.
const edm::EDGetTokenT< edm::View< reco::Track > > tracks_
const edm::EDGetTokenT< edm::View< reco::Track > > gsfTracks_
void calculateTrackTimes(const edm::View< reco::Track > &, const std::vector< reco::RecoToSimCollection > &, std::vector< float > &) const
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55