CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BetaCalculatorECAL.cc
Go to the documentation of this file.
3 
10 
13 
15 
17  EBRecHitCollection_ (iConfig.getParameter<edm::InputTag>("EBRecHitCollection")),
18  EERecHitCollection_ (iConfig.getParameter<edm::InputTag>("EERecHitCollection"))
19 {
20  edm::ParameterSet trkParameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
21  parameters_.loadParameters( trkParameters );
23 
24 }
25 
26 
28  bool setCalo = false;
30 
31  // EcalDetIdAssociator
32  iSetup.get<DetIdAssociatorRecord>().get("EcalDetIdAssociator", ecalDetIdAssociator_);
33  // Get the Bfield
34  iSetup.get<IdealMagneticFieldRecord>().get(bField_);
35  // Geometry
37  const CaloGeometry* theGeometry = theCaloGeometry_.product();
38  // Topology
39  edm::ESHandle<CaloTopology> pCaloTopology;
40  iSetup.get<CaloTopologyRecord>().get(pCaloTopology);
41  const CaloTopology* theCaloTopology = pCaloTopology.product();
42  // EcalRecHits
44  iEvent.getByLabel(EBRecHitCollection_,ebRecHits);
46  iEvent.getByLabel(EERecHitCollection_,eeRecHits);
47 
48  // select the track
50  if(candidate.hasTrackRef())
51  track = *(candidate.trackRef());
52  else
53  return; // in case there is no track ref, we can't do much
54 
55  // compute the track isolation
56  result.trkIsoDr=100;
57  for(reco::TrackCollection::const_iterator ndTrack = tracks->begin(); ndTrack != tracks->end(); ++ndTrack) {
58  double dr=sqrt(pow((track.outerEta()-ndTrack->outerEta()),2)+pow((track.outerPhi()-ndTrack->outerPhi()),2));
59  if(dr>0.00001 && dr<result.trkIsoDr) result.trkIsoDr=dr;
60  }
61 
62  // use the track associator to propagate to the calo
65  parameters_ );
66 
67  // do a custom propagation through Ecal
68  std::map<int,GlobalPoint> trackExitPositionMap; // rawId to exit position (subtracting cry center)
69  std::map<int,float> trackCrossedXtalCurvedMap; // rawId to trackLength
70  TrajectoryStateTransform tsTransform;
71  FreeTrajectoryState tkInnerState = tsTransform.innerFreeState(track, &*bField_);
72  // Build set of points in Ecal (necklace) using the propagator
73  std::vector<SteppingHelixStateInfo> neckLace;
74  neckLace = calcEcalDeposit(&tkInnerState,*ecalDetIdAssociator_);
75  // Initialize variables to be filled by the track-length function
76  double totalLengthCurved = 0.;
77  GlobalPoint internalPointCurved(0., 0., 0.);
78  GlobalPoint externalPointCurved(0., 0., 0.);
79  if(neckLace.size() > 1)
80  {
81  getDetailedTrackLengthInXtals(trackExitPositionMap,
82  trackCrossedXtalCurvedMap,
83  totalLengthCurved,
84  internalPointCurved,
85  externalPointCurved,
86  & (*theGeometry),
87  & (*theCaloTopology),
88  neckLace);
89  }
90  int numCrysCrossed = -1;
91  numCrysCrossed = trackCrossedXtalCurvedMap.size();
92 
93  // Make weighted sum of times
94  float sumWeightedTime = 0;
95  float sumTimeErrorSqr = 0;
96  float sumEnergy = 0;
97  float sumTrackLength = 0;
98  std::vector<EcalRecHit> crossedRecHits;
100 
101  std::map<int,GlobalPoint>::const_iterator trackExitMapIt = trackExitPositionMap.begin();
102  for(std::map<int,float>::const_iterator mapIt = trackCrossedXtalCurvedMap.begin();
103  mapIt != trackCrossedXtalCurvedMap.end(); ++mapIt)
104  {
105  if(DetId(mapIt->first).subdetId()==EcalBarrel)
106  {
107  EBDetId ebDetId(mapIt->first);
108  thisHit = ebRecHits->find(ebDetId);
109  if(thisHit == ebRecHits->end())
110  {
111  //std::cout << "\t Could not find crossedEcal detId: " << ebDetId << " in EBRecHitCollection!" << std::endl;
112  continue;
113  }
114  const EcalRecHit hit = *thisHit;
115  // Cut out badly-reconstructed hits
116  if(!hit.isTimeValid())
117  continue;
118  uint32_t rhFlag = hit.recoFlag();
119  if((rhFlag != EcalRecHit::kGood) && (rhFlag != EcalRecHit::kOutOfTime) && (rhFlag != EcalRecHit::kPoorCalib))
120  continue;
121 
122  float errorOnThis = hit.timeError();
123  sumTrackLength+=mapIt->second;
124  sumEnergy+=hit.energy();
125  crossedRecHits.push_back(hit);
126 // result.ecalSwissCrossKs.push_back(EcalSeverityLevelAlgo::spikeFromNeighbours(ebDetId,(*ebRecHits),0.2,EcalSeverityLevelAlgo::kSwissCross));
127 // result.ecalE1OverE9s.push_back(EcalSeverityLevelAlgo::spikeFromNeighbours(ebDetId,(*ebRecHits),0.2,EcalSeverityLevelAlgo::kE1OverE9));
128  result.ecalTrackLengths.push_back(mapIt->second);
129  result.ecalTrackExitPositions.push_back(trackExitMapIt->second);
130  result.ecalEnergies.push_back(hit.energy());
131  result.ecalTimes.push_back(hit.time());
132  result.ecalTimeErrors.push_back(hit.timeError());
133  result.ecalOutOfTimeEnergies.push_back(hit.outOfTimeEnergy());
134  result.ecalOutOfTimeChi2s.push_back(hit.outOfTimeChi2());
135  result.ecalChi2s.push_back(hit.chi2());
136  result.ecalDetIds.push_back(ebDetId);
137  // SIC DEBUG
138  //std::cout << " SIC DEBUG: time error on this crossed RecHit: " << errorOnThis << " energy of hit: "
139  // << hit.energy() << " time of hit: " << hit.time() << " trackLength: " << mapIt->second << std::endl;
140 
141  if(hit.isTimeErrorValid()) // use hit time for weighted time average
142  {
143  sumWeightedTime+=hit.time()/(errorOnThis*errorOnThis);
144  sumTimeErrorSqr+=1/(errorOnThis*errorOnThis);
145  }
146  }
147  trackExitMapIt++;
148  }
149 
150  if(crossedRecHits.size() > 0)
151  {
152  setCalo = true;
153  sort(crossedRecHits.begin(),crossedRecHits.end(),EcalRecHitLess());
154  result.ecalCrossedEnergy = sumEnergy;
155  result.ecalCrysCrossed = crossedRecHits.size();
156  result.ecalDeDx = sumEnergy/sumTrackLength;
157  // replace the below w/o trackassociator quantities?
160 
161  if(sumTimeErrorSqr > 0)
162  {
163  result.ecalTime = sumWeightedTime/sumTimeErrorSqr;
164  result.ecalTimeError = sqrt(1/sumTimeErrorSqr);
165  DetId maxEnergyId = crossedRecHits.begin()->id();
166 
167  if(maxEnergyId != DetId()) // double check
168  {
169  // To get beta, we assume photon propagation time is about the same for muons and e/gamma
170  // Since the typical path length is >> crystal length, this shouldn't be too bad
171  GlobalPoint position = info.getPosition(maxEnergyId); // position of crystal center on front face
172  double frontFaceR = sqrt(pow(position.x(),2)+pow(position.y(),2)+pow(position.z(),2));
173  double muonShowerMax = frontFaceR+11.5; // assume muon "showerMax" is halfway into the crystal
174  double gammaShowerMax = frontFaceR+6.23; // 7 X0 for e/gamma showerMax
175  double speedOfLight = 29.979; // cm/ns
176  result.ecalBeta = (muonShowerMax)/(result.ecalTime*speedOfLight+gammaShowerMax);
177  result.ecalBetaError = (speedOfLight*muonShowerMax*result.ecalTimeError)/pow(speedOfLight*result.ecalTime+gammaShowerMax,2);
178  result.ecalInvBetaError = speedOfLight*result.ecalTimeError/muonShowerMax;
179  }
180  // SIC debug
181  //std::cout << "BetaCalcEcal: CrossedRecHits: " << crossedRecHits.size()
182  // << " ecalTime: " << result.ecalTime << " timeError: " << result.ecalTimeError
183  // << " ecalCrossedEnergy: " << result.ecalCrossedEnergy << " ecalBeta: " << result.ecalBeta
184  // << " ecalBetaError: " << result.ecalBetaError << " ecalDeDx (MeV/cm): " << 1000*result.ecalDeDx << std::endl;
185  }
186  }
187 
188  if(info.crossedHcalRecHits.size() > 0)
189  {
190  // HCAL (not ECAL) info
193  //maxEnergyId = info.findMaxDeposition(TrackDetMatchInfo::HcalRecHits);
196  }
197 
198  if(setCalo)
199  caloInfo = result;
200 }
201 
202 std::vector<SteppingHelixStateInfo> BetaCalculatorECAL::calcEcalDeposit(const FreeTrajectoryState* tkInnerState,
203  const DetIdAssociator& associator)
204 {
205  // Set some parameters
206  double minR = associator.volume().minR () ;
207  double minZ = associator.volume().minZ () ;
208  double maxR = associator.volume().maxR () ;
209  double maxZ = associator.volume().maxZ () ;
210 
211  // Define the TrackOrigin (where the propagation starts)
212  SteppingHelixStateInfo trackOrigin(*tkInnerState);
213 
214  // Define Propagator
216  prop -> setMaterialMode(false);
217  prop -> applyRadX0Correction(true);
218 
219  // Build the necklace
220  CachedTrajectory neckLace;
221  neckLace.setStateAtIP(trackOrigin);
222  neckLace.reset_trajectory();
223  neckLace.setPropagator(prop);
224  neckLace.setPropagationStep(0.1);
225  neckLace.setMinDetectorRadius(minR);
226  neckLace.setMinDetectorLength(minZ*2.);
227  neckLace.setMaxDetectorRadius(maxR);
228  neckLace.setMaxDetectorLength(maxZ*2.);
229 
230  // Propagate track
231  bool isPropagationSuccessful = neckLace.propagateAll(trackOrigin);
232 
233  if (!isPropagationSuccessful)
234  {
235  //std::cout << ">>>>>> calcEcalDeposits::propagateAll::failed " << "<<<<<<" << std::endl;
236  //std::cout << "innerOrigin = " << glbTrackInnerOrigin.position() << " innerR = " << innerR << std::endl;
237  return std::vector<SteppingHelixStateInfo> () ;
238  }
239 
240  std::vector<SteppingHelixStateInfo> complicatePoints;
241  neckLace.getTrajectory(complicatePoints, associator.volume(), 500);
242  //std::cerr << "necklace size = " << complicatePoints.size() << std::endl;
243 
244  return complicatePoints;
245 }
246 
247 int BetaCalculatorECAL::getDetailedTrackLengthInXtals(std::map<int,GlobalPoint>& trackExitPositionMap,
248  std::map<int,float>& trackCrossedXtalMap,
249  double& totalLengthCurved,
250  GlobalPoint& internalPointCurved,
251  GlobalPoint& externalPointCurved,
252  const CaloGeometry* theGeometry,
253  const CaloTopology* theTopology,
254  const std::vector<SteppingHelixStateInfo>& neckLace)
255 {
256  GlobalPoint origin (0., 0., 0.);
257  internalPointCurved = origin ;
258  externalPointCurved = origin ;
259 
260  bool firstPoint = false;
261  trackCrossedXtalMap.clear();
262 
263  const CaloSubdetectorGeometry* theBarrelSubdetGeometry = theGeometry->getSubdetectorGeometry(DetId::Ecal,1);
264  const CaloSubdetectorGeometry* theEndcapSubdetGeometry = theGeometry->getSubdetectorGeometry(DetId::Ecal,2);
265 
266  for(std::vector<SteppingHelixStateInfo>::const_iterator itr = (neckLace.begin() + 1); itr != neckLace.end(); ++itr)
267  {
268  GlobalPoint probe_gp = (*itr).position();
269  GlobalVector probe_gv = (*itr).momentum();
270  std::vector<DetId> surroundingMatrix;
271 
272  EBDetId closestBarrelDetIdToProbe = ((theBarrelSubdetGeometry -> getClosestCell(probe_gp)).rawId());
273  EEDetId closestEndcapDetIdToProbe = ((theEndcapSubdetGeometry -> getClosestCell(probe_gp)).rawId());
274 
275  // check if the probe is inside the xtal
276  if( (closestEndcapDetIdToProbe) && (theGeometry->getSubdetectorGeometry(closestEndcapDetIdToProbe)->
277  getGeometry(closestEndcapDetIdToProbe)->inside(probe_gp)) )
278  {
279  double step = ((*itr).position() - (*(itr-1)).position()).mag();
280  GlobalPoint point = itr->position();
281  addStepToXtal(trackExitPositionMap, trackCrossedXtalMap, closestEndcapDetIdToProbe, step, point, theEndcapSubdetGeometry);
282  totalLengthCurved += step;
283 
284  if (firstPoint == false)
285  {
286  internalPointCurved = probe_gp ;
287  firstPoint = true ;
288  }
289 
290  externalPointCurved = probe_gp ;
291  }
292 
293  if( (closestBarrelDetIdToProbe) && (theGeometry->getSubdetectorGeometry(closestBarrelDetIdToProbe)->
294  getGeometry(closestBarrelDetIdToProbe)->inside(probe_gp)) )
295  {
296  double step = ((*itr).position() - (*(itr-1)).position()).mag();
297  GlobalPoint point = itr->position();
298  addStepToXtal(trackExitPositionMap, trackCrossedXtalMap, closestBarrelDetIdToProbe, step, point, theBarrelSubdetGeometry);
299  totalLengthCurved += step;
300 
301  if (firstPoint == false)
302  {
303  internalPointCurved = probe_gp ;
304  firstPoint = true ;
305  }
306 
307  externalPointCurved = probe_gp ;
308  }
309  else
310  {
311  // 3x3 matrix surrounding the probe
312  surroundingMatrix = theTopology->getSubdetectorTopology(closestBarrelDetIdToProbe)->getWindow(closestBarrelDetIdToProbe,3,3);
313 
314  for( unsigned int k=0; k<surroundingMatrix.size(); ++k ) {
315  if(theGeometry->getSubdetectorGeometry(surroundingMatrix.at(k))->getGeometry(surroundingMatrix.at(k))->inside(probe_gp))
316  {
317  double step = ((*itr).position() - (*(itr-1)).position()).mag();
318  GlobalPoint point = itr->position();
319  addStepToXtal(trackExitPositionMap, trackCrossedXtalMap, surroundingMatrix[k], step,
320  point, theGeometry->getSubdetectorGeometry(surroundingMatrix.at(k)));
321  totalLengthCurved += step;
322 
323  if (firstPoint == false)
324  {
325  internalPointCurved = probe_gp ;
326  firstPoint = true ;
327  }
328 
329  externalPointCurved = probe_gp ;
330  }
331  }
332 
333  // clear neighborhood matrix
334  surroundingMatrix.clear();
335  }
336  }
337 
338  return 0;
339 }
340 
341 void BetaCalculatorECAL::addStepToXtal(std::map<int,GlobalPoint>& trackExitPositionMap,
342  std::map<int,float>& trackCrossedXtalMap,
343  DetId aDetId,
344  float step,
346  const CaloSubdetectorGeometry* theSubdetGeometry)
347 {
348 
349  const CaloCellGeometry *cell_p = theSubdetGeometry->getGeometry(aDetId);
350  GlobalPoint p = (dynamic_cast <const TruncatedPyramid *> (cell_p))->getPosition(23);
351  GlobalPoint diff(point.x()-p.x(),point.y()-p.y(),point.z()-p.z());
352 
353  std::map<int,GlobalPoint>::iterator xtal = trackExitPositionMap.find(aDetId.rawId());
354  if (xtal!=trackExitPositionMap.end())
355  ((*xtal).second)=diff;
356  else
357  trackExitPositionMap.insert(std::pair<int,GlobalPoint>(aDetId.rawId(),diff));
358 
359  std::map<int,float>::iterator xtal2 = trackCrossedXtalMap.find(aDetId.rawId());
360  if (xtal2!= trackCrossedXtalMap.end())
361  ((*xtal2).second)+=step;
362  else
363  trackCrossedXtalMap.insert(std::pair<int,float>(aDetId.rawId(),step));
364 }
365 
366 
367 
T getParameter(std::string const &) const
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:43
GlobalPoint getPosition(const DetId &)
int getDetailedTrackLengthInXtals(std::map< int, GlobalPoint > &trackExitPositionMap, std::map< int, float > &trackCrossedXtalMap, double &totalLengthCurved, GlobalPoint &internalPointCurved, GlobalPoint &externalPointCurved, const CaloGeometry *theGeometry, const CaloTopology *theTopology, const std::vector< SteppingHelixStateInfo > &neckLace)
std::vector< float > ecalTrackLengths
Definition: HSCPCaloInfo.h:28
std::vector< float > ecalEnergies
Definition: HSCPCaloInfo.h:29
bool isTimeErrorValid() const
Definition: EcalRecHit.cc:184
std::vector< const HBHERecHit * > crossedHcalRecHits
void setMinDetectorLength(float l=0.)
static FreeTrajectoryState getFreeTrajectoryState(const edm::EventSetup &, const reco::Track &)
get FreeTrajectoryState from different track representations
void setMinDetectorRadius(float r=0.)
double maxZ(bool withTolerance=true) const
std::vector< GlobalPoint > ecalTrackExitPositions
Definition: HSCPCaloInfo.h:36
void useDefaultPropagator()
use the default propagator
std::vector< T >::const_iterator const_iterator
T y() const
Definition: PV3DBase.h:57
BetaCalculatorECAL(const edm::ParameterSet &iConfig)
void setPropagationStep(float s=20.)
void getTrajectory(std::vector< SteppingHelixStateInfo > &, const FiducialVolume &, int steps=4)
float time() const
Definition: CaloRecHit.h:20
double minZ(bool withTolerance=true) const
bool hasTrackRef() const
Definition: HSCParticle.h:56
double nXnEnergy(const DetId &, EnergyType, int gridSize=1)
get energy of the NxN shape (N = 2*gridSize + 1) around given detector element
bool isTimeValid() const
Definition: EcalRecHit.cc:175
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
FreeTrajectoryState innerFreeState(const reco::Track &tk, const MagneticField *field) const
std::vector< DetId > ecalDetIds
Definition: HSCPCaloInfo.h:35
std::vector< float > ecalTimeErrors
Definition: HSCPCaloInfo.h:31
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
std::vector< float > ecalTimes
Definition: HSCPCaloInfo.h:30
int iEvent
Definition: GenABIO.cc:243
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
bool propagateAll(const SteppingHelixStateInfo &initialState)
propagate through the whole detector, returns true if successful
uint32_t recoFlag() const
Definition: EcalRecHit.h:78
float outOfTimeEnergy() const
Definition: EcalRecHit.cc:64
float outOfTimeChi2() const
Definition: EcalRecHit.cc:58
float energy() const
Definition: CaloRecHit.h:19
T sqrt(T t)
Definition: SSEVec.h:28
float chi2() const
Definition: EcalRecHit.cc:27
T z() const
Definition: PV3DBase.h:58
tuple result
Definition: query.py:137
edm::ESHandle< DetIdAssociator > ecalDetIdAssociator_
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:39
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
double crossedEnergy(EnergyType)
energy in detector elements crossed by the track by types
float timeError() const
Definition: EcalRecHit.cc:158
std::vector< float > ecalChi2s
Definition: HSCPCaloInfo.h:32
int k[5][pyjets_maxn]
double minR(bool withTolerance=true) const
TrackAssociatorParameters parameters_
Definition: DetId.h:20
double outerEta() const
pseudorapidity of the momentum vector at the outermost hit position
Definition: Track.h:89
void setPropagator(const Propagator *ptr)
virtual std::vector< DetId > getWindow(const DetId &id, const int &northSouthSize, const int &eastWestSize) const
tuple tracks
Definition: testEve_cfg.py:39
const T & get() const
Definition: EventSetup.h:55
edm::InputTag EERecHitCollection_
T const * product() const
Definition: ESHandle.h:62
void addStepToXtal(std::map< int, GlobalPoint > &trackExitPositionMap, std::map< int, float > &trackCrossedXtalMap, DetId aDetId, float step, GlobalPoint point, const CaloSubdetectorGeometry *theSubdetGeometry)
const CaloSubdetectorTopology * getSubdetectorTopology(const DetId &id) const
access the subdetector Topology for the given subdetector directly
Definition: CaloTopology.cc:26
A base class to handle the particular shape of Ecal Xtals. Taken from ORCA Calorimetry Code...
reco::TrackRef trackRef() const
Definition: HSCParticle.h:70
TrackDetectorAssociator trackAssociator_
TrackDetMatchInfo associate(const edm::Event &, const edm::EventSetup &, const FreeTrajectoryState &, const AssociatorParameters &)
std::vector< float > ecalOutOfTimeChi2s
Definition: HSCPCaloInfo.h:33
const FiducialVolume & volume() const
get active detector volume
edm::ESHandle< MagneticField > bField_
std::vector< float > ecalOutOfTimeEnergies
Definition: HSCPCaloInfo.h:34
void setStateAtIP(const SteppingHelixStateInfo &state)
T x() const
Definition: PV3DBase.h:56
double outerPhi() const
azimuthal angle of the momentum vector at the outermost hit position
Definition: Track.h:87
void setMaxDetectorLength(float l=2200.)
edm::ESHandle< CaloGeometry > theCaloGeometry_
double maxR(bool withTolerance=true) const
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
void loadParameters(const edm::ParameterSet &)
void setMaxDetectorRadius(float r=800.)
std::vector< SteppingHelixStateInfo > calcEcalDeposit(const FreeTrajectoryState *tkInnerState, const DetIdAssociator &associator)
void addInfoToCandidate(HSCParticle &candidate, edm::Handle< reco::TrackCollection > &tracks, edm::Event &iEvent, const edm::EventSetup &iSetup, HSCPCaloInfo &caloInfo)
const double speedOfLight
edm::InputTag EBRecHitCollection_