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