test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalBarrelClusterFastTimer.cc
Go to the documentation of this file.
1 // This producer eats standard PF ECAL clusters
2 // finds the corresponding fast-timing det IDs and attempts to
3 // assign a reasonable time guess
4 
7 
11 
13 
19 
22 
27 
28 #include <memory>
29 
31 #include "CLHEP/Units/SystemOfUnits.h"
33 #include "CLHEP/Random/RandGauss.h"
36 
38 public:
41 
42  virtual void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
43 
44 private:
45  // inputs
48  // options
49  std::vector<std::unique_ptr<const ResolutionModel> > _resolutions;
50  const float minFraction_, minEnergy_;
51  const unsigned ecalDepth_;
52  // functions
53  std::pair<float, DetId> getTimeForECALPFCluster(const EcalRecHitCollection&,const reco::PFCluster&) const;
54  float correctTimeToVertex(const float intime, const DetId& timeDet, const reco::Vertex& vtx,
55  const CaloSubdetectorGeometry* ecalGeom) const;
56 };
57 
59 
60 namespace {
61  static const std::string resolution("Resolution");
62 
63  constexpr float cm_per_ns = 29.9792458;
64 
65  template<typename T>
66  void writeValueMap(edm::Event &iEvent,
67  const edm::Handle<std::vector<reco::PFCluster> > & handle,
68  const std::vector<T> & values,
69  const std::string & label) {
70  auto valMap = std::make_unique<edm::ValueMap<T>>();
71  typename edm::ValueMap<T>::Filler filler(*valMap);
72  filler.insert(handle, values.begin(), values.end());
73  filler.fill();
74  iEvent.put(std::move(valMap), label);
75  }
76 }
77 
79  ebTimeHitsToken_(consumes<EcalRecHitCollection>( conf.getParameter<edm::InputTag>("ebTimeHits") ) ),
80  ebClustersToken_(consumes<std::vector<reco::PFCluster> >( conf.getParameter<edm::InputTag>("ebClusters") ) ),
81  minFraction_( conf.getParameter<double>("minFractionToConsider") ),
82  minEnergy_(conf.getParameter<double>("minEnergyToConsider") ),
83  ecalDepth_(conf.getParameter<double>("ecalDepth") )
84 {
85  // setup resolution models
86  const std::vector<edm::ParameterSet>& resos = conf.getParameterSetVector("resolutionModels");
87  for( const auto& reso : resos ) {
88  const std::string& name = reso.getParameter<std::string>("modelName");
89  ResolutionModel* resomod = ResolutionModelFactory::get()->create(name,reso);
90  _resolutions.emplace_back( resomod );
91 
92  // times and time resolutions for general tracks
93  produces<edm::ValueMap<float> >(name);
94  produces<edm::ValueMap<float> >(name+resolution);
95  }
96  // get RNG engine
98  if (!rng.isAvailable()){
99  throw cms::Exception("Configuration")
100  << "EcalBarrelClusterFastTimer::EcalBarrelClusterFastTimer() - RandomNumberGeneratorService is not present in configuration file.\n"
101  << "Add the service in the configuration file or remove the modules that require it.";
102  }
103 }
104 
106  // get RNG engine
108  auto rng_engine = &(rng->getEngine(sid));
109 
112 
113  evt.getByToken(ebClustersToken_,clustersH);
114  evt.getByToken(ebTimeHitsToken_,timehitsH);
115 
116  const auto& clusters = *clustersH;
117  const auto& timehits = *timehitsH;
118 
119  std::vector<std::pair<float,DetId> > times; // perfect times keyed to cluster index
120  times.reserve(clusters.size());
121 
122  for( const auto& cluster : clusters ) {
123  times.push_back( getTimeForECALPFCluster( timehits, cluster ) );
124  }
125 
126  for( const auto& reso : _resolutions ) {
127  const std::string& name = reso->name();
128  std::vector<float> resolutions;
129  std::vector<float> smeared_times;
130  resolutions.reserve(clusters.size());
131  smeared_times.reserve(clusters.size());
132 
133  // smear once then correct to multiple vertices
134  for( unsigned i = 0 ; i < clusters.size(); ++i ) {
135  const float theresolution = reso->getTimeResolution(clusters[i]);
136 
137  smeared_times.emplace_back( CLHEP::RandGauss::shoot(rng_engine, times[i].first, theresolution) );
138  resolutions.push_back( theresolution );
139  }
140 
141  writeValueMap(evt,clustersH,smeared_times,name);
142  writeValueMap(evt,clustersH,resolutions,name+resolution);
143  }
144 
145 }
146 
147 std::pair<float,DetId> EcalBarrelClusterFastTimer::getTimeForECALPFCluster(const EcalRecHitCollection& timehits, const reco::PFCluster& cluster) const {
148 
149  const auto& rhfs = cluster.recHitFractions();
150  unsigned best_hit = 0;
151  float best_energy = -1.f;
152  for( const auto& rhf : rhfs ) {
153  const auto& hitref = rhf.recHitRef();
154  const unsigned detid = hitref->detId();
155  const auto fraction = rhf.fraction();
156  const auto energy = hitref->energy();
157  if( fraction < minFraction_ || energy < minEnergy_ ) continue;
158  auto timehit = timehits.find(detid);
159  float e_hit = rhf.fraction() * hitref->energy();
160  if( e_hit > best_energy && timehit->isTimeValid() ) {
161  best_energy = e_hit;
162  best_hit = detid;
163  }
164  }
165 
166  float best_time_guess = std::numeric_limits<float>::max();
167  if( best_energy > 0. ) {
168  best_time_guess = timehits.find(best_hit)->time();
169  }
170 
171  //std::cout << "EcalBarrelFastTimer: " << best_time_guess << ' ' << best_energy << ' ' << best_hit << std::endl;
172 
173  return std::make_pair(best_time_guess,DetId(best_hit));
174 }
175 
176 float EcalBarrelClusterFastTimer::correctTimeToVertex(const float intime, const DetId& timeDet, const reco::Vertex& vtx,
177  const CaloSubdetectorGeometry* ecalGeom) const {
178  if( timeDet.rawId() == 0 ) return -999.;
179  // correct the cluster time from 0,0,0 to the primary vertex given
180  const CaloCellGeometry* cellGeometry ( ecalGeom->getGeometry( timeDet ) ) ;
181  if( nullptr == cellGeometry ) {
182  throw cms::Exception("BadECALBarrelCell")
183  << timeDet << " is not a valid ECAL Barrel DetId!";
184  }
185  //depth in mm in the middle of the layer position;
186  GlobalPoint layerPos = (dynamic_cast<const TruncatedPyramid*>(cellGeometry))->getPosition( ecalDepth_+0.5 );
187  const math::XYZPoint layerPos_cm( layerPos.x(), layerPos.y(), layerPos.z() );
188  const math::XYZVector to_center = layerPos_cm - math::XYZPoint(0.,0.,0.);
189  const math::XYZVector to_vtx = layerPos_cm - vtx.position();
190 
191  /*
192  std::cout << intime << ' ' << to_center.r()/cm_per_ns << ' ' << to_vtx.r()/cm_per_ns
193  << ' ' << intime + to_center.r()/cm_per_ns - to_vtx.r()/cm_per_ns << std::endl;
194  */
195 
196  return intime + to_center.r()/cm_per_ns - to_vtx.r()/cm_per_ns;
197 }
int i
Definition: DBlmapReader.cc:9
VParameterSet const & getParameterSetVector(std::string const &name) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::EDGetTokenT< std::vector< reco::PFCluster > > ebClustersToken_
edm::Service< edm::RandomNumberGenerator > rng
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
T y() const
Definition: PV3DBase.h:63
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
const Point & position() const
position
Definition: Vertex.h:109
#define constexpr
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
std::vector< std::unique_ptr< const ResolutionModel > > _resolutions
int iEvent
Definition: GenABIO.cc:230
T z() const
Definition: PV3DBase.h:64
EcalBarrelClusterFastTimer(const edm::ParameterSet &)
float correctTimeToVertex(const float intime, const DetId &timeDet, const reco::Vertex &vtx, const CaloSubdetectorGeometry *ecalGeom) const
def move
Definition: eostools.py:510
bool isAvailable() const
Definition: Service.h:46
tuple handle
Definition: patZpeak.py:22
const std::vector< edm::ParameterSet > & resos
edm::EDGetTokenT< EcalRecHitCollection > ebTimeHitsToken_
Definition: DetId.h:18
virtual void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
A base class to handle the particular shape of Ecal Xtals. Taken from ORCA Calorimetry Code...
iterator find(key_type k)
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:72
std::pair< float, DetId > getTimeForECALPFCluster(const EcalRecHitCollection &, const reco::PFCluster &) const
T x() const
Definition: PV3DBase.h:62
T get(const Candidate &c)
Definition: component.h:55