CMS 3D CMS Logo

DeDxDiscriminatorLearner.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: DeDxDiscriminatorLearner
4 // Class: DeDxDiscriminatorLearner
5 //
13 //
14 // Original Author: Loic Quertenmont(querten)
15 // Created: Mon October 20 10:09:02 CEST 2008
16 //
17 
18 
19 // system include files
20 #include <memory>
21 
23 
26 
31 
32 using namespace reco;
33 using namespace std;
34 using namespace edm;
35 
37 {
38  m_tracksTag = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("tracks"));
39  m_trajTrackAssociationTag = consumes<TrajTrackAssociationCollection>(iConfig.getParameter<edm::InputTag>("trajectoryTrackAssociation"));
40 
41  P_Min = iConfig.getParameter<double> ("P_Min" );
42  P_Max = iConfig.getParameter<double> ("P_Max" );
43  P_NBins = iConfig.getParameter<int> ("P_NBins");
44  Path_Min = iConfig.getParameter<double> ("Path_Min" );
45  Path_Max = iConfig.getParameter<double> ("Path_Max" );
46  Path_NBins = iConfig.getParameter<int> ("Path_NBins");
47  Charge_Min = iConfig.getParameter<double> ("Charge_Min" );
48  Charge_Max = iConfig.getParameter<double> ("Charge_Max" );
49  Charge_NBins = iConfig.getParameter<int> ("Charge_NBins");
50 
51  MinTrackMomentum = iConfig.getUntrackedParameter<double> ("minTrackMomentum" , 5.0);
52  MaxTrackMomentum = iConfig.getUntrackedParameter<double> ("maxTrackMomentum" , 99999.0);
53  MinTrackEta = iConfig.getUntrackedParameter<double> ("minTrackEta" , -5.0);
54  MaxTrackEta = iConfig.getUntrackedParameter<double> ("maxTrackEta" , 5.0);
55  MaxNrStrips = iConfig.getUntrackedParameter<unsigned>("maxNrStrips" , 255);
56  MinTrackHits = iConfig.getUntrackedParameter<unsigned>("MinTrackHits" , 4);
57 
58  algoMode = iConfig.getUntrackedParameter<string> ("AlgoMode" , "SingleJob");
59  HistoFile = iConfig.getUntrackedParameter<string> ("HistoFile" , "out.root");
60  VInputFiles = iConfig.getUntrackedParameter<vector<string> > ("InputFiles");
61 
62  shapetest = iConfig.getParameter<bool>("ShapeTest");
63  useCalibration = iConfig.getUntrackedParameter<bool>("UseCalibration");
64  m_calibrationPath = iConfig.getUntrackedParameter<string>("calibrationPath");
65 }
66 
67 
69 
70 // ------------ method called once each job just before starting event loop ------------
71 
73 {
74  Charge_Vs_Path = new TH3F ("Charge_Vs_Path" , "Charge_Vs_Path" , P_NBins, P_Min, P_Max, Path_NBins, Path_Min, Path_Max, Charge_NBins, Charge_Min, Charge_Max);
75 
76  if(useCalibration && calibGains.empty()){
78  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
79  m_off = tkGeom->offsetDU(GeomDetEnumerators::PixelBarrel); //index start at the first pixel
80 
82  }
83 
84  //Read the calibTree if in calibTree mode
85  if(strcmp(algoMode.c_str(),"CalibTree")==0)algoAnalyzeTheTree(iSetup);
86 }
87 
88 // ------------ method called once each job just after ending the event loop ------------
89 
91 {
92  if( strcmp(algoMode.c_str(),"MultiJob")==0){
93  TFile* Output = new TFile(HistoFile.c_str(), "RECREATE");
94  Charge_Vs_Path->Write();
95  Output->Write();
96  Output->Close();
97  }else if( strcmp(algoMode.c_str(),"WriteOnDB")==0){
98  TFile* Input = new TFile(HistoFile.c_str() );
99  Charge_Vs_Path = (TH3F*)(Input->FindObjectAny("Charge_Vs_Path"))->Clone();
100  Input->Close();
101  }else if(strcmp(algoMode.c_str(),"CalibTree")==0){
102  TFile* Output = new TFile(HistoFile.c_str(), "RECREATE");
103  Charge_Vs_Path->Write();
104  Output->Write();
105  Output->Close();
106  TFile* Input = new TFile(HistoFile.c_str() );
107  Charge_Vs_Path = (TH3F*)(Input->FindObjectAny("Charge_Vs_Path"))->Clone();
108  Input->Close();
109  }
110 }
111 
113 {
114  Handle<TrajTrackAssociationCollection> trajTrackAssociationHandle;
115  iEvent.getByToken(m_trajTrackAssociationTag, trajTrackAssociationHandle);
116 
117  edm::Handle<reco::TrackCollection> trackCollectionHandle;
118  iEvent.getByToken(m_tracksTag,trackCollectionHandle);
119 
120  unsigned track_index = 0;
121  for(TrajTrackAssociationCollection::const_iterator it = trajTrackAssociationHandle->begin(); it!=trajTrackAssociationHandle->end(); ++it, track_index++) {
122  const Track& track = *it->val;
123  const Trajectory& traj = *it->key;
124 
125  if(track.eta() <MinTrackEta || track.eta()>MaxTrackEta ){continue;}
126  if(track.pt() <MinTrackMomentum || track.pt() >MaxTrackMomentum){continue;}
127  if(track.found()<MinTrackHits ){continue;}
128 
129  const vector<TrajectoryMeasurement> & measurements = traj.measurements();
130  for(vector<TrajectoryMeasurement>::const_iterator it = measurements.begin(); it!=measurements.end(); it++){
131  TrajectoryStateOnSurface trajState=it->updatedState();
132  if( !trajState.isValid()) continue;
133 
134 
135  const TrackingRecHit * recHit=(*it->recHit()).hit();
136  if(!recHit || !recHit->isValid())continue;
137  LocalVector trackDirection = trajState.localDirection();
138  float cosine = trackDirection.z()/trackDirection.mag();
139 
140  processHit(recHit, trajState.localMomentum().mag(), cosine, trajState);
141  }
142  }
143 
144 }
145 
146 
148  auto const & thit = static_cast<BaseTrackerRecHit const&>(*recHit);
149  if(!thit.isValid())return;
150 
151  auto const & clus = thit.firstClusterRef();
152  if(!clus.isValid())return;
153 
154  int NSaturating = 0;
155  if(clus.isPixel()){
156  return;
157  }else if(clus.isStrip() && !thit.isMatched()){
158  auto& detUnit = *(recHit->detUnit());
159  auto& cluster = clus.stripCluster();
160  if( cluster.amplitudes().size()>MaxNrStrips) { return; }
161  if( DeDxTools::IsSpanningOver2APV (cluster.firstStrip(), cluster.amplitudes().size())) { return; }
162  if(!DeDxTools::IsFarFromBorder (trajState, &detUnit )) { return; }
163  float pathLen = 10.0*detUnit.surface().bounds().thickness()/fabs(cosine);
164  float chargeAbs = DeDxTools::getCharge(&cluster,NSaturating, detUnit, calibGains, m_off);
165  float charge = chargeAbs/pathLen;
166  if(!shapetest || (shapetest && DeDxTools::shapeSelection(cluster))){
167  Charge_Vs_Path->Fill(trackMomentum, pathLen, charge);
168  }
169  }else if(clus.isStrip() && thit.isMatched()){
170  const SiStripMatchedRecHit2D* matchedHit=dynamic_cast<const SiStripMatchedRecHit2D*>(recHit);
171  if(!matchedHit)return;
172  const GluedGeomDet* gdet = static_cast<const GluedGeomDet*>(matchedHit->det());
173 
174  auto& detUnitM = *(gdet->monoDet());
175  auto& clusterM = matchedHit->monoCluster();
176  if( clusterM.amplitudes().size()>MaxNrStrips) { return; }
177  if( DeDxTools::IsSpanningOver2APV (clusterM.firstStrip(), clusterM.amplitudes().size())) { return; }
178  if(!DeDxTools::IsFarFromBorder (trajState, &detUnitM )) { return; }
179  float pathLen = 10.0*detUnitM.surface().bounds().thickness()/fabs(cosine);
180  float chargeAbs = DeDxTools::getCharge(&clusterM,NSaturating, detUnitM, calibGains, m_off);
181  float charge = chargeAbs/pathLen;
182  if(!shapetest || (shapetest && DeDxTools::shapeSelection(clusterM))){
183  Charge_Vs_Path->Fill(trackMomentum, pathLen, charge);
184  }
185 
186  auto& detUnitS = *(gdet->stereoDet());
187  auto& clusterS = matchedHit->stereoCluster();
188  if( clusterS.amplitudes().size()>MaxNrStrips) { return; }
189  if( DeDxTools::IsSpanningOver2APV (clusterS.firstStrip(), clusterS.amplitudes().size())) { return; }
190  if(!DeDxTools::IsFarFromBorder (trajState, &detUnitS )) { return; }
191  pathLen = 10.0*detUnitS.surface().bounds().thickness()/fabs(cosine);
192  chargeAbs = DeDxTools::getCharge(&clusterS,NSaturating, detUnitS, calibGains, m_off);
193  charge = chargeAbs/pathLen;
194  if(!shapetest || (shapetest && DeDxTools::shapeSelection(clusterS))){
195  Charge_Vs_Path->Fill(trackMomentum, pathLen, charge);
196  }
197  }
198 }
199 
200 
201 //this function is only used when we run over a calibTree instead of running over EDM files
203 {
205  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
206 
207  unsigned int NEvent = 0;
208  for(unsigned int i=0;i<VInputFiles.size();i++){
209  printf("Openning file %3i/%3i --> %s\n",i+1, (int)VInputFiles.size(), (char*)(VInputFiles[i].c_str())); fflush(stdout);
210  TChain* tree = new TChain("gainCalibrationTree/tree");
211  tree->Add(VInputFiles[i].c_str());
212 
213  TString EventPrefix("");
214  TString EventSuffix("");
215 
216  TString TrackPrefix("track");
217  TString TrackSuffix("");
218 
219  TString CalibPrefix("GainCalibration");
220  TString CalibSuffix("");
221 
222  unsigned int eventnumber = 0; tree->SetBranchAddress(EventPrefix + "event" + EventSuffix, &eventnumber , nullptr);
223  unsigned int runnumber = 0; tree->SetBranchAddress(EventPrefix + "run" + EventSuffix, &runnumber , nullptr);
224  std::vector<bool>* TrigTech = nullptr; tree->SetBranchAddress(EventPrefix + "TrigTech" + EventSuffix, &TrigTech , nullptr);
225 
226  std::vector<double>* trackchi2ndof = nullptr; tree->SetBranchAddress(TrackPrefix + "chi2ndof" + TrackSuffix, &trackchi2ndof , nullptr);
227  std::vector<float>* trackp = nullptr; tree->SetBranchAddress(TrackPrefix + "momentum" + TrackSuffix, &trackp , nullptr);
228  std::vector<float>* trackpt = nullptr; tree->SetBranchAddress(TrackPrefix + "pt" + TrackSuffix, &trackpt , nullptr);
229  std::vector<double>* tracketa = nullptr; tree->SetBranchAddress(TrackPrefix + "eta" + TrackSuffix, &tracketa , nullptr);
230  std::vector<double>* trackphi = nullptr; tree->SetBranchAddress(TrackPrefix + "phi" + TrackSuffix, &trackphi , nullptr);
231  std::vector<unsigned int>* trackhitsvalid = nullptr; tree->SetBranchAddress(TrackPrefix + "hitsvalid" + TrackSuffix, &trackhitsvalid, nullptr);
232 
233  std::vector<int>* trackindex = nullptr; tree->SetBranchAddress(CalibPrefix + "trackindex" + CalibSuffix, &trackindex , nullptr);
234  std::vector<unsigned int>* rawid = nullptr; tree->SetBranchAddress(CalibPrefix + "rawid" + CalibSuffix, &rawid , nullptr);
235  std::vector<unsigned short>* firststrip = nullptr; tree->SetBranchAddress(CalibPrefix + "firststrip" + CalibSuffix, &firststrip , nullptr);
236  std::vector<unsigned short>* nstrips = nullptr; tree->SetBranchAddress(CalibPrefix + "nstrips" + CalibSuffix, &nstrips , nullptr);
237  std::vector<unsigned int>* charge = nullptr; tree->SetBranchAddress(CalibPrefix + "charge" + CalibSuffix, &charge , nullptr);
238  std::vector<float>* path = nullptr; tree->SetBranchAddress(CalibPrefix + "path" + CalibSuffix, &path , nullptr);
239  std::vector<unsigned char>* amplitude = nullptr; tree->SetBranchAddress(CalibPrefix + "amplitude" + CalibSuffix, &amplitude , nullptr);
240  std::vector<double>* gainused = nullptr; tree->SetBranchAddress(CalibPrefix + "gainused" + CalibSuffix, &gainused , nullptr);
241 
242  printf("Number of Events = %i + %i = %i\n",NEvent,(unsigned int)tree->GetEntries(),(unsigned int)(NEvent+tree->GetEntries()));NEvent+=tree->GetEntries();
243  printf("Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
244  printf("Looping on the Tree :");
245  int TreeStep = tree->GetEntries()/50;if(TreeStep<=1)TreeStep=1;
246  for (unsigned int ientry = 0; ientry < tree->GetEntries(); ientry++) {
247  if(ientry%TreeStep==0){printf(".");fflush(stdout);}
248  tree->GetEntry(ientry);
249 
250  int FirstAmplitude = 0;
251  for(unsigned int c=0;c<(*path).size();c++){
252  FirstAmplitude+=(*nstrips)[c];
253  int t = (*trackindex)[c];
254  if((*trackpt)[t]<5)continue;
255  if((*trackhitsvalid)[t]<5)continue;
256 
257  int Charge = 0;
258  if(useCalibration){
259  auto & gains = calibGains[tkGeom->idToDetUnit(DetId((*rawid)[c]))->index()-m_off];
260  auto & gain = gains[(*firststrip)[c]/128];
261  for(unsigned int s=0;s<(*nstrips)[c];s++){
262  int StripCharge = (*amplitude)[FirstAmplitude-(*nstrips)[c]+s];
263  if(StripCharge<254){
264  StripCharge=(int)(StripCharge/gain);
265  if(StripCharge>=1024){
266  StripCharge = 255;
267  }else if(StripCharge>=254){
268  StripCharge = 254;
269  }
270  }
271  Charge += StripCharge;
272  }
273  }else{
274  Charge = (*charge)[c];
275  }
276 
277 // printf("ChargeDifference = %i Vs %i with Gain = %f\n",(*charge)[c],Charge,Gains[(*rawid)[c]]);
278  double ClusterChargeOverPath = ( (double) Charge )/(*path)[c] ;
279  Charge_Vs_Path->Fill((*trackp)[t],(*path)[c],ClusterChargeOverPath);
280  }
281  }printf("\n");
282  }
283 }
284 
285 
287 {
290  Charge_Vs_Path->GetNbinsX(), Charge_Vs_Path->GetXaxis()->GetXmin(), Charge_Vs_Path->GetXaxis()->GetXmax(),
291  Charge_Vs_Path->GetNbinsY(), Charge_Vs_Path->GetYaxis()->GetXmin(), Charge_Vs_Path->GetYaxis()->GetXmax(),
292  Charge_Vs_Path->GetNbinsZ(), Charge_Vs_Path->GetZaxis()->GetXmin(), Charge_Vs_Path->GetZaxis()->GetXmax());
293 
294  for(int ix=0; ix<=Charge_Vs_Path->GetNbinsX()+1; ix++){
295  for(int iy=0; iy<=Charge_Vs_Path->GetNbinsY()+1; iy++){
296  for(int iz=0; iz<=Charge_Vs_Path->GetNbinsZ()+1; iz++){
297  obj->setBinContent(ix, iy, iz, Charge_Vs_Path->GetBinContent(ix,iy, iz) );
298 // if(Charge_Vs_Path->GetBinContent(ix,iy)!=0)printf("%i %i %i --> %f\n",ix,iy, iz, Charge_Vs_Path->GetBinContent(ix,iy,iz));
299  }
300  }
301  }
302 
303  return obj;
304 }
305 
306 
307 //define this as a plug-in
bool IsFarFromBorder(const TrajectoryStateOnSurface &trajState, const GeomDetUnit *it)
Definition: DeDxTools.cc:311
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int getCharge(const SiStripCluster *cluster, int &nSatStrip, const GeomDetUnit &detUnit, const std::vector< std::vector< float > > &calibGains, const unsigned int &m_off)
Definition: DeDxTools.cc:161
void algoAnalyze(const edm::Event &, const edm::EventSetup &) override
const GeomDetUnit * monoDet() const
Definition: GluedGeomDet.h:20
const_iterator end() const
last iterator over the map (read only)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
SiStripCluster const & monoCluster() const
LocalVector localDirection() const
#define Input(cl)
Definition: vmac.h:190
void makeCalibrationMap(const std::string &m_calibrationPath, const TrackerGeometry &tkGeom, std::vector< std::vector< float > > &calibGains, const unsigned int &m_off)
Definition: DeDxTools.cc:196
PhysicsTools::Calibration::HistogramD3D * getNewObject() override
LocalVector localMomentum() const
const TrackerGeomDet * idToDetUnit(DetId) const override
Return the pointer to the GeomDetUnit corresponding to a given DetId.
DataContainer const & measurements() const
Definition: Trajectory.h:196
SiStripCluster const & stripCluster() const
int iEvent
Definition: GenABIO.cc:230
T mag() const
Definition: PV3DBase.h:67
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:651
Histogram3D< double > HistogramD3D
Definition: Histogram3D.h:187
void algoAnalyzeTheTree(const edm::EventSetup &iSetup)
const GeomDet * det() const
bool shapeSelection(const SiStripCluster &ampls)
Definition: DeDxTools.cc:10
unsigned int offsetDU(SubDetector sid) const
double pt() const
track transverse momentum
Definition: TrackBase.h:621
T z() const
Definition: PV3DBase.h:64
void processHit(const TrackingRecHit *recHit, float trackMomentum, float &cosine, const TrajectoryStateOnSurface &trajState)
int index() const
Definition: GeomDet.h:99
std::vector< std::string > VInputFiles
Definition: DetId.h:18
void algoBeginJob(const edm::EventSetup &) override
bool isValid() const
SiStripCluster const & stereoCluster() const
fixed size matrix
HLT enums.
virtual const GeomDetUnit * detUnit() const
#define Output(cl)
Definition: vmac.h:194
unsigned short found() const
Number of valid hits on track.
Definition: Track.h:194
T get() const
Definition: EventSetup.h:63
virtual OmniClusterRef const & firstClusterRef() const =0
edm::EDGetTokenT< TrajTrackAssociationCollection > m_trajTrackAssociationTag
const_iterator begin() const
first iterator over the map (read only)
Definition: tree.py:1
bool IsSpanningOver2APV(unsigned int FirstStrip, unsigned int ClusterSize)
Definition: DeDxTools.cc:286
edm::EDGetTokenT< reco::TrackCollection > m_tracksTag
const GeomDetUnit * stereoDet() const
Definition: GluedGeomDet.h:21
std::vector< std::vector< float > > calibGains
void setBinContent(int bin, Value_t value)
DeDxDiscriminatorLearner(const edm::ParameterSet &)