CMS 3D CMS Logo

ShallowGainCalibration.cc
Go to the documentation of this file.
4 
5 using namespace edm;
6 using namespace reco;
7 using namespace std;
8 
10  : tracks_token_( consumes< edm::View<reco::Track> >(iConfig.getParameter<edm::InputTag>("Tracks")) ),
11  association_token_( consumes< TrajTrackAssociationCollection >(iConfig.getParameter<edm::InputTag>("Tracks")) ),
12  Suffix ( iConfig.getParameter<std::string>("Suffix") ),
13  Prefix ( iConfig.getParameter<std::string>("Prefix") )
14 {
15  produces <std::vector<int> > ( Prefix + "trackindex" + Suffix );
16  produces <std::vector<unsigned int> > ( Prefix + "rawid" + Suffix );
17  produces <std::vector<double> > ( Prefix + "localdirx" + Suffix );
18  produces <std::vector<double> > ( Prefix + "localdiry" + Suffix );
19  produces <std::vector<double> > ( Prefix + "localdirz" + Suffix );
20  produces <std::vector<unsigned short> > ( Prefix + "firststrip" + Suffix );
21  produces <std::vector<unsigned short> > ( Prefix + "nstrips" + Suffix );
22  produces <std::vector<bool> > ( Prefix + "saturation" + Suffix );
23  produces <std::vector<bool> > ( Prefix + "overlapping" + Suffix );
24  produces <std::vector<bool> > ( Prefix + "farfromedge" + Suffix );
25  produces <std::vector<unsigned int> > ( Prefix + "charge" + Suffix );
26  produces <std::vector<double> > ( Prefix + "path" + Suffix );
27  #ifdef ExtendedCALIBTree
28  produces <std::vector<double> > ( Prefix + "chargeoverpath" + Suffix );
29  #endif
30  produces <std::vector<unsigned char> > ( Prefix + "amplitude" + Suffix );
31  produces <std::vector<double> > ( Prefix + "gainused" + Suffix );
32  produces <std::vector<double> > ( Prefix + "gainusedTick" + Suffix );
33 }
34 
37  auto trackindex = std::make_unique<std::vector<int>> ();
38  auto rawid = std::make_unique<std::vector<unsigned int>> ();
39  auto localdirx = std::make_unique<std::vector<double>> ();
40  auto localdiry = std::make_unique<std::vector<double>> ();
41  auto localdirz = std::make_unique<std::vector<double>> ();
42  auto firststrip = std::make_unique<std::vector<unsigned short>> ();
43  auto nstrips = std::make_unique<std::vector<unsigned short>> ();
44  auto saturation = std::make_unique<std::vector<bool>> ();
45  auto overlapping = std::make_unique<std::vector<bool>> ();
46  auto farfromedge = std::make_unique<std::vector<bool>> ();
47  auto charge = std::make_unique<std::vector<unsigned int>> ();
48  auto path = std::make_unique<std::vector<double>> ();
49  #ifdef ExtendedCALIBTree
50  auto chargeoverpath= std::make_unique<std::vector<double>> ();
51  #endif
52  auto amplitude = std::make_unique<std::vector<unsigned char>> ();
53  auto gainused = std::make_unique<std::vector<double>> ();
54  auto gainusedTick = std::make_unique<std::vector<double>> ();
55 
56  edm::ESHandle<TrackerGeometry> theTrackerGeometry; iSetup.get<TrackerDigiGeometryRecord>().get( theTrackerGeometry );
57  m_tracker=&(* theTrackerGeometry );
58  edm::ESHandle<SiStripGain> gainHandle; iSetup.get<SiStripGainRcd>().get(gainHandle);
61 
62  for( TrajTrackAssociationCollection::const_iterator association = associations->begin(); association != associations->end(); association++) {
63  const Trajectory* traj = association->key.get();
64  const reco::Track* track = association->val.get();
65 
66  vector<TrajectoryMeasurement> measurements = traj->measurements();
67  for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
68  TrajectoryStateOnSurface trajState = measurement_it->updatedState();
69  if( !trajState.isValid() ) continue;
70 
71  const TrackingRecHit* hit = (*measurement_it->recHit()).hit();
72  const SiStripRecHit1D* sistripsimple1dhit = dynamic_cast<const SiStripRecHit1D*>(hit);
73  const SiStripRecHit2D* sistripsimplehit = dynamic_cast<const SiStripRecHit2D*>(hit);
74  const SiStripMatchedRecHit2D* sistripmatchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
75  const SiPixelRecHit* sipixelhit = dynamic_cast<const SiPixelRecHit*>(hit);
76 
77  const SiPixelCluster* PixelCluster = nullptr;
78  const SiStripCluster* StripCluster = nullptr;
79  uint32_t DetId = 0;
80 
81  for(unsigned int h=0;h<2;h++){
82  if(!sistripmatchedhit && h==1){
83  continue;
84  }else if(sistripmatchedhit && h==0){
85  StripCluster = &sistripmatchedhit->monoCluster();
86  DetId = sistripmatchedhit->monoId();
87  }else if(sistripmatchedhit && h==1){
88  StripCluster = &sistripmatchedhit->stereoCluster();;
89  DetId = sistripmatchedhit->stereoId();
90  }else if(sistripsimplehit){
91  StripCluster = (sistripsimplehit->cluster()).get();
92  DetId = sistripsimplehit->geographicalId().rawId();
93  }else if(sistripsimple1dhit){
94  StripCluster = (sistripsimple1dhit->cluster()).get();
95  DetId = sistripsimple1dhit->geographicalId().rawId();
96  }else if(sipixelhit){
97  PixelCluster = (sipixelhit->cluster()).get();
98  DetId = sipixelhit->geographicalId().rawId();
99  }else{
100  continue;
101  }
102 
103  LocalVector trackDirection = trajState.localDirection();
104  double cosine = trackDirection.z()/trackDirection.mag();
105  bool Saturation = false;
106  bool Overlapping = false;
107  unsigned int Charge = 0;
108  double Path = (10.0*thickness(DetId))/fabs(cosine);
109  double PrevGain = -1;
110  double PrevGainTick = -1;
111  int FirstStrip = 0;
112  int NStrips = 0;
113 
114  if(StripCluster){
115  const auto & Ampls = StripCluster->amplitudes();
116  FirstStrip = StripCluster->firstStrip();
117  NStrips = Ampls.size();
118  int APVId = FirstStrip/128;
119 
120 
121  if(gainHandle.isValid()){
122  PrevGain = gainHandle->getApvGain(APVId,gainHandle->getRange(DetId, 1),1);
123  PrevGainTick = gainHandle->getApvGain(APVId,gainHandle->getRange(DetId, 0),1);
124  }
125 
126  for(unsigned int a=0;a<Ampls.size();a++){
127  Charge+=Ampls[a];
128  if(Ampls[a] >=254)Saturation =true;
129  amplitude->push_back( Ampls[a] );
130  }
131 
132  if(FirstStrip==0 )Overlapping=true;
133  if(FirstStrip==128 )Overlapping=true;
134  if(FirstStrip==256 )Overlapping=true;
135  if(FirstStrip==384 )Overlapping=true;
136  if(FirstStrip==512 )Overlapping=true;
137  if(FirstStrip==640 )Overlapping=true;
138 
139  if(FirstStrip<=127 && FirstStrip+Ampls.size()>127)Overlapping=true;
140  if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlapping=true;
141  if(FirstStrip<=383 && FirstStrip+Ampls.size()>383)Overlapping=true;
142  if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlapping=true;
143  if(FirstStrip<=639 && FirstStrip+Ampls.size()>639)Overlapping=true;
144 
145  if(FirstStrip+Ampls.size()==127 )Overlapping=true;
146  if(FirstStrip+Ampls.size()==255 )Overlapping=true;
147  if(FirstStrip+Ampls.size()==383 )Overlapping=true;
148  if(FirstStrip+Ampls.size()==511 )Overlapping=true;
149  if(FirstStrip+Ampls.size()==639 )Overlapping=true;
150  if(FirstStrip+Ampls.size()==767 )Overlapping=true;
151  }else if(PixelCluster){
152  const auto& Ampls = PixelCluster->pixelADC();
153  int FirstRow = PixelCluster->minPixelRow();
154  int FirstCol = PixelCluster->minPixelCol();
155  FirstStrip = ((FirstRow/80)<<3 | (FirstCol/52)) * 128; //Hack to save the APVId
156  NStrips = 0;
157  Saturation = false;
158  Overlapping = false;
159 
160  for(unsigned int a=0;a<Ampls.size();a++){
161  Charge+=Ampls[a];
162  if(Ampls[a] >=254)Saturation =true;
163  }
164  }
165  #ifdef ExtendedCALIBTree
166  double ChargeOverPath = (double)Charge / Path ;
167  #endif
168 
169  trackindex ->push_back( shallow::findTrackIndex(tracks, track) );
170  rawid ->push_back( DetId );
171  localdirx ->push_back( trackDirection.x() );
172  localdiry ->push_back( trackDirection.y() );
173  localdirz ->push_back( trackDirection.z() );
174  firststrip ->push_back( FirstStrip );
175  nstrips ->push_back( NStrips );
176  saturation ->push_back( Saturation );
177  overlapping ->push_back( Overlapping );
178  farfromedge ->push_back( StripCluster ? IsFarFromBorder(&trajState,DetId, &iSetup) : true );
179  charge ->push_back( Charge );
180  path ->push_back( Path );
181  #ifdef ExtendedCALIBTree
182  chargeoverpath->push_back( ChargeOverPath );
183  #endif
184  gainused ->push_back( PrevGain );
185  gainusedTick ->push_back( PrevGainTick );
186  }
187  }
188  }
189 
190  iEvent.put(std::move(trackindex), Prefix + "trackindex" + Suffix );
191  iEvent.put(std::move(rawid ), Prefix + "rawid" + Suffix );
192  iEvent.put(std::move(localdirx ), Prefix + "localdirx" + Suffix );
193  iEvent.put(std::move(localdiry ), Prefix + "localdiry" + Suffix );
194  iEvent.put(std::move(localdirz ), Prefix + "localdirz" + Suffix );
195  iEvent.put(std::move(firststrip), Prefix + "firststrip" + Suffix );
196  iEvent.put(std::move(nstrips), Prefix + "nstrips" + Suffix );
197  iEvent.put(std::move(saturation), Prefix + "saturation" + Suffix );
198  iEvent.put(std::move(overlapping), Prefix + "overlapping" + Suffix );
199  iEvent.put(std::move(farfromedge), Prefix + "farfromedge" + Suffix );
200  iEvent.put(std::move(charge), Prefix + "charge" + Suffix );
201  iEvent.put(std::move(path), Prefix + "path" + Suffix );
202  #ifdef ExtendedCALIBTree
203  iEvent.put(std::move(chargeoverpath),Prefix + "chargeoverpath"+ Suffix );
204  #endif
205  iEvent.put(std::move(amplitude), Prefix + "amplitude" + Suffix );
206  iEvent.put(std::move(gainused), Prefix + "gainused" + Suffix );
207  iEvent.put(std::move(gainusedTick), Prefix + "gainusedTick" + Suffix );
208 }
209 
210 /*
211 void ShallowGainCalibration::beginJob(const edm::EventSetup& iSetup)
212 {
213  printf("Befin JOB\n");
214 
215  edm::ESHandle<TrackerGeometry> tkGeom;
216  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
217  vector<GeomDet*> Det = tkGeom->dets();
218 
219  edm::ESHandle<SiStripGain> gainHandle;
220  iSetup.get<SiStripGainRcd>().get(gainHandle);
221  if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
222 
223  for(unsigned int i=0;i<Det.size();i++){
224  DetId Detid = Det[i]->geographicalId();
225  int SubDet = Detid.subdetId();
226 
227  if( SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID ||
228  SubDet == StripSubdetector::TOB || SubDet == StripSubdetector::TEC ){
229 
230  StripGeomDetUnit* DetUnit = dynamic_cast<StripGeomDetUnit*> (Det[i]);
231  if(!DetUnit)continue;
232 
233  const StripTopology& Topo = DetUnit->specificTopology();
234  unsigned int NAPV = Topo.nstrips()/128;
235 
236  for(unsigned int j=0;j<NAPV;j++){
237  stAPVGain* APV = new stAPVGain;
238  APV->DetId = Detid.rawId();
239  APV->APVId = j;
240  APV->PreviousGain = 1;
241 
242  APVsCollOrdered.push_back(APV);
243  APVsColl[(APV->DetId<<3) | APV->APVId] = APV;
244  }
245  }
246  }
247 }
248 
249 
250 void ShallowGainCalibration::beginRun(edm::Run &, const edm::EventSetup &iSetup){
251  printf("BEFIN RUN\n");
252 
253  edm::ESHandle<SiStripGain> gainHandle;
254  iSetup.get<SiStripGainRcd>().get(gainHandle);
255  if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
256 
257  for(std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin();it!=APVsCollOrdered.end();it++){
258  stAPVGain* APV = *it;
259  SiStripApvGain::Range detGainRange = gainHandle->getRange(APV->DetId);
260  APV->PreviousGain = *(detGainRange.first + APV->APVId);
261  }
262 }
263 */
264 
265 bool ShallowGainCalibration::IsFarFromBorder(TrajectoryStateOnSurface* trajState, const uint32_t detid, const edm::EventSetup* iSetup)
266 {
267  edm::ESHandle<TrackerGeometry> tkGeom; iSetup->get<TrackerDigiGeometryRecord>().get( tkGeom );
268 
269  LocalPoint HitLocalPos = trajState->localPosition();
270  LocalError HitLocalError = trajState->localError().positionError() ;
271 
272  const GeomDetUnit* it = tkGeom->idToDetUnit(DetId(detid));
273  if (dynamic_cast<const StripGeomDetUnit*>(it)==nullptr && dynamic_cast<const PixelGeomDetUnit*>(it)==nullptr) {
274  std::cout << "this detID doesn't seem to belong to the Tracker" << std::endl;
275  return false;
276  }
277 
278  const BoundPlane plane = it->surface();
279  const TrapezoidalPlaneBounds* trapezoidalBounds( dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
280  const RectangularPlaneBounds* rectangularBounds( dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
281 
282  double DistFromBorder = 1.0;
283  double HalfLength = it->surface().bounds().length() /2.0;
284 
285  if(trapezoidalBounds)
286  {
287  std::array<const float, 4> const & parameters = (*trapezoidalBounds).parameters();
288  HalfLength = parameters[3];
289  }else if(rectangularBounds){
290  HalfLength = it->surface().bounds().length() /2.0;
291  }else{return false;}
292 
293  if (fabs(HitLocalPos.y())+HitLocalError.yy() >= (HalfLength - DistFromBorder) ) return false;
294 
295  return true;
296 }
297 
298 
300 {
301  map<DetId,double>::iterator th=m_thicknessMap.find(id);
302  if(th!=m_thicknessMap.end())
303  return (*th).second;
304  else {
305  double detThickness=1.;
306  //compute thickness normalization
307  const GeomDetUnit* it = m_tracker->idToDetUnit(DetId(id));
308  bool isPixel = dynamic_cast<const PixelGeomDetUnit*>(it)!=nullptr;
309  bool isStrip = dynamic_cast<const StripGeomDetUnit*>(it)!=nullptr;
310  if (!isPixel && ! isStrip) {
311  //FIXME throw exception
312  edm::LogWarning("DeDxHitsProducer") << "\t\t this detID doesn't seem to belong to the Tracker";
313  detThickness = 1.;
314  }else{
315  detThickness = it->surface().bounds().thickness();
316  }
317 
318  m_thicknessMap[id]=detThickness;//computed value
319  return detThickness;
320  }
321 
322 }
323 
ClusterRef cluster() const
virtual float length() const =0
int minPixelCol() const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
unsigned int stereoId() const
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
const_iterator end() const
last iterator over the map (read only)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
int findTrackIndex(const edm::Handle< edm::View< reco::Track > > &h, const reco::Track *t)
Definition: ShallowTools.cc:28
SiStripCluster const & monoCluster() const
LocalVector localDirection() const
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:47
std::map< DetId, double > m_thicknessMap
T y() const
Definition: PV3DBase.h:63
const Bounds & bounds() const
Definition: Surface.h:120
uint16_t firstStrip() const
ShallowGainCalibration(const edm::ParameterSet &)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:42
LocalError positionError() const
bool IsFarFromBorder(TrajectoryStateOnSurface *trajState, const uint32_t detid, const edm::EventSetup *iSetup)
static float getApvGain(const uint16_t &apv, const SiStripApvGain::Range &range)
Definition: SiStripGain.h:73
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
int iEvent
Definition: GenABIO.cc:230
T mag() const
Definition: PV3DBase.h:67
int minPixelRow() const
float yy() const
Definition: LocalError.h:26
Definition: Path.h:44
T z() const
Definition: PV3DBase.h:64
ClusterRef cluster() const
const LocalTrajectoryError & localError() const
const edm::EDGetTokenT< edm::View< reco::Track > > tracks_token_
const std::vector< uint16_t > & pixelADC() const
Definition: DetId.h:18
const TrackerGeometry * m_tracker
ClusterRef cluster() const
Definition: SiPixelRecHit.h:49
SiStripCluster const & stereoCluster() const
const edm::EDGetTokenT< TrajTrackAssociationCollection > association_token_
Pixel cluster – collection of neighboring pixels above threshold.
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
T get() const
Definition: EventSetup.h:62
bool isPixel(HitType hitType)
unsigned int monoId() const
const_iterator begin() const
first iterator over the map (read only)
DetId geographicalId() const
void produce(edm::Event &, const edm::EventSetup &) override
bool isValid() const
Definition: ESHandle.h:47
T x() const
Definition: PV3DBase.h:62
const std::vector< uint8_t > & amplitudes() const
def move(src, dest)
Definition: eostools.py:511
Our base class.
Definition: SiPixelRecHit.h:23
const SiStripApvGain::Range getRange(uint32_t detID) const
Definition: SiStripGain.h:70