CMS 3D CMS Logo

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