CMS 3D CMS Logo

EopTreeWriter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EopTreeWriter
4 // Class: EopTreeWriter
5 //
13 //
14 // Original Author: Holger Enderle
15 // Created: Thu Dec 4 11:22:48 CET 2008
16 // $Id: EopTreeWriter.cc,v 1.2 2011/11/30 07:45:28 mussgill Exp $
17 //
18 //
19 
20 // framework include files
30 
31 // user include files
53 
54 // ROOT includes
55 #include "TH1.h"
56 #include "TTree.h"
57 
58 //
59 // class decleration
60 //
61 
62 class EopTreeWriter : public edm::one::EDAnalyzer<edm::one::SharedResources> {
63 public:
64  explicit EopTreeWriter(const edm::ParameterSet&);
65  ~EopTreeWriter() override = default;
66 
68 
69 private:
70  void analyze(const edm::Event&, const edm::EventSetup&) override;
71  void endJob() override;
72  double getDistInCM(double eta1, double phi1, double eta2, double phi2);
73 
74  // ----------member data ---------------------------
77 
79  TTree* tree_;
83 
89 };
90 
91 //
92 // constructors and destructor
93 //
95  : src_(iConfig.getParameter<edm::InputTag>("src")), geometryToken_(esConsumes()) {
96  usesResource(TFileService::kSharedResource);
97  //now do what ever initialization is needed
98 
99  // TrackAssociator parameters
100  edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
103 
104  tree_ = fs_->make<TTree>("EopTree", "EopTree");
106  tree_->Branch("EopVariables", &treeMemPtr_); // address of pointer!
107 
108  // do the consumes
109  theTrackCollectionToken_ = consumes<reco::TrackCollection>(src_);
111  consumes<reco::IsolatedPixelTrackCandidateCollection>(edm::InputTag("IsoProd", "HcalIsolatedTrackCollection"));
112 
113  ecalRecHitTokenAlCaToken_ = consumes<EcalRecHitCollection>(edm::InputTag("IsoProd", "IsoTrackEcalRecHitCollection"));
114  ecalRecHitTokenEBRecoToken_ = consumes<EcalRecHitCollection>(edm::InputTag("ecalRecHit", "EcalRecHitsEB"));
115  ecalRecHitTokenEERecoToken_ = consumes<EcalRecHitCollection>(edm::InputTag("ecalRecHit", "EcalRecHitsEE"));
116 }
117 
118 //
119 // member functions
120 //
121 
122 // ------------ method called to for each event ------------
124  using namespace edm;
125 
126  // get geometry
127  const CaloGeometry* geo = &iSetup.getData(geometryToken_);
128 
129  // temporary collection of EB+EE recHits
130  std::unique_ptr<EcalRecHitCollection> tmpEcalRecHitCollection(new EcalRecHitCollection);
131  bool ecalInAlca = iEvent.getHandle(ecalRecHitTokenAlCaToken_).isValid();
132  bool ecalInReco =
133  iEvent.getHandle(ecalRecHitTokenEBRecoToken_) && iEvent.getHandle(ecalRecHitTokenEERecoToken_).isValid();
134 
135  std::vector<edm::EDGetTokenT<EcalRecHitCollection> > ecalTokens_;
136  if (ecalInAlca)
137  ecalTokens_.push_back(ecalRecHitTokenAlCaToken_);
138  else if (ecalInReco) {
139  ecalTokens_.push_back(ecalRecHitTokenEBRecoToken_);
140  ecalTokens_.push_back(ecalRecHitTokenEERecoToken_);
141  } else {
142  throw cms::Exception("MissingProduct", "can not find EcalRecHits");
143  }
144 
145  for (const auto& i : ecalTokens_) {
147  for (EcalRecHitCollection::const_iterator recHit = (*ec).begin(); recHit != (*ec).end(); ++recHit) {
148  tmpEcalRecHitCollection->push_back(*recHit);
149  }
150  }
151 
152  const auto& tracks = iEvent.get(theTrackCollectionToken_);
153 
155  bool pixelInAlca = isoPixelTracks.isValid();
156 
157  double trackemc1;
158  double trackemc3;
159  double trackemc5;
160  double trackhac1;
161  double trackhac3;
162  double trackhac5;
163  double maxPNearby;
164  double dist;
165  double EnergyIn;
166  double EnergyOut;
167 
168  parameters_.useMuon = false;
169 
170  if (pixelInAlca)
171  if (isoPixelTracks->empty())
172  return;
173 
174  for (const auto& track : tracks) {
175  bool noChargedTracks = true;
176 
177  if (track.p() < 9.)
178  continue;
179 
182  iEvent,
183  iSetup,
185  parameters_);
186 
187  trackemc1 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 0);
188  trackemc3 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 1);
189  trackemc5 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 2);
190  trackhac1 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 0);
191  trackhac3 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 1);
192  trackhac5 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 2);
193 
194  if (trackhac3 < 5.)
195  continue;
196 
197  double etaecal = info.trkGlobPosAtEcal.eta();
198  double phiecal = info.trkGlobPosAtEcal.phi();
199 
200  maxPNearby = -10;
201  dist = 50;
202  for (const auto& track1 : tracks) {
203  if (&track == &track1) {
204  continue;
205  }
207  double etaecal1 = info1.trkGlobPosAtEcal.eta();
208  double phiecal1 = info1.trkGlobPosAtEcal.phi();
209 
210  if (etaecal1 == 0 && phiecal1 == 0)
211  continue;
212 
213  double ecDist = getDistInCM(etaecal, phiecal, etaecal1, phiecal1);
214 
215  if (ecDist < 40.) {
216  //calculate maximum P and sum P near seed track
217  if (track1.p() > maxPNearby) {
218  maxPNearby = track1.p();
219  dist = ecDist;
220  }
221 
222  //apply loose isolation criteria
223  if (track1.p() > 5.) {
224  noChargedTracks = false;
225  break;
226  }
227  }
228  }
229  EnergyIn = 0;
230  EnergyOut = 0;
231  if (noChargedTracks) {
232  for (std::vector<EcalRecHit>::const_iterator ehit = tmpEcalRecHitCollection->begin();
233  ehit != tmpEcalRecHitCollection->end();
234  ehit++) {
236  // R-scheme of ECAL CLUSTERIZATION
237  const GlobalPoint& posH = geo->getPosition((*ehit).detid());
238  double phihit = posH.phi();
239  double etahit = posH.eta();
240 
241  double dHitCM = getDistInCM(etaecal, phiecal, etahit, phihit);
242 
243  if (dHitCM < 9.0) {
244  EnergyIn += ehit->energy();
245  }
246  if (dHitCM > 15.0 && dHitCM < 35.0) {
247  EnergyOut += ehit->energy();
248  }
249  }
250 
251  treeMemPtr_->fillVariables(track.charge(),
252  track.innerOk(),
253  track.outerRadius(),
254  track.numberOfValidHits(),
255  track.numberOfLostHits(),
256  track.chi2(),
257  track.normalizedChi2(),
258  track.p(),
259  track.pt(),
260  track.ptError(),
261  track.theta(),
262  track.eta(),
263  track.phi(),
264  trackemc1,
265  trackemc3,
266  trackemc5,
267  trackhac1,
268  trackhac3,
269  trackhac5,
270  maxPNearby,
271  dist,
272  EnergyIn,
273  EnergyOut);
274 
275  tree_->Fill();
276  }
277  }
278 }
279 
280 // ------------ method called once each job just after ending the event loop ------------
282  delete treeMemPtr_;
283  treeMemPtr_ = nullptr;
284 }
285 
286 //*************************************************************
287 double EopTreeWriter::getDistInCM(double eta1, double phi1, double eta2, double phi2) {
288  //*************************************************************
289 
290  static constexpr float EEBoundary = 1.479; // psedo-rapidity
291  static constexpr float EBRadius = 129; // in cm
292  static constexpr float EEIPdis = 317; // in cm
293 
294  double deltaPhi = phi1 - phi2;
295  while (deltaPhi > M_PI)
296  deltaPhi -= 2 * M_PI;
297  while (deltaPhi <= -M_PI)
298  deltaPhi += 2 * M_PI;
299  double dR;
300  // double Rec;
301  double theta1 = 2 * atan(exp(-eta1));
302  double theta2 = 2 * atan(exp(-eta2));
303  double cotantheta1;
304  if (cos(theta1) == 0)
305  cotantheta1 = 0;
306  else
307  cotantheta1 = 1 / tan(theta1);
308  double cotantheta2;
309  if (cos(theta2) == 0)
310  cotantheta2 = 0;
311  else
312  cotantheta2 = 1 / tan(theta2);
313  // if (fabs(eta1)<1.479) Rec=129; //radius of ECAL barrel
314  // else Rec=317; //distance from IP to ECAL endcap
315  //|vect| times tg of acos(scalar product)
316  // dR=fabs((Rec/sin(theta1))*tan(acos(sin(theta1)*sin(theta2)*(sin(phi1)*sin(phi2)+cos(phi1)*cos(phi2))+cos(theta1)*cos(theta2))));
317 
318  if (fabs(eta1) < EEBoundary) {
319  dR = EBRadius * sqrt((cotantheta1 - cotantheta2) * (cotantheta1 - cotantheta2) + deltaPhi * deltaPhi);
320  } else {
321  dR = EEIPdis *
322  sqrt(tan(theta1) * tan(theta1) + tan(theta2) * tan(theta2) - 2 * tan(theta1) * tan(theta2) * cos(deltaPhi));
323  }
324  return dR;
325 }
326 
327 //*************************************************************
329 //*************************************************************
330 {
332  desc.setComment("Generate tree for Tracker Alignment E/p validation");
333  desc.add<edm::InputTag>("src", edm::InputTag("generalTracks"));
334 
335  // track association
337  psd0.add<bool>("accountForTrajectoryChangeCalo", false);
338  psd0.add<bool>("propagateAllDirections", true);
339  psd0.add<bool>("truthMatch", false);
340  psd0.add<bool>("useCalo", false);
341  psd0.add<bool>("useEcal", true);
342  psd0.add<bool>("useGEM", false);
343  psd0.add<bool>("useHO", false);
344  psd0.add<bool>("useHcal", true);
345  psd0.add<bool>("useME0", false);
346  psd0.add<bool>("useMuon", true);
347  psd0.add<bool>("usePreshower", false);
348  psd0.add<double>("dREcal", 9999.0);
349  psd0.add<double>("dREcalPreselection", 0.05);
350  psd0.add<double>("dRHcal", 9999.0);
351  psd0.add<double>("dRHcalPreselection", 0.2);
352  psd0.add<double>("dRMuon", 9999.0);
353  psd0.add<double>("dRMuonPreselection", 0.2);
354  psd0.add<double>("dRPreshowerPreselection", 0.2);
355  psd0.add<double>("muonMaxDistanceSigmaX", 0.0);
356  psd0.add<double>("muonMaxDistanceSigmaY", 0.0);
357  psd0.add<double>("muonMaxDistanceX", 5.0);
358  psd0.add<double>("muonMaxDistanceY", 5.0);
359  psd0.add<double>("trajectoryUncertaintyTolerance", -1.0);
360  psd0.add<edm::InputTag>("CSCSegmentCollectionLabel", edm::InputTag("cscSegments"));
361  psd0.add<edm::InputTag>("CaloTowerCollectionLabel", edm::InputTag("towerMaker"));
362  psd0.add<edm::InputTag>("DTRecSegment4DCollectionLabel", edm::InputTag("dt4DSegments"));
363  psd0.add<edm::InputTag>("EBRecHitCollectionLabel", edm::InputTag("IsoProd", "IsoTrackEcalRecHitCollection"));
364  psd0.add<edm::InputTag>("EERecHitCollectionLabel", edm::InputTag("IsoProd", "IsoTrackEcalRecHitCollection"));
365  psd0.add<edm::InputTag>("GEMSegmentCollectionLabel", edm::InputTag("gemSegments"));
366  psd0.add<edm::InputTag>("HBHERecHitCollectionLabel", edm::InputTag("IsoProd", "IsoTrackHBHERecHitCollection"));
367  psd0.add<edm::InputTag>("HORecHitCollectionLabel", edm::InputTag("IsoProd", "IsoTrackHORecHitCollection"));
368  psd0.add<edm::InputTag>("ME0SegmentCollectionLabel", edm::InputTag("me0Segments"));
369  desc.add<edm::ParameterSetDescription>("TrackAssociatorParameters", psd0);
370 
371  descriptions.addWithDefaultLabel(desc);
372 }
373 
374 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static const TGPicture * info(bool iBackgroundIsBlack)
container to hold data to be written into TTree
Definition: EopVariables.h:8
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T eta() const
Definition: PV3DBase.h:73
void useDefaultPropagator()
use the default propagator
void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< EcalRecHit >::const_iterator const_iterator
~EopTreeWriter() override=default
void loadParameters(const edm::ParameterSet &, edm::ConsumesCollector &)
edm::InputTag src_
edm::EDGetTokenT< reco::IsolatedPixelTrackCandidateCollection > isoPixelTkToken_
int iEvent
Definition: GenABIO.cc:224
GlobalPoint getPosition(const DetId &id) const
Get the position of a given detector id.
Definition: CaloGeometry.cc:50
edm::EDGetTokenT< EcalRecHitCollection > ecalRecHitTokenEBRecoToken_
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > bFieldToken
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
TrackAssociatorParameters parameters_
void endJob() override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
edm::Service< TFileService > fs_
void fillVariables(Int_t charge, Int_t innerOk, Double_t outerRadius, Int_t numberOfValidHits, Int_t numberOfLostHits, Double_t chi2, Double_t normalizedChi2, Double_t p, Double_t pt, Double_t ptError, Double_t theta, Double_t eta, Double_t phi, Double_t emc1, Double_t emc3, Double_t emc5, Double_t hac1, Double_t hac3, Double_t hac5, Double_t maxPNearby, Double_t dist, Double_t EnergyIn, Double_t EnergyOut)
fill variables into tree
Definition: EopVariables.h:22
ParameterDescriptionBase * add(U const &iLabel, T const &value)
#define M_PI
TrackDetectorAssociator trackAssociator_
static void fillDescriptions(edm::ConfigurationDescriptions &)
auto const & tracks
cannot be loose
edm::EDGetTokenT< EcalRecHitCollection > ecalRecHitTokenAlCaToken_
edm::EDGetTokenT< reco::TrackCollection > theTrackCollectionToken_
static FreeTrajectoryState getFreeTrajectoryState(const MagneticField *, const reco::Track &)
get FreeTrajectoryState from different track representations
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
double getDistInCM(double eta1, double phi1, double eta2, double phi2)
TrackDetMatchInfo associate(const edm::Event &, const edm::EventSetup &, const FreeTrajectoryState &, const AssociatorParameters &)
edm::EDGetTokenT< EcalRecHitCollection > ecalRecHitTokenEERecoToken_
edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geometryToken_
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
math::XYZPoint trkGlobPosAtEcal
Track position at different parts of the calorimeter.
EopVariables * treeMemPtr_
EopTreeWriter(const edm::ParameterSet &)