CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ShallowGainCalibration.cc
Go to the documentation of this file.
4 
5 using namespace edm;
6 using namespace reco;
7 using namespace std;
8 
9 
10 
11 
13  : theTracksLabel( iConfig.getParameter<edm::InputTag>("Tracks") ),
14  Suffix ( iConfig.getParameter<std::string>("Suffix") ),
15  Prefix ( iConfig.getParameter<std::string>("Prefix") )
16 {
17  produces <std::vector<int> > ( Prefix + "trackindex" + Suffix );
18  produces <std::vector<unsigned int> > ( Prefix + "rawid" + Suffix );
19  produces <std::vector<float> > ( Prefix + "localdirx" + Suffix );
20  produces <std::vector<float> > ( Prefix + "localdiry" + Suffix );
21  produces <std::vector<float> > ( Prefix + "localdirz" + Suffix );
22  produces <std::vector<unsigned short> > ( Prefix + "firststrip" + Suffix );
23  produces <std::vector<unsigned short> > ( Prefix + "nstrips" + Suffix );
24  produces <std::vector<bool> > ( Prefix + "saturation" + Suffix );
25  produces <std::vector<bool> > ( Prefix + "overlapping" + Suffix );
26  produces <std::vector<bool> > ( Prefix + "farfromedge" + Suffix );
27  produces <std::vector<unsigned int> > ( Prefix + "charge" + Suffix );
28  produces <std::vector<float> > ( Prefix + "path" + Suffix );
29  produces <std::vector<float> > ( Prefix + "chargeoverpath" + Suffix );
30  produces <std::vector<unsigned char> > ( Prefix + "amplitude" + Suffix );
31  produces <std::vector<double> > ( Prefix + "gainused" + Suffix );
32 }
33 
36  std::auto_ptr<std::vector<int> > trackindex ( new std::vector<int> );
37  std::auto_ptr<std::vector<unsigned int> > rawid ( new std::vector<unsigned int> );
38  std::auto_ptr<std::vector<float> > localdirx ( new std::vector<float> );
39  std::auto_ptr<std::vector<float> > localdiry ( new std::vector<float> );
40  std::auto_ptr<std::vector<float> > localdirz ( new std::vector<float> );
41  std::auto_ptr<std::vector<unsigned short> > firststrip ( new std::vector<unsigned short> );
42  std::auto_ptr<std::vector<unsigned short> > nstrips ( new std::vector<unsigned short> );
43  std::auto_ptr<std::vector<bool> > saturation ( new std::vector<bool> );
44  std::auto_ptr<std::vector<bool> > overlapping ( new std::vector<bool> );
45  std::auto_ptr<std::vector<bool> > farfromedge ( new std::vector<bool> );
46  std::auto_ptr<std::vector<unsigned int> > charge ( new std::vector<unsigned int> );
47  std::auto_ptr<std::vector<float> > path ( new std::vector<float> );
48  std::auto_ptr<std::vector<float> > chargeoverpath( new std::vector<float> );
49  std::auto_ptr<std::vector<unsigned char> > amplitude ( new std::vector<unsigned char> );
50  std::auto_ptr<std::vector<double> > gainused ( new std::vector<double> );
51 
52  edm::ESHandle<TrackerGeometry> theTrackerGeometry; iSetup.get<TrackerDigiGeometryRecord>().get( theTrackerGeometry );
53  m_tracker=&(* theTrackerGeometry );
54  edm::ESHandle<SiStripGain> gainHandle; iSetup.get<SiStripGainRcd>().get(gainHandle);
56  edm::Handle<TrajTrackAssociationCollection> associations; iEvent.getByLabel(theTracksLabel, associations);
57 
58  for( TrajTrackAssociationCollection::const_iterator association = associations->begin(); association != associations->end(); association++) {
59  const Trajectory* traj = association->key.get();
60  const reco::Track* track = association->val.get();
61 
62  vector<TrajectoryMeasurement> measurements = traj->measurements();
63  for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
64  TrajectoryStateOnSurface trajState = measurement_it->updatedState();
65  if( !trajState.isValid() ) continue;
66 
67  const TrackingRecHit* hit = (*measurement_it->recHit()).hit();
68  const SiStripRecHit1D* sistripsimple1dhit = dynamic_cast<const SiStripRecHit1D*>(hit);
69  const SiStripRecHit2D* sistripsimplehit = dynamic_cast<const SiStripRecHit2D*>(hit);
70  const SiStripMatchedRecHit2D* sistripmatchedhit = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
71 
72  const SiStripCluster* Cluster = NULL;
73  uint32_t DetId = 0;
74 
75  for(unsigned int h=0;h<2;h++){
76  if(!sistripmatchedhit && h==1){
77  continue;
78  }else if(sistripmatchedhit && h==0){
79  Cluster = &sistripmatchedhit->monoCluster();
80  DetId = sistripmatchedhit->monoId();
81  }else if(sistripmatchedhit && h==1){
82  Cluster = &sistripmatchedhit->stereoCluster();;
83  DetId = sistripmatchedhit->stereoId();
84  }else if(sistripsimplehit){
85  Cluster = (sistripsimplehit->cluster()).get();
86  DetId = sistripsimplehit->geographicalId().rawId();
87  }else if(sistripsimple1dhit){
88  Cluster = (sistripsimple1dhit->cluster()).get();
89  DetId = sistripsimple1dhit->geographicalId().rawId();
90  }else{
91  continue;
92  }
93 
94  LocalVector trackDirection = trajState.localDirection();
95  double cosine = trackDirection.z()/trackDirection.mag();
96  const auto & Ampls = Cluster->amplitudes();
97  int FirstStrip = Cluster->firstStrip();
98  int APVId = FirstStrip/128;
99  bool Saturation = false;
100  bool Overlapping = false;
101  unsigned int Charge = 0;
102  double Path = (10.0*thickness(DetId))/fabs(cosine);
103  double PrevGain = -1;
104 
105  if(gainHandle.isValid()){
106  SiStripApvGain::Range detGainRange = gainHandle->getRange(DetId);
107  PrevGain = *(detGainRange.first + APVId);
108  }
109 
110  for(unsigned int a=0;a<Ampls.size();a++){
111  Charge+=Ampls[a];
112  if(Ampls[a] >=254)Saturation =true;
113  amplitude->push_back( Ampls[a] );
114  }
115  double ChargeOverPath = (double)Charge / Path ;
116 
117  if(FirstStrip==0 )Overlapping=true;
118  if(FirstStrip==128 )Overlapping=true;
119  if(FirstStrip==256 )Overlapping=true;
120  if(FirstStrip==384 )Overlapping=true;
121  if(FirstStrip==512 )Overlapping=true;
122  if(FirstStrip==640 )Overlapping=true;
123 
124  if(FirstStrip<=127 && FirstStrip+Ampls.size()>127)Overlapping=true;
125  if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlapping=true;
126  if(FirstStrip<=383 && FirstStrip+Ampls.size()>383)Overlapping=true;
127  if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlapping=true;
128  if(FirstStrip<=639 && FirstStrip+Ampls.size()>639)Overlapping=true;
129 
130  if(FirstStrip+Ampls.size()==127 )Overlapping=true;
131  if(FirstStrip+Ampls.size()==255 )Overlapping=true;
132  if(FirstStrip+Ampls.size()==383 )Overlapping=true;
133  if(FirstStrip+Ampls.size()==511 )Overlapping=true;
134  if(FirstStrip+Ampls.size()==639 )Overlapping=true;
135  if(FirstStrip+Ampls.size()==767 )Overlapping=true;
136 
137  trackindex ->push_back( shallow::findTrackIndex(tracks, track) );
138  rawid ->push_back( DetId );
139  localdirx ->push_back( trackDirection.x() );
140  localdiry ->push_back( trackDirection.y() );
141  localdirz ->push_back( trackDirection.z() );
142  firststrip ->push_back( FirstStrip );
143  nstrips ->push_back( Ampls.size() );
144  saturation ->push_back( Saturation );
145  overlapping ->push_back( Overlapping );
146  farfromedge ->push_back( IsFarFromBorder(&trajState,DetId, &iSetup) );
147  charge ->push_back( Charge );
148  path ->push_back( Path );
149  chargeoverpath->push_back( ChargeOverPath );
150  gainused ->push_back( PrevGain );
151  }
152  }
153  }
154 
155  iEvent.put(trackindex, Prefix + "trackindex" + Suffix );
156  iEvent.put(rawid , Prefix + "rawid" + Suffix );
157  iEvent.put(localdirx , Prefix + "localdirx" + Suffix );
158  iEvent.put(localdiry , Prefix + "localdiry" + Suffix );
159  iEvent.put(localdirz , Prefix + "localdirz" + Suffix );
160  iEvent.put(firststrip, Prefix + "firststrip" + Suffix );
161  iEvent.put(nstrips, Prefix + "nstrips" + Suffix );
162  iEvent.put(saturation, Prefix + "saturation" + Suffix );
163  iEvent.put(overlapping, Prefix + "overlapping" + Suffix );
164  iEvent.put(farfromedge, Prefix + "farfromedge" + Suffix );
165  iEvent.put(charge, Prefix + "charge" + Suffix );
166  iEvent.put(path, Prefix + "path" + Suffix );
167  iEvent.put(chargeoverpath,Prefix + "chargeoverpath"+ Suffix );
168  iEvent.put(amplitude, Prefix + "amplitude" + Suffix );
169  iEvent.put(gainused, Prefix + "gainused" + Suffix );
170 }
171 
172 /*
173 void ShallowGainCalibration::beginJob(const edm::EventSetup& iSetup)
174 {
175  printf("Befin JOB\n");
176 
177  edm::ESHandle<TrackerGeometry> tkGeom;
178  iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
179  vector<GeomDet*> Det = tkGeom->dets();
180 
181  edm::ESHandle<SiStripGain> gainHandle;
182  iSetup.get<SiStripGainRcd>().get(gainHandle);
183  if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
184 
185  for(unsigned int i=0;i<Det.size();i++){
186  DetId Detid = Det[i]->geographicalId();
187  int SubDet = Detid.subdetId();
188 
189  if( SubDet == StripSubdetector::TIB || SubDet == StripSubdetector::TID ||
190  SubDet == StripSubdetector::TOB || SubDet == StripSubdetector::TEC ){
191 
192  StripGeomDetUnit* DetUnit = dynamic_cast<StripGeomDetUnit*> (Det[i]);
193  if(!DetUnit)continue;
194 
195  const StripTopology& Topo = DetUnit->specificTopology();
196  unsigned int NAPV = Topo.nstrips()/128;
197 
198  for(unsigned int j=0;j<NAPV;j++){
199  stAPVGain* APV = new stAPVGain;
200  APV->DetId = Detid.rawId();
201  APV->APVId = j;
202  APV->PreviousGain = 1;
203 
204  APVsCollOrdered.push_back(APV);
205  APVsColl[(APV->DetId<<3) | APV->APVId] = APV;
206  }
207  }
208  }
209 }
210 
211 
212 void ShallowGainCalibration::beginRun(edm::Run &, const edm::EventSetup &iSetup){
213  printf("BEFIN RUN\n");
214 
215  edm::ESHandle<SiStripGain> gainHandle;
216  iSetup.get<SiStripGainRcd>().get(gainHandle);
217  if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
218 
219  for(std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin();it!=APVsCollOrdered.end();it++){
220  stAPVGain* APV = *it;
221  SiStripApvGain::Range detGainRange = gainHandle->getRange(APV->DetId);
222  APV->PreviousGain = *(detGainRange.first + APV->APVId);
223  }
224 }
225 */
226 
228 {
229  edm::ESHandle<TrackerGeometry> tkGeom; iSetup->get<TrackerDigiGeometryRecord>().get( tkGeom );
230 
231  LocalPoint HitLocalPos = trajState->localPosition();
232  LocalError HitLocalError = trajState->localError().positionError() ;
233 
234  const GeomDetUnit* it = tkGeom->idToDetUnit(DetId(detid));
235  if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
236  std::cout << "this detID doesn't seem to belong to the Tracker" << std::endl;
237  return false;
238  }
239 
240  const BoundPlane plane = it->surface();
241  const TrapezoidalPlaneBounds* trapezoidalBounds( dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
242  const RectangularPlaneBounds* rectangularBounds( dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
243 
244  double DistFromBorder = 1.0;
245  double HalfLength = it->surface().bounds().length() /2.0;
246 
247  if(trapezoidalBounds)
248  {
249  std::array<const float, 4> const & parameters = (*trapezoidalBounds).parameters();
250  HalfLength = parameters[3];
251  }else if(rectangularBounds){
252  HalfLength = it->surface().bounds().length() /2.0;
253  }else{return false;}
254 
255  if (fabs(HitLocalPos.y())+HitLocalError.yy() >= (HalfLength - DistFromBorder) ) return false;
256 
257  return true;
258 }
259 
260 
262 {
263  map<DetId,double>::iterator th=m_thicknessMap.find(id);
264  if(th!=m_thicknessMap.end())
265  return (*th).second;
266  else {
267  double detThickness=1.;
268  //compute thickness normalization
269  const GeomDetUnit* it = m_tracker->idToDetUnit(DetId(id));
270  bool isPixel = dynamic_cast<const PixelGeomDetUnit*>(it)!=0;
271  bool isStrip = dynamic_cast<const StripGeomDetUnit*>(it)!=0;
272  if (!isPixel && ! isStrip) {
273  //FIXME throw exception
274  edm::LogWarning("DeDxHitsProducer") << "\t\t this detID doesn't seem to belong to the Tracker";
275  detThickness = 1.;
276  }else{
277  detThickness = it->surface().bounds().thickness();
278  }
279 
280  m_thicknessMap[id]=detThickness;//computed value
281  return detThickness;
282  }
283 
284 }
285 
ClusterRef cluster() const
virtual const TrackerGeomDet * idToDetUnit(DetId) const
Return the pointer to the GeomDetUnit corresponding to a given DetId.
dictionary parameters
Definition: Parameters.py:2
unsigned int stereoId() const
virtual float length() const =0
int findTrackIndex(const edm::Handle< edm::View< reco::Track > > &h, const reco::Track *t)
Definition: ShallowTools.cc:29
SiStripCluster const & monoCluster() const
LocalVector localDirection() const
std::map< DetId, double > m_thicknessMap
T y() const
Definition: PV3DBase.h:63
#define NULL
Definition: scimark2.h:8
const Bounds & bounds() const
Definition: Surface.h:128
uint16_t firstStrip() const
ShallowGainCalibration(const edm::ParameterSet &)
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:40
LocalError positionError() const
bool IsFarFromBorder(TrajectoryStateOnSurface *trajState, const uint32_t detid, const edm::EventSetup *iSetup)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
DataContainer const & measurements() const
Definition: Trajectory.h:203
int iEvent
Definition: GenABIO.cc:230
T mag() const
Definition: PV3DBase.h:67
tuple path
else: Piece not in the list, fine.
float yy() const
Definition: LocalError.h:26
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
Definition: Path.h:40
T z() const
Definition: PV3DBase.h:64
std::pair< ContainerIterator, ContainerIterator > Range
ClusterRef cluster() const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
const LocalTrajectoryError & localError() const
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
Definition: DetId.h:18
const TrackerGeometry * m_tracker
tuple tracks
Definition: testEve_cfg.py:39
const T & get() const
Definition: EventSetup.h:55
void produce(edm::Event &, const edm::EventSetup &)
SiStripCluster const & stereoCluster() const
double a
Definition: hdecay.h:121
tuple cout
Definition: gather_cfg.py:121
unsigned int monoId() const
DetId geographicalId() const
bool isValid() const
Definition: ESHandle.h:47
T x() const
Definition: PV3DBase.h:62
const std::vector< uint8_t > & amplitudes() const