CMS 3D CMS Logo

BetaCalculatorECAL.cc
Go to the documentation of this file.
3 
12 
14 
16 
17 using namespace susybsm;
18 
20  : EBRecHitCollectionToken_(
21  iC.consumes<EBRecHitCollection>(iConfig.getParameter<edm::InputTag>("EBRecHitCollection"))),
22  EERecHitCollectionToken_(
23  iC.consumes<EERecHitCollection>(iConfig.getParameter<edm::InputTag>("EERecHitCollection"))) {
24  edm::ParameterSet trkParameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
25  parameters_.loadParameters(trkParameters, iC);
27 }
28 
32  const edm::EventSetup& iSetup,
33  HSCPCaloInfo& caloInfo) {
34  bool setCalo = false;
36 
37  // EcalDetIdAssociator
38  iSetup.get<DetIdAssociatorRecord>().get("EcalDetIdAssociator", ecalDetIdAssociator_);
39  // Get the Bfield
40  iSetup.get<IdealMagneticFieldRecord>().get(bField_);
41  // Geometry
43  const CaloGeometry* theGeometry = theCaloGeometry_.product();
44  // Topology
45  edm::ESHandle<CaloTopology> pCaloTopology;
46  iSetup.get<CaloTopologyRecord>().get(pCaloTopology);
47  const CaloTopology* theCaloTopology = pCaloTopology.product();
48  // EcalRecHits
50  iEvent.getByToken(EBRecHitCollectionToken_, ebRecHits);
52  iEvent.getByToken(EERecHitCollectionToken_, eeRecHits);
53 
54  // select the track
56  if (candidate.hasTrackRef())
57  track = *(candidate.trackRef());
58  else
59  return; // in case there is no track ref, we can't do much
60 
61  // compute the track isolation
62  result.trkIsoDr = 100;
63  for (reco::TrackCollection::const_iterator ndTrack = tracks->begin(); ndTrack != tracks->end(); ++ndTrack) {
64  double dr =
65  sqrt(pow((track.outerEta() - ndTrack->outerEta()), 2) + pow((track.outerPhi() - ndTrack->outerPhi()), 2));
66  if (dr > 0.00001 && dr < result.trkIsoDr)
67  result.trkIsoDr = dr;
68  }
69 
70  // use the track associator to propagate to the calo
73 
74  // do a custom propagation through Ecal
75  std::map<int, GlobalPoint> trackExitPositionMap; // rawId to exit position (subtracting cry center)
76  std::map<int, float> trackCrossedXtalCurvedMap; // rawId to trackLength
77 
79  // Build set of points in Ecal (necklace) using the propagator
80  std::vector<SteppingHelixStateInfo> neckLace;
81  neckLace = calcEcalDeposit(&tkInnerState, *ecalDetIdAssociator_);
82  // Initialize variables to be filled by the track-length function
83  double totalLengthCurved = 0.;
84  GlobalPoint internalPointCurved(0., 0., 0.);
85  GlobalPoint externalPointCurved(0., 0., 0.);
86  if (neckLace.size() > 1) {
87  getDetailedTrackLengthInXtals(trackExitPositionMap,
88  trackCrossedXtalCurvedMap,
89  totalLengthCurved,
90  internalPointCurved,
91  externalPointCurved,
92  &(*theGeometry),
93  &(*theCaloTopology),
94  neckLace);
95  }
96 
97  // Make weighted sum of times
98  float sumWeightedTime = 0;
99  float sumTimeErrorSqr = 0;
100  float sumEnergy = 0;
101  float sumTrackLength = 0;
102  std::vector<EcalRecHit> crossedRecHits;
104 
105  std::map<int, GlobalPoint>::const_iterator trackExitMapIt = trackExitPositionMap.begin();
106  for (std::map<int, float>::const_iterator mapIt = trackCrossedXtalCurvedMap.begin();
107  mapIt != trackCrossedXtalCurvedMap.end();
108  ++mapIt) {
109  if (DetId(mapIt->first).subdetId() == EcalBarrel) {
110  EBDetId ebDetId(mapIt->first);
111  thisHit = ebRecHits->find(ebDetId);
112  if (thisHit == ebRecHits->end()) {
113  //std::cout << "\t Could not find crossedEcal detId: " << ebDetId << " in EBRecHitCollection!" << std::endl;
114  continue;
115  }
116  const EcalRecHit hit = *thisHit;
117  // Cut out badly-reconstructed hits
118  if (!hit.isTimeValid())
119  continue;
120  uint32_t rhFlag = hit.recoFlag();
121  if ((rhFlag != EcalRecHit::kGood) && (rhFlag != EcalRecHit::kOutOfTime) && (rhFlag != EcalRecHit::kPoorCalib))
122  continue;
123 
124  float errorOnThis = hit.timeError();
125  sumTrackLength += mapIt->second;
126  sumEnergy += hit.energy();
127  crossedRecHits.push_back(hit);
128  // result.ecalSwissCrossKs.push_back(EcalSeverityLevelAlgo::spikeFromNeighbours(ebDetId,(*ebRecHits),0.2,EcalSeverityLevelAlgo::kSwissCross));
129  // result.ecalE1OverE9s.push_back(EcalSeverityLevelAlgo::spikeFromNeighbours(ebDetId,(*ebRecHits),0.2,EcalSeverityLevelAlgo::kE1OverE9));
130  result.ecalTrackLengths.push_back(mapIt->second);
131  result.ecalTrackExitPositions.push_back(trackExitMapIt->second);
132  result.ecalEnergies.push_back(hit.energy());
133  result.ecalTimes.push_back(hit.time());
134  result.ecalTimeErrors.push_back(hit.timeError());
135  result.ecalOutOfTimeEnergies.push_back(0.);
136  result.ecalOutOfTimeChi2s.push_back(0.);
137  result.ecalChi2s.push_back(hit.chi2());
138  result.ecalDetIds.push_back(ebDetId);
139  // SIC DEBUG
140  //std::cout << " SIC DEBUG: time error on this crossed RecHit: " << errorOnThis << " energy of hit: "
141  // << hit.energy() << " time of hit: " << hit.time() << " trackLength: " << mapIt->second << std::endl;
142 
143  if (hit.isTimeErrorValid()) // use hit time for weighted time average
144  {
145  sumWeightedTime += hit.time() / (errorOnThis * errorOnThis);
146  sumTimeErrorSqr += 1 / (errorOnThis * errorOnThis);
147  }
148  }
149  trackExitMapIt++;
150  }
151 
152  if (!crossedRecHits.empty()) {
153  setCalo = true;
154  sort(crossedRecHits.begin(), crossedRecHits.end(), [](auto& x, auto& y) { return (x.energy() > y.energy()); });
155  result.ecalCrossedEnergy = sumEnergy;
156  result.ecalCrysCrossed = crossedRecHits.size();
157  result.ecalDeDx = sumEnergy / sumTrackLength;
158  // replace the below w/o trackassociator quantities?
161 
162  if (sumTimeErrorSqr > 0) {
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) /
178  pow(speedOfLight * result.ecalTime + gammaShowerMax, 2);
179  result.ecalInvBetaError = speedOfLight * result.ecalTimeError / muonShowerMax;
180  }
181  // SIC debug
182  //std::cout << "BetaCalcEcal: CrossedRecHits: " << crossedRecHits.size()
183  // << " ecalTime: " << result.ecalTime << " timeError: " << result.ecalTimeError
184  // << " ecalCrossedEnergy: " << result.ecalCrossedEnergy << " ecalBeta: " << result.ecalBeta
185  // << " ecalBetaError: " << result.ecalBetaError << " ecalDeDx (MeV/cm): " << 1000*result.ecalDeDx << std::endl;
186  }
187  }
188 
189  if (!info.crossedHcalRecHits.empty()) {
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  // Set some parameters
205  double minR = associator.volume().minR();
206  double minZ = associator.volume().minZ();
207  double maxR = associator.volume().maxR();
208  double maxZ = associator.volume().maxZ();
209 
210  // Define the TrackOrigin (where the propagation starts)
211  SteppingHelixStateInfo trackOrigin(*tkInnerState);
212 
213  // Define Propagator
215  prop->setMaterialMode(false);
216  prop->applyRadX0Correction(true);
217 
218  return propagateThoughFromIP(trackOrigin, prop, associator.volume(), 500, 0.1, minR, minZ, maxR, maxZ);
219 }
220 
221 int BetaCalculatorECAL::getDetailedTrackLengthInXtals(std::map<int, GlobalPoint>& trackExitPositionMap,
222  std::map<int, float>& trackCrossedXtalMap,
223  double& totalLengthCurved,
224  GlobalPoint& internalPointCurved,
225  GlobalPoint& externalPointCurved,
226  const CaloGeometry* theGeometry,
227  const CaloTopology* theTopology,
228  const std::vector<SteppingHelixStateInfo>& neckLace) {
229  GlobalPoint origin(0., 0., 0.);
230  internalPointCurved = origin;
231  externalPointCurved = origin;
232 
233  bool firstPoint = false;
234  trackCrossedXtalMap.clear();
235 
236  const CaloSubdetectorGeometry* theBarrelSubdetGeometry = theGeometry->getSubdetectorGeometry(DetId::Ecal, 1);
237  const CaloSubdetectorGeometry* theEndcapSubdetGeometry = theGeometry->getSubdetectorGeometry(DetId::Ecal, 2);
238 
239  for (std::vector<SteppingHelixStateInfo>::const_iterator itr = (neckLace.begin() + 1); itr != neckLace.end(); ++itr) {
240  GlobalPoint probe_gp = (*itr).position();
241  std::vector<DetId> surroundingMatrix;
242 
243  EBDetId closestBarrelDetIdToProbe = ((theBarrelSubdetGeometry->getClosestCell(probe_gp)).rawId());
244  EEDetId closestEndcapDetIdToProbe = ((theEndcapSubdetGeometry->getClosestCell(probe_gp)).rawId());
245 
246  // check if the probe is inside the xtal
247  if ((closestEndcapDetIdToProbe) && (theGeometry->getSubdetectorGeometry(closestEndcapDetIdToProbe)
248  ->getGeometry(closestEndcapDetIdToProbe)
249  ->inside(probe_gp))) {
250  double step = ((*itr).position() - (*(itr - 1)).position()).mag();
251  GlobalPoint point = itr->position();
253  trackExitPositionMap, trackCrossedXtalMap, closestEndcapDetIdToProbe, step, point, theEndcapSubdetGeometry);
254  totalLengthCurved += step;
255 
256  if (firstPoint == false) {
257  internalPointCurved = probe_gp;
258  firstPoint = true;
259  }
260 
261  externalPointCurved = probe_gp;
262  }
263 
264  if ((closestBarrelDetIdToProbe) && (theGeometry->getSubdetectorGeometry(closestBarrelDetIdToProbe)
265  ->getGeometry(closestBarrelDetIdToProbe)
266  ->inside(probe_gp))) {
267  double step = ((*itr).position() - (*(itr - 1)).position()).mag();
268  GlobalPoint point = itr->position();
270  trackExitPositionMap, trackCrossedXtalMap, closestBarrelDetIdToProbe, step, point, theBarrelSubdetGeometry);
271  totalLengthCurved += step;
272 
273  if (firstPoint == false) {
274  internalPointCurved = probe_gp;
275  firstPoint = true;
276  }
277 
278  externalPointCurved = probe_gp;
279  } else {
280  // 3x3 matrix surrounding the probe
281  surroundingMatrix =
282  theTopology->getSubdetectorTopology(closestBarrelDetIdToProbe)->getWindow(closestBarrelDetIdToProbe, 3, 3);
283 
284  for (unsigned int k = 0; k < surroundingMatrix.size(); ++k) {
285  if (theGeometry->getSubdetectorGeometry(surroundingMatrix.at(k))
286  ->getGeometry(surroundingMatrix.at(k))
287  ->inside(probe_gp)) {
288  double step = ((*itr).position() - (*(itr - 1)).position()).mag();
289  GlobalPoint point = itr->position();
290  addStepToXtal(trackExitPositionMap,
291  trackCrossedXtalMap,
292  surroundingMatrix[k],
293  step,
294  point,
295  theGeometry->getSubdetectorGeometry(surroundingMatrix.at(k)));
296  totalLengthCurved += step;
297 
298  if (firstPoint == false) {
299  internalPointCurved = probe_gp;
300  firstPoint = true;
301  }
302 
303  externalPointCurved = probe_gp;
304  }
305  }
306 
307  // clear neighborhood matrix
308  surroundingMatrix.clear();
309  }
310  }
311 
312  return 0;
313 }
314 
315 void BetaCalculatorECAL::addStepToXtal(std::map<int, GlobalPoint>& trackExitPositionMap,
316  std::map<int, float>& trackCrossedXtalMap,
317  DetId aDetId,
318  float step,
320  const CaloSubdetectorGeometry* theSubdetGeometry) {
321  auto cell_p = theSubdetGeometry->getGeometry(aDetId);
322  GlobalPoint p = cell_p->getPosition(23);
323  GlobalPoint diff(point.x() - p.x(), point.y() - p.y(), point.z() - p.z());
324 
325  std::map<int, GlobalPoint>::iterator xtal = trackExitPositionMap.find(aDetId.rawId());
326  if (xtal != trackExitPositionMap.end())
327  ((*xtal).second) = diff;
328  else
329  trackExitPositionMap.insert(std::pair<int, GlobalPoint>(aDetId.rawId(), diff));
330 
331  std::map<int, float>::iterator xtal2 = trackCrossedXtalMap.find(aDetId.rawId());
332  if (xtal2 != trackCrossedXtalMap.end())
333  ((*xtal2).second) += step;
334  else
335  trackCrossedXtalMap.insert(std::pair<int, float>(aDetId.rawId(), step));
336 }
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:34
GlobalPoint getPosition(const DetId &)
static const TGPicture * info(bool iBackgroundIsBlack)
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:26
std::vector< float > ecalEnergies
Definition: HSCPCaloInfo.h:27
bool isTimeErrorValid() const
Definition: EcalRecHit.h:85
std::vector< const HBHERecHit * > crossedHcalRecHits
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
BetaCalculatorECAL(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
static FreeTrajectoryState getFreeTrajectoryState(const edm::EventSetup &, const reco::Track &)
get FreeTrajectoryState from different track representations
double maxZ(bool withTolerance=true) const
std::vector< GlobalPoint > ecalTrackExitPositions
Definition: HSCPCaloInfo.h:34
void useDefaultPropagator()
use the default propagator
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
std::vector< EcalRecHit >::const_iterator const_iterator
T y() const
Definition: PV3DBase.h:60
void loadParameters(const edm::ParameterSet &, edm::ConsumesCollector &)
static double constexpr speedOfLight
Speed of light [cm / ns].
Definition: Constants.h:12
float time() const
Definition: EcalRecHit.h:70
void addInfoToCandidate(susybsm::HSCParticle &candidate, edm::Handle< reco::TrackCollection > &tracks, edm::Event &iEvent, const edm::EventSetup &iSetup, susybsm::HSCPCaloInfo &caloInfo)
double minZ(bool withTolerance=true) const
CaloGeometry const * getGeometry()
bool hasTrackRef() const
Definition: HSCParticle.h:50
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.h:83
std::vector< DetId > ecalDetIds
Definition: HSCPCaloInfo.h:33
std::vector< float > ecalTimeErrors
Definition: HSCPCaloInfo.h:29
std::vector< float > ecalTimes
Definition: HSCPCaloInfo.h:28
int iEvent
Definition: GenABIO.cc:224
edm::EDGetTokenT< EERecHitCollection > EERecHitCollectionToken_
void applyRadX0Correction(bool applyRadX0Correction)
T sqrt(T t)
Definition: SSEVec.h:19
float chi2() const
Definition: EcalRecHit.h:120
T z() const
Definition: PV3DBase.h:61
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
float energy() const
Definition: EcalRecHit.h:68
edm::ESHandle< DetIdAssociator > ecalDetIdAssociator_
double crossedEnergy(EnergyType)
energy in detector elements crossed by the track by types
float timeError() const
Definition: EcalRecHit.h:165
std::vector< float > ecalChi2s
Definition: HSCPCaloInfo.h:30
double minR(bool withTolerance=true) const
const_iterator end() const
TrackAssociatorParameters parameters_
virtual DetId getClosestCell(const GlobalPoint &r) const
Definition: DetId.h:17
double outerEta() const
pseudorapidity of the momentum vector at the outermost hit position
Definition: Track.h:127
virtual std::vector< DetId > getWindow(const DetId &id, const int &northSouthSize, const int &eastWestSize) const
virtual std::shared_ptr< const CaloCellGeometry > getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
Flags recoFlag() const
DEPRECATED provided for temporary backward compatibility.
Definition: EcalRecHit.h:205
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:17
reco::TrackRef trackRef() const
Definition: HSCParticle.h:64
TrackDetectorAssociator trackAssociator_
iterator find(key_type k)
HLT enums.
static int position[264][3]
Definition: ReadPGInfo.cc:289
T get() const
Definition: EventSetup.h:73
TrackDetMatchInfo associate(const edm::Event &, const edm::EventSetup &, const FreeTrajectoryState &, const AssociatorParameters &)
std::vector< float > ecalOutOfTimeChi2s
Definition: HSCPCaloInfo.h:31
const FiducialVolume & volume() const
get active detector volume
std::vector< SteppingHelixStateInfo > propagateThoughFromIP(const SteppingHelixStateInfo &state, const Propagator *prop, const FiducialVolume &volume, int nsteps, float step, float minR, float minZ, float maxR, float maxZ)
step
Definition: StallMonitor.cc:94
FreeTrajectoryState innerFreeState(const reco::Track &tk, const MagneticField *field, bool withErr=true)
edm::EDGetTokenT< EBRecHitCollection > EBRecHitCollectionToken_
edm::ESHandle< MagneticField > bField_
std::vector< float > ecalOutOfTimeEnergies
Definition: HSCPCaloInfo.h:32
T x() const
Definition: PV3DBase.h:59
T const * product() const
Definition: ESHandle.h:86
double outerPhi() const
azimuthal angle of the momentum vector at the outermost hit position
Definition: Track.h:124
edm::ESHandle< CaloGeometry > theCaloGeometry_
double maxR(bool withTolerance=true) const
void setMaterialMode(bool noMaterial)
Switch for material effects mode: no material effects if true.
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
*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
std::vector< SteppingHelixStateInfo > calcEcalDeposit(const FreeTrajectoryState *tkInnerState, const DetIdAssociator &associator)