CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DeDxEstimatorProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DeDxEstimatorProducer
4 // Class: DeDxEstimatorProducer
5 //
13 //
14 // Original Author: andrea
15 // Created: Thu May 31 14:09:02 CEST 2007
16 // Code Updates: loic Quertenmont (querten)
17 // Created: Thu May 10 14:09:02 CEST 2008
18 //
19 //
20 
21 
22 // system include files
23 #include <memory>
25 
32 
37 
41 
44 
45 
46 using namespace reco;
47 using namespace std;
48 using namespace edm;
49 
51 {
52 
53  produces<ValueMap<DeDxData> >();
54 
55 
56  string estimatorName = iConfig.getParameter<string>("estimator");
57  if(estimatorName == "median") m_estimator = new MedianDeDxEstimator(-2.);
58  if(estimatorName == "generic") m_estimator = new GenericAverageDeDxEstimator (iConfig.getParameter<double>("exponent"));
59  if(estimatorName == "truncated") m_estimator = new TruncatedAverageDeDxEstimator(iConfig.getParameter<double>("fraction"));
60  if(estimatorName == "unbinnedFit") m_estimator = new UnbinnedFitDeDxEstimator();
61 
62  MaxNrStrips = iConfig.getUntrackedParameter<unsigned>("maxNrStrips" , 255);
63  MinTrackHits = iConfig.getUntrackedParameter<unsigned>("MinTrackHits" , 4);
64 
65  m_tracksTag = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"));
66  m_trajTrackAssociationTag = consumes<TrajTrackAssociationCollection>(iConfig.getParameter<edm::InputTag>("trajectoryTrackAssociation"));
67 
68  usePixel = iConfig.getParameter<bool>("UsePixel");
69  useStrip = iConfig.getParameter<bool>("UseStrip");
70  MeVperADCPixel = iConfig.getParameter<double>("MeVperADCPixel");
71  MeVperADCStrip = iConfig.getParameter<double>("MeVperADCStrip");
72 
73  shapetest = iConfig.getParameter<bool>("ShapeTest");
74  useCalibration = iConfig.getParameter<bool>("UseCalibration");
75  m_calibrationPath = iConfig.getParameter<string>("calibrationPath");
76 
77  if(!usePixel && !useStrip)
78  edm::LogWarning("DeDxHitsProducer") << "Pixel Hits AND Strip Hits will not be used to estimate dEdx --> BUG, Please Update the config file";
79 }
80 
81 
83 {
84  delete m_estimator;
85 }
86 
87 // ------------ method called once each job just before starting event loop ------------
89 {
90  if(MODsColl.size()!=0)return;
91 
92 
94  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
95 
96  vector<GeomDet*> Det = tkGeom->dets();
97  for(unsigned int i=0;i<Det.size();i++){
98  DetId Detid = Det[i]->geographicalId();
99 
100  StripGeomDetUnit* StripDetUnit = dynamic_cast<StripGeomDetUnit*> (Det[i]);
101  PixelGeomDetUnit* PixelDetUnit = dynamic_cast<PixelGeomDetUnit*> (Det[i]);
102 
103  double Thick=-1, Dist=-1, Norma=-1;
104  if(StripDetUnit){
105  Dist = StripDetUnit->position().mag();
106  Thick = StripDetUnit->surface().bounds().thickness();
107  Norma = MeVperADCStrip/Thick;
108  }else if(PixelDetUnit){
109  Dist = PixelDetUnit->position().mag();
110  Thick = PixelDetUnit->surface().bounds().thickness();
111  Norma = MeVperADCPixel/Thick;
112  }
113 
114  stModInfo* MOD = new stModInfo;
115  MOD->DetId = Detid.rawId();
116  MOD->Thickness = Thick;
117  MOD->Distance = Dist;
118  MOD->Normalization = Norma;
119  MOD->Gain = 1;
120  MODsColl[MOD->DetId] = MOD;
121  }
122 
123  MakeCalibrationMap();
124 }
125 
126 // ------------ method called once each job just after ending the event loop ------------
128  MODsColl.clear();
129 }
130 
131 
132 
133 
135 {
136  auto_ptr<ValueMap<DeDxData> > trackDeDxEstimateAssociation(new ValueMap<DeDxData> );
137  ValueMap<DeDxData>::Filler filler(*trackDeDxEstimateAssociation);
138 
139  Handle<TrajTrackAssociationCollection> trajTrackAssociationHandle;
140  iEvent.getByToken(m_trajTrackAssociationTag, trajTrackAssociationHandle);
141  const TrajTrackAssociationCollection & TrajToTrackMap = *trajTrackAssociationHandle.product();
142 
143  edm::Handle<reco::TrackCollection> trackCollectionHandle;
144  iEvent.getByToken(m_tracksTag,trackCollectionHandle);
145 
146  size_t n = TrajToTrackMap.size();
147  std::vector<DeDxData> dedxEstimate(n);
148 
149  //assume trajectory collection size is equal to track collection size and that order is kept
150  int j=0;
151  for(TrajTrackAssociationCollection::const_iterator cit=TrajToTrackMap.begin(); cit!=TrajToTrackMap.end(); cit++,j++){
152 
153  const edm::Ref<std::vector<Trajectory> > traj = cit->key;
154  const reco::TrackRef track = cit->val;
155 
156  DeDxHitCollection dedxHits;
157  vector<DeDxTools::RawHits> hits;
158 // DeDxTools::trajectoryRawHits(traj, hits, usePixel, useStrip);
159 
160  const vector<TrajectoryMeasurement> & measurements = traj->measurements();
161  for(vector<TrajectoryMeasurement>::const_iterator it = measurements.begin(); it!=measurements.end(); it++){
162  TrajectoryStateOnSurface trajState=it->updatedState();
163  if( !trajState.isValid()) continue;
164 
165  const TrackingRecHit * recHit=(*it->recHit()).hit();
166  LocalVector trackDirection = trajState.localDirection();
167  double cosine = trackDirection.z()/trackDirection.mag();
168 
169  if(const SiStripMatchedRecHit2D* matchedHit=dynamic_cast<const SiStripMatchedRecHit2D*>(recHit)){
170  if(!useStrip) continue;
171  DeDxTools::RawHits mono,stereo;
172  mono.trajectoryMeasurement = &(*it);
173  stereo.trajectoryMeasurement = &(*it);
174  mono.angleCosine = cosine;
175  stereo.angleCosine = cosine;
176 
177  mono.charge = getCharge(DeDxTools::GetCluster(matchedHit->monoHit()),mono.NSaturating,matchedHit->monoId());
178  stereo.charge = getCharge(DeDxTools::GetCluster(matchedHit->stereoHit()),stereo.NSaturating,matchedHit->stereoId());
179 
180  mono.detId= matchedHit->monoId();
181  stereo.detId= matchedHit->stereoId();
182 
183  if(shapetest && !(DeDxTools::shapeSelection((DeDxTools::GetCluster(matchedHit->stereoHit()))->amplitudes()))) hits.push_back(stereo);
184  if(shapetest && !(DeDxTools::shapeSelection((DeDxTools::GetCluster(matchedHit-> monoHit()))->amplitudes()))) hits.push_back(mono);
185  }else if(const ProjectedSiStripRecHit2D* projectedHit=dynamic_cast<const ProjectedSiStripRecHit2D*>(recHit)) {
186  if(!useStrip) continue;
187  const SiStripRecHit2D* singleHit=&(projectedHit->originalHit());
188  DeDxTools::RawHits mono;
189 
190  mono.trajectoryMeasurement = &(*it);
191  mono.angleCosine = cosine;
192  mono.charge = getCharge(DeDxTools::GetCluster(singleHit),mono.NSaturating,singleHit->geographicalId());
193  mono.detId= singleHit->geographicalId();
194  if(shapetest && !(DeDxTools::shapeSelection((DeDxTools::GetCluster(singleHit))->amplitudes()))) continue;
195  hits.push_back(mono);
196  }else if(const SiStripRecHit2D* singleHit=dynamic_cast<const SiStripRecHit2D*>(recHit)){
197  if(!useStrip) continue;
198  DeDxTools::RawHits mono;
199 
200  mono.trajectoryMeasurement = &(*it);
201  mono.angleCosine = cosine;
202  mono.charge = getCharge(DeDxTools::GetCluster(singleHit),mono.NSaturating,singleHit->geographicalId());
203  mono.detId= singleHit->geographicalId();
204  if(shapetest && !(DeDxTools::shapeSelection((DeDxTools::GetCluster(singleHit))->amplitudes()))) continue;
205  hits.push_back(mono);
206  }else if(const SiStripRecHit1D* single1DHit=dynamic_cast<const SiStripRecHit1D*>(recHit)){
207  if(!useStrip) continue;
208  DeDxTools::RawHits mono;
209 
210  mono.trajectoryMeasurement = &(*it);
211  mono.angleCosine = cosine;
212  mono.charge = getCharge(DeDxTools::GetCluster(single1DHit),mono.NSaturating,single1DHit->geographicalId());
213  mono.detId= single1DHit->geographicalId();
214  if(shapetest && !(DeDxTools::shapeSelection((DeDxTools::GetCluster(single1DHit))->amplitudes()))) continue;
215  hits.push_back(mono);
216  }else if(const SiPixelRecHit* pixelHit=dynamic_cast<const SiPixelRecHit*>(recHit)){
217  if(!usePixel) continue;
218 
219  DeDxTools::RawHits pixel;
220 
221  pixel.trajectoryMeasurement = &(*it);
222  pixel.angleCosine = cosine;
223  pixel.charge = pixelHit->cluster()->charge();
224  pixel.NSaturating=-1;
225  pixel.detId= pixelHit->geographicalId();
226  hits.push_back(pixel);
227  }
228  }
230 
231  int NClusterSaturating = 0;
232  for(size_t i=0; i < hits.size(); i++)
233  {
234  stModInfo* MOD = MODsColl[hits[i].detId];
235  float pathLen = MOD->Thickness/std::abs(hits[i].angleCosine);
236  float charge = MOD->Normalization*hits[i].charge*std::abs(hits[i].angleCosine);
237  dedxHits.push_back( DeDxHit( charge, MOD->Distance, pathLen, hits[i].detId) );
238 
239  if(hits[i].NSaturating>0)NClusterSaturating++;
240  }
241 
242  sort(dedxHits.begin(),dedxHits.end(),less<DeDxHit>());
243  std::pair<float,float> val_and_error = m_estimator->dedx(dedxHits);
244 
245  //WARNING: Since the dEdX Error is not properly computed for the moment
246  //It was decided to store the number of saturating cluster in that dataformat
247  val_and_error.second = NClusterSaturating;
248 
249  dedxEstimate[j] = DeDxData(val_and_error.first, val_and_error.second, dedxHits.size() );
250  }
251  filler.insert(trackCollectionHandle, dedxEstimate.begin(), dedxEstimate.end());
252 
253  // really fill the association map
254  filler.fill();
255  // put into the event
256  iEvent.put(trackDeDxEstimateAssociation);
257 }
258 
259 
260 
262 {
263  if(!useCalibration)return;
264 
265  TChain* t1 = new TChain("SiStripCalib/APVGain");
266  t1->Add(m_calibrationPath.c_str());
267 
268  unsigned int tree_DetId;
269  unsigned char tree_APVId;
270  double tree_Gain;
271 
272  t1->SetBranchAddress("DetId" ,&tree_DetId );
273  t1->SetBranchAddress("APVId" ,&tree_APVId );
274  t1->SetBranchAddress("Gain" ,&tree_Gain );
275 
276 
277 
278  for (unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
279  t1->GetEntry(ientry);
280  stModInfo* MOD = MODsColl[tree_DetId];
281  MOD->Gain = tree_Gain;
282  }
283 
284  delete t1;
285 
286 }
287 
288 
289 int DeDxEstimatorProducer::getCharge(const SiStripCluster* Cluster, int& Saturating_Strips,
290  const uint32_t & DetId)
291 {
292  //const vector<uint8_t>& Ampls = Cluster->amplitudes();
293  const vector<uint8_t>& Ampls = Cluster->amplitudes();
294  //uint32_t DetId = Cluster->geographicalId();
295 
296 // float G=1.0f;
297 
298  int toReturn = 0;
299  Saturating_Strips = 0;
300  for(unsigned int i=0;i<Ampls.size();i++){
301  int CalibratedCharge = Ampls[i];
302 
303  if(useCalibration){
304  stModInfo* MOD = MODsColl[DetId];
305 // G = MOD->Gain;
306  CalibratedCharge = (int)(CalibratedCharge / MOD->Gain );
307  if(CalibratedCharge>=1024){
308  CalibratedCharge = 255;
309  }else if(CalibratedCharge>=255){
310  CalibratedCharge = 254;
311  }
312  }
313 
314  toReturn+=CalibratedCharge;
315  if(CalibratedCharge>=254)Saturating_Strips++;
316  }
317 
318 // printf("Charge = %i --> %i (Gain=%f)\n", accumulate(Ampls.begin(), Ampls.end(), 0), toReturn, G);
319 
320 
321  return toReturn;
322 }
323 
324 
325 
326 
327 //define this as a plug-in
T getParameter(std::string const &) const
DeDxEstimatorProducer(const edm::ParameterSet &)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
const SiStripCluster * GetCluster(const TrackerSingleRecHit *hit)
Definition: DeDxTools.h:27
const_iterator end() const
last iterator over the map (read only)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
LocalVector localDirection() const
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
std::vector< DeDxHit > DeDxHitCollection
Definition: DeDxHit.h:49
const Bounds & bounds() const
Definition: Surface.h:128
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:35
double charge(const std::vector< uint8_t > &Ampls)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
virtual float thickness() const =0
virtual void beginRun(edm::Run const &run, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:243
T mag() const
Definition: PV3DBase.h:67
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:41
double angleCosine
Definition: DeDxTools.h:21
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
T z() const
Definition: PV3DBase.h:64
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
DEFINE_FWK_MODULE(CosmicTrackingParticleSelector)
Definition: DetId.h:18
bool shapeSelection(const std::vector< uint8_t > &ampls)
Definition: DeDxTools.cc:134
size_type size() const
map size
const TrajectoryMeasurement * trajectoryMeasurement
Definition: DeDxTools.h:23
const T & get() const
Definition: EventSetup.h:55
key_type key() const
Accessor for product key.
Definition: Ref.h:266
T const * product() const
Definition: Handle.h:81
virtual void produce(edm::Event &, const edm::EventSetup &) override
const_iterator begin() const
first iterator over the map (read only)
const std::vector< uint8_t > & amplitudes() const
Definition: Run.h:41
Pixel Reconstructed Hit.
int getCharge(const SiStripCluster *Cluster, int &Saturating_Strips, const uint32_t &)