CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DeDxDiscriminatorProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DeDxDiscriminatorProducer
4 // Class: DeDxDiscriminatorProducer
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>
26 
30 
34 
40 
41 
42 #include "TFile.h"
43 
44 using namespace reco;
45 using namespace std;
46 using namespace edm;
47 
49 {
50 
51  produces<ValueMap<DeDxData> >();
52 
53  m_tracksTag = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"));
54  m_trajTrackAssociationTag = consumes<TrajTrackAssociationCollection>(iConfig.getParameter<edm::InputTag>("trajectoryTrackAssociation"));
55 
56  usePixel = iConfig.getParameter<bool>("UsePixel");
57  useStrip = iConfig.getParameter<bool>("UseStrip");
58  if(!usePixel && !useStrip)
59  edm::LogWarning("DeDxHitsProducer") << "Pixel Hits AND Strip Hits will not be used to estimate dEdx --> BUG, Please Update the config file";
60 
61  Formula = iConfig.getUntrackedParameter<unsigned>("Formula" , 0);
62  Reccord = iConfig.getUntrackedParameter<string> ("Reccord" , "SiStripDeDxMip_3D_Rcd");
63  ProbabilityMode = iConfig.getUntrackedParameter<string> ("ProbabilityMode" , "Accumulation");
64 
65 
66  MinTrackMomentum = iConfig.getUntrackedParameter<double> ("minTrackMomentum" , 0.0);
67  MaxTrackMomentum = iConfig.getUntrackedParameter<double> ("maxTrackMomentum" , 99999.0);
68  MinTrackEta = iConfig.getUntrackedParameter<double> ("minTrackEta" , -5.0);
69  MaxTrackEta = iConfig.getUntrackedParameter<double> ("maxTrackEta" , 5.0);
70  MaxNrStrips = iConfig.getUntrackedParameter<unsigned>("maxNrStrips" , 255);
71  MinTrackHits = iConfig.getUntrackedParameter<unsigned>("MinTrackHits" , 3);
72 
73  shapetest = iConfig.getParameter<bool>("ShapeTest");
74  useCalibration = iConfig.getParameter<bool>("UseCalibration");
75  m_calibrationPath = iConfig.getParameter<string>("calibrationPath");
76 
77  Prob_ChargePath = NULL;
78 }
79 
80 
82 
83 // ------------ method called once each job just before starting event loop ------------
85 {
87  if( strcmp(Reccord.c_str(),"SiStripDeDxMip_3D_Rcd")==0){
88  iSetup.get<SiStripDeDxMip_3D_Rcd>() .get(DeDxMapHandle_);
89  }else if(strcmp(Reccord.c_str(),"SiStripDeDxPion_3D_Rcd")==0){
90  iSetup.get<SiStripDeDxPion_3D_Rcd>().get(DeDxMapHandle_);
91  }else if(strcmp(Reccord.c_str(),"SiStripDeDxKaon_3D_Rcd")==0){
92  iSetup.get<SiStripDeDxKaon_3D_Rcd>().get(DeDxMapHandle_);
93  }else if(strcmp(Reccord.c_str(),"SiStripDeDxProton_3D_Rcd")==0){
94  iSetup.get<SiStripDeDxProton_3D_Rcd>().get(DeDxMapHandle_);
95  }else if(strcmp(Reccord.c_str(),"SiStripDeDxElectron_3D_Rcd")==0){
96  iSetup.get<SiStripDeDxElectron_3D_Rcd>().get(DeDxMapHandle_);
97  }else{
98 // printf("The reccord %s is not known by the DeDxDiscriminatorProducer\n", Reccord.c_str());
99 // printf("Program will exit now\n");
100  exit(0);
101  }
102  DeDxMap_ = *DeDxMapHandle_.product();
103 
104  double xmin = DeDxMap_.rangeX().min;
105  double xmax = DeDxMap_.rangeX().max;
106  double ymin = DeDxMap_.rangeY().min;
107  double ymax = DeDxMap_.rangeY().max;
108  double zmin = DeDxMap_.rangeZ().min;
109  double zmax = DeDxMap_.rangeZ().max;
110 
111  if(Prob_ChargePath)delete Prob_ChargePath;
112  Prob_ChargePath = new TH3D ("Prob_ChargePath" , "Prob_ChargePath" , DeDxMap_.numberOfBinsX(), xmin, xmax, DeDxMap_.numberOfBinsY() , ymin, ymax, DeDxMap_.numberOfBinsZ(), zmin, zmax);
113 
114 
115 
116  if(strcmp(ProbabilityMode.c_str(),"Accumulation")==0){
117 // printf("LOOOP ON P\n");
118  for(int i=0;i<=Prob_ChargePath->GetXaxis()->GetNbins()+1;i++){
119 // printf("LOOOP ON PATH\n");
120  for(int j=0;j<=Prob_ChargePath->GetYaxis()->GetNbins()+1;j++){
121 // printf("LOOOP ON CHARGE\n");
122 
123  double Ni = 0;
124  for(int k=0;k<=Prob_ChargePath->GetZaxis()->GetNbins()+1;k++){ Ni+=DeDxMap_.binContent(i,j,k);}
125 
126  for(int k=0;k<=Prob_ChargePath->GetZaxis()->GetNbins()+1;k++){
127  double tmp = 0;
128  for(int l=0;l<=k;l++){ tmp+=DeDxMap_.binContent(i,j,l);}
129 
130  if(Ni>0){
131  Prob_ChargePath->SetBinContent (i, j, k, tmp/Ni);
132 // printf("P=%6.2f Path=%6.2f Charge%8.2f --> Prob=%8.3f\n",Prob_ChargePath->GetXaxis()->GetBinCenter(i), Prob_ChargePath->GetYaxis()->GetBinCenter(j), Prob_ChargePath->GetZaxis()->GetBinCenter(k), tmp/Ni);
133  }else{
134  Prob_ChargePath->SetBinContent (i, j, k, 0);
135  }
136  }
137  }
138  }
139  }else if(strcmp(ProbabilityMode.c_str(),"Integral")==0){
140 // printf("LOOOP ON P\n");
141  for(int i=0;i<=Prob_ChargePath->GetXaxis()->GetNbins()+1;i++){
142 // printf("LOOOP ON PATH\n");
143  for(int j=0;j<=Prob_ChargePath->GetYaxis()->GetNbins()+1;j++){
144 // printf("LOOOP ON CHARGE\n");
145 
146  double Ni = 0;
147  for(int k=0;k<=Prob_ChargePath->GetZaxis()->GetNbins()+1;k++){ Ni+=DeDxMap_.binContent(i,j,k);}
148 
149  for(int k=0;k<=Prob_ChargePath->GetZaxis()->GetNbins()+1;k++){
150  double tmp = DeDxMap_.binContent(i,j,k);
151 
152  if(Ni>0){
153  Prob_ChargePath->SetBinContent (i, j, k, tmp/Ni);
154 // printf("P=%6.2f Path=%6.2f Charge%8.2f --> Prob=%8.3f\n",Prob_ChargePath->GetXaxis()->GetBinCenter(i), Prob_ChargePath->GetYaxis()->GetBinCenter(j), Prob_ChargePath->GetZaxis()->GetBinCenter(k), tmp/Ni);
155  }else{
156  Prob_ChargePath->SetBinContent (i, j, k, 0);
157  }
158  }
159  }
160  }
161  }else{
162 // printf("The ProbabilityMode: %s is unknown\n",ProbabilityMode.c_str());
163 // printf("The program will stop now\n");
164  exit(0);
165  }
166 
167 
168 
169 /*
170  for(int i=0;i<Prob_ChargePath->GetXaxis()->GetNbins();i++){
171  for(int j=0;j<Prob_ChargePath->GetYaxis()->GetNbins();j++){
172  double tmp = DeDxMap_.binContent(i,j);
173  Prob_ChargePath->SetBinContent (i, j, tmp);
174  printf("%i %i --> %f\n",i,j,tmp);
175  }
176  }
177 */
178 
179 
180 
181  if(MODsColl.size()==0){
183  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
184  m_tracker = tkGeom.product();
185 
186  auto const & Det = tkGeom->dets();
187  for(unsigned int i=0;i<Det.size();i++){
188  DetId Detid = Det[i]->geographicalId();
189  int SubDet = Detid.subdetId();
190 
191  if( SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID ||
192  SubDet == StripSubdetector::TOB || SubDet == StripSubdetector::TEC ){
193 
194  auto DetUnit = dynamic_cast<StripGeomDetUnit const*> (Det[i]);
195  if(!DetUnit)continue;
196 
197  const StripTopology& Topo = DetUnit->specificTopology();
198  unsigned int NAPV = Topo.nstrips()/128;
199 
200  double Eta = DetUnit->position().basicVector().eta();
201  double R = DetUnit->position().basicVector().transverse();
202  double Thick = DetUnit->surface().bounds().thickness();
203 
204  stModInfo* MOD = new stModInfo;
205  MOD->DetId = Detid.rawId();
206  MOD->SubDet = SubDet;
207  MOD->Eta = Eta;
208  MOD->R = R;
209  MOD->Thickness = Thick;
210  MOD->NAPV = NAPV;
211  MODsColl[MOD->DetId] = MOD;
212  }
213  }
214 
215  MakeCalibrationMap();
216  }
217 
218 }
219 
221 {
222  MODsColl.clear();
223 }
224 
225 
226 
228 {
229  auto_ptr<ValueMap<DeDxData> > trackDeDxDiscrimAssociation(new ValueMap<DeDxData> );
230  ValueMap<DeDxData>::Filler filler(*trackDeDxDiscrimAssociation);
231 
232  Handle<TrajTrackAssociationCollection> trajTrackAssociationHandle;
233  iEvent.getByToken(m_trajTrackAssociationTag, trajTrackAssociationHandle);
234  const TrajTrackAssociationCollection TrajToTrackMap = *trajTrackAssociationHandle.product();
235 
236  edm::Handle<reco::TrackCollection> trackCollectionHandle;
237  iEvent.getByToken(m_tracksTag,trackCollectionHandle);
238 
240  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
241  m_tracker = tkGeom.product();
242 
243  std::vector<DeDxData> dEdxDiscrims( TrajToTrackMap.size() );
244 
245  unsigned track_index = 0;
246  for(TrajTrackAssociationCollection::const_iterator it = TrajToTrackMap.begin(); it!=TrajToTrackMap.end(); ++it, track_index++) {
247  dEdxDiscrims[track_index] = DeDxData(-1, -2, 0 );
248 
249  const Track track = *it->val;
250  const Trajectory traj = *it->key;
251 
252  if(track.eta() <MinTrackEta || track.eta()>MaxTrackEta ){continue;}
253  if(track.p() <MinTrackMomentum || track.p() >MaxTrackMomentum){continue;}
254  if(track.found()<MinTrackHits ){continue;}
255 
256  std::vector<double> vect_probs;
257  vector<TrajectoryMeasurement> measurements = traj.measurements();
258 
259  unsigned int NClusterSaturating = 0;
260  for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
261 
262  TrajectoryStateOnSurface trajState = measurement_it->updatedState();
263  if( !trajState.isValid() ) continue;
264 
265  const TrackingRecHit* hit = (*measurement_it->recHit()).hit();
266  const SiStripRecHit2D* sistripsimplehit = dynamic_cast<const SiStripRecHit2D*>(hit);
267  const SiStripMatchedRecHit2D* sistripmatchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
268  const SiStripRecHit1D* sistripsimple1dhit = dynamic_cast<const SiStripRecHit1D*>(hit);
269 
270  double Prob;
271  if(sistripsimplehit){
272  Prob = GetProbability(DeDxTools::GetCluster(sistripsimplehit), trajState,sistripsimplehit->geographicalId());
273  if(shapetest && !(DeDxTools::shapeSelection(DeDxTools::GetCluster(sistripsimplehit)->amplitudes()))) Prob=-1.0;
274  if(Prob>=0) vect_probs.push_back(Prob);
275 
276  if(ClusterSaturatingStrip(DeDxTools::GetCluster(sistripsimplehit),sistripsimplehit->geographicalId())>0)NClusterSaturating++;
277 
278  }else if(sistripmatchedhit){
279  Prob = GetProbability(DeDxTools::GetCluster(sistripmatchedhit->monoHit()), trajState, sistripmatchedhit->monoId());
280  if(shapetest && !(DeDxTools::shapeSelection(DeDxTools::GetCluster(sistripmatchedhit->monoHit())->amplitudes()))) Prob=-1.0;
281  if(Prob>=0) vect_probs.push_back(Prob);
282 
283  Prob = GetProbability(DeDxTools::GetCluster(sistripmatchedhit->stereoHit()), trajState,sistripmatchedhit->stereoId());
284  if(Prob>=0) vect_probs.push_back(Prob);
285 
286  if(ClusterSaturatingStrip(DeDxTools::GetCluster(sistripmatchedhit->monoHit()),sistripmatchedhit->monoId()) >0)NClusterSaturating++;
287  if(ClusterSaturatingStrip(DeDxTools::GetCluster(sistripmatchedhit->stereoHit()),sistripmatchedhit->stereoId())>0)NClusterSaturating++;
288  }else if(sistripsimple1dhit){
289  Prob = GetProbability(DeDxTools::GetCluster(sistripsimple1dhit), trajState, sistripsimple1dhit->geographicalId());
290  if(shapetest && !(DeDxTools::shapeSelection(DeDxTools::GetCluster(sistripsimple1dhit)->amplitudes()))) Prob=-1.0;
291  if(Prob>=0) vect_probs.push_back(Prob);
292  if(ClusterSaturatingStrip(DeDxTools::GetCluster(sistripsimple1dhit),sistripsimple1dhit->geographicalId())>0)NClusterSaturating++;
293  }else{
294  }
295  }
296 
297  double estimator = ComputeDiscriminator(vect_probs);
298  int size = vect_probs.size();
299  float Error = -1;
300 
301  //WARNING: Since the dEdX Error is not properly computed for the moment
302  //It was decided to store the number of saturating cluster in that dataformat
303  Error = NClusterSaturating;
304  dEdxDiscrims[track_index] = DeDxData(estimator, Error, size );
305 
306 // printf("%i --> %g\n",size,estimator);
307  }
308 
309  filler.insert(trackCollectionHandle, dEdxDiscrims.begin(), dEdxDiscrims.end());
310  filler.fill();
311  iEvent.put(trackDeDxDiscrimAssociation);
312 }
313 
314 
316  const uint32_t & detId){
317  const vector<uint8_t>& ampls = cluster->amplitudes();
318 
319  stModInfo* MOD = NULL;
320  if(useCalibration)MOD = MODsColl[detId];
321 
322  int SaturatingStrip = 0;
323  for(unsigned int i=0;i<ampls.size();i++){
324  int StripCharge = ampls[i];
325  if(MOD){StripCharge = (int)(StripCharge / MOD->Gain);}
326  if(StripCharge>=254)SaturatingStrip++;
327  }
328  return SaturatingStrip;
329 }
330 
332  const uint32_t & detId)
333 {
334  // Get All needed variables
335  LocalVector trackDirection = trajState.localDirection();
336  double cosine = trackDirection.z()/trackDirection.mag();
337  const vector<uint8_t>& ampls = cluster->amplitudes();
338  // uint32_t detId = cluster->geographicalId();
339  // int firstStrip = cluster->firstStrip();
340  stModInfo* MOD = MODsColl[detId];
341 
342 
343  // Sanity Checks
344  if( ampls.size()>MaxNrStrips) {return -1;}
345 // if( DeDxDiscriminatorTools::IsSaturatingStrip (ampls)) {return -1;}
346 // if( DeDxDiscriminatorTools::IsSpanningOver2APV (firstStrip, ampls.size())) {return -1;}
347 // if(!DeDxDiscriminatorTools::IsFarFromBorder (trajState, m_tracker->idToDetUnit(DetId(detId)) )) {return -1;}
348 
349 
350  // Find Probability for this given Charge and Path
351  double charge = 0;
352  if(useCalibration){
353  for(unsigned int i=0;i<ampls.size();i++){
354  int CalibratedCharge = ampls[i];
355  CalibratedCharge = (int)(CalibratedCharge / MOD->Gain);
356  if(CalibratedCharge>=1024){
357  CalibratedCharge = 255;
358  }else if(CalibratedCharge>254){
359  CalibratedCharge = 254;
360  }
361  charge+=CalibratedCharge;
362  }
363  }else{
364  charge = DeDxDiscriminatorTools::charge(ampls);
365  }
366  double path = DeDxDiscriminatorTools::path(cosine,MOD->Thickness);
367 
368  int BinX = Prob_ChargePath->GetXaxis()->FindBin(trajState.localMomentum().mag());
369  int BinY = Prob_ChargePath->GetYaxis()->FindBin(path);
370  int BinZ = Prob_ChargePath->GetZaxis()->FindBin(charge/path);
371  return Prob_ChargePath->GetBinContent(BinX,BinY,BinZ);
372 }
373 
374 
375 double DeDxDiscriminatorProducer::ComputeDiscriminator(std::vector<double>& vect_probs)
376 {
377  double estimator = -1;
378  int size = vect_probs.size();
379  if(size<=0)return estimator;
380 
381  if(Formula==0){
382  double P = 1;
383  for(int i=0;i<size;i++){
384  if(vect_probs[i]<=0.0001){P *= pow(0.0001 , 1.0/size);}
385  else {P *= pow(vect_probs[i], 1.0/size);}
386  }
387  estimator = P;
388  }else if(Formula==1){
389  std::sort(vect_probs.begin(), vect_probs.end(), std::less<double>() );
390  for(int i=0;i<size;i++){if(vect_probs[i]<=0.0001)vect_probs[i] = 0.0001; }
391 
392  double SumJet = 0.;
393  for(int i=0;i<size;i++){ SumJet+= log(vect_probs[i]); }
394 
395  double Loginvlog=log(-SumJet);
396  double Prob =1.;
397  double lfact=1.;
398 
399  for(int l=1; l!=size; l++){
400  lfact*=l;
401  Prob+=exp(l*Loginvlog-log(1.*lfact));
402  }
403 
404  double LogProb=log(Prob);
405  double ProbJet=std::min(exp(std::max(LogProb+SumJet,-30.)),1.);
406  estimator = -log10(ProbJet)/4.;
407  estimator = 1-estimator;
408  }else if(Formula==2){
409  std::sort(vect_probs.begin(), vect_probs.end(), std::less<double>() );
410  double P = 1.0/(12*size);
411  for(int i=1;i<=size;i++){
412  P += pow(vect_probs[i-1] - ((2.0*i-1.0)/(2.0*size)),2);
413  }
414  P *= (3.0/size);
415  estimator = P;
416  }else{
417  std::sort(vect_probs.begin(), vect_probs.end(), std::less<double>() );
418  double P = 1.0/(12*size);
419  for(int i=1;i<=size;i++){
420  P += vect_probs[i-1] * pow(vect_probs[i-1] - ((2.0*i-1.0)/(2.0*size)),2);
421  }
422  P *= (3.0/size);
423  estimator = P;
424  }
425 
426  return estimator;
427 }
428 
429 
431  if(!useCalibration)return;
432 
433 
434  TChain* t1 = new TChain("SiStripCalib/APVGain");
435  t1->Add(m_calibrationPath.c_str());
436 
437  unsigned int tree_DetId;
438  unsigned char tree_APVId;
439  double tree_Gain;
440 
441  t1->SetBranchAddress("DetId" ,&tree_DetId );
442  t1->SetBranchAddress("APVId" ,&tree_APVId );
443  t1->SetBranchAddress("Gain" ,&tree_Gain );
444 
445  for (unsigned int ientry = 0; ientry < t1->GetEntries(); ientry++) {
446  t1->GetEntry(ientry);
447  stModInfo* MOD = MODsColl[tree_DetId];
448  MOD->Gain = tree_Gain;
449  }
450 
451  delete t1;
452 
453 }
454 
455 //define this as a plug-in
double path(double cosine, double thickness)
double p() const
momentum vector magnitude
Definition: TrackBase.h:127
T getParameter(std::string const &) const
virtual int nstrips() const =0
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
const SiStripCluster * GetCluster(const TrackerSingleRecHit *hit)
Definition: DeDxTools.h:27
virtual void produce(edm::Event &, const edm::EventSetup &) override
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
double GetProbability(const SiStripCluster *cluster, TrajectoryStateOnSurface trajState, const uint32_t &)
fixed size error matrix
Definition: Error.h:38
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
DeDxDiscriminatorProducer(const edm::ParameterSet &)
#define P
#define NULL
Definition: scimark2.h:8
virtual void beginRun(edm::Run const &run, const edm::EventSetup &) override
double charge(const std::vector< uint8_t > &Ampls)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
LocalVector localMomentum() const
DataContainer const & measurements() const
Definition: Trajectory.h:203
int iEvent
Definition: GenABIO.cc:230
T mag() const
Definition: PV3DBase.h:67
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:139
tuple path
else: Piece not in the list, fine.
const T & max(const T &a, const T &b)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
T z() const
Definition: PV3DBase.h:64
int j
Definition: DBlmapReader.cc:9
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
int k[5][pyjets_maxn]
DEFINE_FWK_MODULE(CosmicTrackingParticleSelector)
Definition: DetId.h:18
bool shapeSelection(const std::vector< uint8_t > &ampls)
Definition: DeDxTools.cc:133
size_type size() const
map size
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
string const
Definition: compareJSON.py:14
int ClusterSaturatingStrip(const SiStripCluster *cluster, const uint32_t &)
T const * product() const
Definition: Handle.h:81
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
unsigned short found() const
Number of valid hits on track.
Definition: Track.h:100
double ComputeDiscriminator(std::vector< double > &vect_probs)
const_iterator begin() const
first iterator over the map (read only)
tuple size
Write out results.
const std::vector< uint8_t > & amplitudes() const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: Run.h:41