CMS 3D CMS Logo

PiZeroAnalyzer.cc
Go to the documentation of this file.
1 #include <iostream>
2 
4 
5 //#define TWOPI 6.283185308
6 //
7 
18 using namespace std;
19 
21  : caloGeometryToken_{esConsumes()}, caloTopologyToken_{esConsumes()} {
22  fName_ = pset.getUntrackedParameter<std::string>("Name");
23  prescaleFactor_ = pset.getUntrackedParameter<int>("prescaleFactor", 1);
24 
25  barrelEcalHits_token_ = consumes<edm::SortedCollection<EcalRecHit, edm::StrictWeakOrdering<EcalRecHit> > >(
26  pset.getParameter<edm::InputTag>("barrelEcalHits"));
27  endcapEcalHits_token_ = consumes<edm::SortedCollection<EcalRecHit, edm::StrictWeakOrdering<EcalRecHit> > >(
28  pset.getParameter<edm::InputTag>("endcapEcalHits"));
29 
30  nEvt_ = 0;
31 
32  // parameters for Pizero finding
33  seleXtalMinEnergy_ = pset.getParameter<double>("seleXtalMinEnergy");
34  clusSeedThr_ = pset.getParameter<double>("clusSeedThr");
35  clusEtaSize_ = pset.getParameter<int>("clusEtaSize");
36  clusPhiSize_ = pset.getParameter<int>("clusPhiSize");
37  ParameterLogWeighted_ = pset.getParameter<bool>("ParameterLogWeighted");
38  ParameterX0_ = pset.getParameter<double>("ParameterX0");
39  ParameterT0_barl_ = pset.getParameter<double>("ParameterT0_barl");
40  ParameterW0_ = pset.getParameter<double>("ParameterW0");
41 
42  selePtGammaOne_ = pset.getParameter<double>("selePtGammaOne");
43  selePtGammaTwo_ = pset.getParameter<double>("selePtGammaTwo");
44  seleS4S9GammaOne_ = pset.getParameter<double>("seleS4S9GammaOne");
45  seleS4S9GammaTwo_ = pset.getParameter<double>("seleS4S9GammaTwo");
46  selePtPi0_ = pset.getParameter<double>("selePtPi0");
47  selePi0Iso_ = pset.getParameter<double>("selePi0Iso");
48  selePi0BeltDR_ = pset.getParameter<double>("selePi0BeltDR");
49  selePi0BeltDeta_ = pset.getParameter<double>("selePi0BeltDeta");
50  seleMinvMaxPi0_ = pset.getParameter<double>("seleMinvMaxPi0");
51  seleMinvMinPi0_ = pset.getParameter<double>("seleMinvMinPi0");
52 
53  posCalcParameters_ = pset.getParameter<edm::ParameterSet>("posCalcParameters");
54 }
55 
57 
59  edm::Run const& /* iRun */,
60  edm::EventSetup const& /* iSetup */) {
61  currentFolder_.str("");
62  currentFolder_ << "Egamma/PiZeroAnalyzer/";
63  ibooker.setCurrentFolder(currentFolder_.str());
64 
65  hMinvPi0EB_ = ibooker.book1D("Pi0InvmassEB", "Pi0 Invariant Mass in EB", 100, 0., 0.5);
66  hMinvPi0EB_->setAxisTitle("Inv Mass [GeV] ", 1);
67 
68  hPt1Pi0EB_ = ibooker.book1D("Pt1Pi0EB", "Pt 1st most energetic Pi0 photon in EB", 100, 0., 20.);
69  hPt1Pi0EB_->setAxisTitle("1st photon Pt [GeV] ", 1);
70 
71  hPt2Pi0EB_ = ibooker.book1D("Pt2Pi0EB", "Pt 2nd most energetic Pi0 photon in EB", 100, 0., 20.);
72  hPt2Pi0EB_->setAxisTitle("2nd photon Pt [GeV] ", 1);
73 
74  hPtPi0EB_ = ibooker.book1D("PtPi0EB", "Pi0 Pt in EB", 100, 0., 20.);
75  hPtPi0EB_->setAxisTitle("Pi0 Pt [GeV] ", 1);
76 
77  hIsoPi0EB_ = ibooker.book1D("IsoPi0EB", "Pi0 Iso in EB", 50, 0., 1.);
78  hIsoPi0EB_->setAxisTitle("Pi0 Iso", 1);
79 }
80 
82  using namespace edm;
83 
84  if (nEvt_ % prescaleFactor_)
85  return;
86  nEvt_++;
87  LogInfo("PiZeroAnalyzer") << "PiZeroAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_
88  << "\n";
89 
90  // Get EcalRecHits
91  bool validEcalRecHits = true;
92  Handle<EcalRecHitCollection> barrelHitHandle;
94  e.getByToken(barrelEcalHits_token_, barrelHitHandle);
95  if (!barrelHitHandle.isValid()) {
96  edm::LogError("PhotonProducer") << "Error! Can't get the product: barrelEcalHits_token_";
97  validEcalRecHits = false;
98  }
99 
100  Handle<EcalRecHitCollection> endcapHitHandle;
101  e.getByToken(endcapEcalHits_token_, endcapHitHandle);
103  if (!endcapHitHandle.isValid()) {
104  edm::LogError("PhotonProducer") << "Error! Can't get the product: endcapEcalHits_token_";
105  validEcalRecHits = false;
106  }
107 
108  if (validEcalRecHits)
109  makePizero(esup, barrelHitHandle, endcapHitHandle);
110 }
111 
115  const EcalRecHitCollection* hitCollection_p = rhEB.product();
116 
117  //edm::ESHandle<CaloGeometry> geoHandle;
118  //es.get<CaloGeometryRecord>().get(geoHandle);
119  auto geoHandle = es.getHandle(caloGeometryToken_);
120 
121  //edm::ESHandle<CaloTopology> theCaloTopology;
122  //es.get<CaloTopologyRecord>().get(theCaloTopology);
123  auto theCaloTopology = es.getHandle(caloTopologyToken_);
124 
125  const CaloSubdetectorTopology* topology_p;
126  const CaloSubdetectorGeometry* geometry_p = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalBarrel);
127  const CaloSubdetectorGeometry* geometryES_p = geoHandle->getSubdetectorGeometry(DetId::Ecal, EcalPreshower);
128 
129  // Parameters for the position calculation:
130  PositionCalc posCalculator_ = PositionCalc(posCalcParameters_);
131  //
132  std::map<DetId, EcalRecHit> recHitsEB_map;
133  //
134  std::vector<EcalRecHit> seeds;
135 
136  seeds.clear();
137  //
138  vector<EBDetId> usedXtals;
139  usedXtals.clear();
140  //
142  //
143  static const int MAXCLUS = 2000;
144  int nClus = 0;
145  vector<float> eClus;
146  vector<float> etClus;
147  vector<float> etaClus;
148  vector<float> phiClus;
149  vector<EBDetId> max_hit;
150  vector<vector<EcalRecHit> > RecHitsCluster;
151  vector<float> s4s9Clus;
152 
153  // find cluster seeds in EB
154  for (itb = rhEB->begin(); itb != rhEB->end(); ++itb) {
155  EBDetId id(itb->id());
156  double energy = itb->energy();
157  if (energy > seleXtalMinEnergy_) {
158  std::pair<DetId, EcalRecHit> map_entry(itb->id(), *itb);
159  recHitsEB_map.insert(map_entry);
160  }
161  if (energy > clusSeedThr_)
162  seeds.push_back(*itb);
163  } // Eb rechits
164 
165  sort(seeds.begin(), seeds.end(), [](auto& x, auto& y) { return (x.energy() > y.energy()); });
166  for (std::vector<EcalRecHit>::iterator itseed = seeds.begin(); itseed != seeds.end(); itseed++) {
167  EBDetId seed_id = itseed->id();
168  std::vector<EBDetId>::const_iterator usedIds;
169 
170  bool seedAlreadyUsed = false;
171  for (usedIds = usedXtals.begin(); usedIds != usedXtals.end(); usedIds++) {
172  if (*usedIds == seed_id) {
173  seedAlreadyUsed = true;
174  //cout<< " Seed with energy "<<itseed->energy()<<" was used !"<<endl;
175  break;
176  }
177  }
178  if (seedAlreadyUsed)
179  continue;
180  topology_p = theCaloTopology->getSubdetectorTopology(DetId::Ecal, EcalBarrel);
181  std::vector<DetId> clus_v = topology_p->getWindow(seed_id, clusEtaSize_, clusPhiSize_);
182  //std::vector<DetId> clus_used;
183  std::vector<std::pair<DetId, float> > clus_used;
184 
185  vector<EcalRecHit> RecHitsInWindow;
186 
187  double simple_energy = 0;
188 
189  for (std::vector<DetId>::iterator det = clus_v.begin(); det != clus_v.end(); det++) {
190  // EBDetId EBdet = *det;
191  // cout<<" det "<< EBdet<<" ieta "<<EBdet.ieta()<<" iphi "<<EBdet.iphi()<<endl;
192  bool HitAlreadyUsed = false;
193  for (usedIds = usedXtals.begin(); usedIds != usedXtals.end(); usedIds++) {
194  if (*usedIds == *det) {
195  HitAlreadyUsed = true;
196  break;
197  }
198  }
199  if (HitAlreadyUsed)
200  continue;
201  if (recHitsEB_map.find(*det) != recHitsEB_map.end()) {
202  // cout<<" Used det "<< EBdet<<endl;
203  std::map<DetId, EcalRecHit>::iterator aHit;
204  aHit = recHitsEB_map.find(*det);
205  usedXtals.push_back(*det);
206  RecHitsInWindow.push_back(aHit->second);
207  clus_used.push_back(std::pair<DetId, float>(*det, 1.));
208  simple_energy = simple_energy + aHit->second.energy();
209  }
210  }
211 
212  math::XYZPoint clus_pos = posCalculator_.Calculate_Location(clus_used, hitCollection_p, geometry_p, geometryES_p);
213  float theta_s = 2. * atan(exp(-clus_pos.eta()));
214  float p0x_s = simple_energy * sin(theta_s) * cos(clus_pos.phi());
215  float p0y_s = simple_energy * sin(theta_s) * sin(clus_pos.phi());
216  // float p0z_s = simple_energy * cos(theta_s);
217  float et_s = sqrt(p0x_s * p0x_s + p0y_s * p0y_s);
218 
219  //cout << " Simple Clustering: E,Et,px,py,pz: "<<simple_energy<<" "<<et_s<<" "<<p0x_s<<" "<<p0y_s<<" "<<endl;
220 
221  eClus.push_back(simple_energy);
222  etClus.push_back(et_s);
223  etaClus.push_back(clus_pos.eta());
224  phiClus.push_back(clus_pos.phi());
225  max_hit.push_back(seed_id);
226  RecHitsCluster.push_back(RecHitsInWindow);
227  //Compute S4/S9 variable
228  //We are not sure to have 9 RecHits so need to check eta and phi:
229  float s4s9_[4];
230  for (int i = 0; i < 4; i++)
231  s4s9_[i] = itseed->energy();
232  for (unsigned int j = 0; j < RecHitsInWindow.size(); j++) {
233  //cout << " Simple cluster rh, ieta, iphi : "<<((EBDetId)RecHitsInWindow[j].id()).ieta()<<" "<<((EBDetId)RecHitsInWindow[j].id()).iphi()<<endl;
234  if ((((EBDetId)RecHitsInWindow[j].id()).ieta() == seed_id.ieta() - 1 && seed_id.ieta() != 1) ||
235  (seed_id.ieta() == 1 && (((EBDetId)RecHitsInWindow[j].id()).ieta() == seed_id.ieta() - 2))) {
236  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() - 1 ||
237  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() - 1) {
238  s4s9_[0] += RecHitsInWindow[j].energy();
239  } else {
240  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi()) {
241  s4s9_[0] += RecHitsInWindow[j].energy();
242  s4s9_[1] += RecHitsInWindow[j].energy();
243  } else {
244  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() + 1 ||
245  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() + 1) {
246  s4s9_[1] += RecHitsInWindow[j].energy();
247  }
248  }
249  }
250  } else {
251  if (((EBDetId)RecHitsInWindow[j].id()).ieta() == seed_id.ieta()) {
252  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() - 1 ||
253  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() - 1) {
254  s4s9_[0] += RecHitsInWindow[j].energy();
255  s4s9_[3] += RecHitsInWindow[j].energy();
256  } else {
257  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() + 1 ||
258  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() + 1) {
259  s4s9_[1] += RecHitsInWindow[j].energy();
260  s4s9_[2] += RecHitsInWindow[j].energy();
261  }
262  }
263  } else {
264  if ((((EBDetId)RecHitsInWindow[j].id()).ieta() == seed_id.ieta() + 1 && seed_id.ieta() != -1) ||
265  (seed_id.ieta() == -1 && (((EBDetId)RecHitsInWindow[j].id()).ieta() == seed_id.ieta() + 2))) {
266  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() - 1 ||
267  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() - 1) {
268  s4s9_[3] += RecHitsInWindow[j].energy();
269  } else {
270  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi()) {
271  s4s9_[2] += RecHitsInWindow[j].energy();
272  s4s9_[3] += RecHitsInWindow[j].energy();
273  } else {
274  if (((EBDetId)RecHitsInWindow[j].id()).iphi() == seed_id.iphi() + 1 ||
275  ((EBDetId)RecHitsInWindow[j].id()).iphi() - 360 == seed_id.iphi() + 1) {
276  s4s9_[2] += RecHitsInWindow[j].energy();
277  }
278  }
279  }
280  } else {
281  cout << " (EBDetId)RecHitsInWindow[j].id()).ieta() " << ((EBDetId)RecHitsInWindow[j].id()).ieta()
282  << " seed_id.ieta() " << seed_id.ieta() << endl;
283  cout << " Problem with S4 calculation " << endl;
284  return;
285  }
286  }
287  }
288  }
289  s4s9Clus.push_back(*max_element(s4s9_, s4s9_ + 4) / simple_energy);
290  // cout<<" s4s9Clus[0] "<<s4s9_[0]/simple_energy<<" s4s9Clus[1] "<<s4s9_[1]/simple_energy<<" s4s9Clus[2] "<<s4s9_[2]/simple_energy<<" s4s9Clus[3] "<<s4s9_[3]/simple_energy<<endl;
291  // cout<<" Max "<<*max_element( s4s9_,s4s9_+4)/simple_energy<<endl;
292  nClus++;
293  if (nClus == MAXCLUS)
294  return;
295  } // End loop over seed clusters
296 
297  // cout<< " Pi0 clusters: "<<nClus<<endl;
298 
299  // Selection, based on Simple clustering
300  //pi0 candidates
301  static const int MAXPI0S = 200;
302  int npi0_s = 0;
303 
304  vector<EBDetId> scXtals;
305  scXtals.clear();
306 
307  if (nClus <= 1)
308  return;
309  for (Int_t i = 0; i < nClus; i++) {
310  for (Int_t j = i + 1; j < nClus; j++) {
311  // cout<<" i "<<i<<" etClus[i] "<<etClus[i]<<" j "<<j<<" etClus[j] "<<etClus[j]<<endl;
312  if (etClus[i] > selePtGammaOne_ && etClus[j] > selePtGammaTwo_ && s4s9Clus[i] > seleS4S9GammaOne_ &&
313  s4s9Clus[j] > seleS4S9GammaTwo_) {
314  float theta_0 = 2. * atan(exp(-etaClus[i]));
315  float theta_1 = 2. * atan(exp(-etaClus[j]));
316 
317  float p0x = eClus[i] * sin(theta_0) * cos(phiClus[i]);
318  float p1x = eClus[j] * sin(theta_1) * cos(phiClus[j]);
319  float p0y = eClus[i] * sin(theta_0) * sin(phiClus[i]);
320  float p1y = eClus[j] * sin(theta_1) * sin(phiClus[j]);
321  float p0z = eClus[i] * cos(theta_0);
322  float p1z = eClus[j] * cos(theta_1);
323 
324  float pt_pi0 = sqrt((p0x + p1x) * (p0x + p1x) + (p0y + p1y) * (p0y + p1y));
325  // cout<<" pt_pi0 "<<pt_pi0<<endl;
326  if (pt_pi0 < selePtPi0_)
327  continue;
328  float m_inv = sqrt((eClus[i] + eClus[j]) * (eClus[i] + eClus[j]) - (p0x + p1x) * (p0x + p1x) -
329  (p0y + p1y) * (p0y + p1y) - (p0z + p1z) * (p0z + p1z));
330  if ((m_inv < seleMinvMaxPi0_) && (m_inv > seleMinvMinPi0_)) {
331  //New Loop on cluster to measure isolation:
332  vector<int> IsoClus;
333  IsoClus.clear();
334  float Iso = 0;
335  TVector3 pi0vect = TVector3((p0x + p1x), (p0y + p1y), (p0z + p1z));
336  for (Int_t k = 0; k < nClus; k++) {
337  if (k == i || k == j)
338  continue;
339  TVector3 Clusvect = TVector3(eClus[k] * sin(2. * atan(exp(-etaClus[k]))) * cos(phiClus[k]),
340  eClus[k] * sin(2. * atan(exp(-etaClus[k]))) * sin(phiClus[k]),
341  eClus[k] * cos(2. * atan(exp(-etaClus[k]))));
342  float dretaclpi0 = fabs(etaClus[k] - pi0vect.Eta());
343  float drclpi0 = Clusvect.DeltaR(pi0vect);
344 
345  if ((drclpi0 < selePi0BeltDR_) && (dretaclpi0 < selePi0BeltDeta_)) {
346  Iso = Iso + etClus[k];
347  IsoClus.push_back(k);
348  }
349  }
350 
351  if (Iso / pt_pi0 < selePi0Iso_) {
352  hMinvPi0EB_->Fill(m_inv);
353  hPt1Pi0EB_->Fill(etClus[i]);
354  hPt2Pi0EB_->Fill(etClus[j]);
355  hPtPi0EB_->Fill(pt_pi0);
356  hIsoPi0EB_->Fill(Iso / pt_pi0);
357 
358  npi0_s++;
359  }
360 
361  if (npi0_s == MAXPI0S)
362  return;
363  }
364  }
365  }
366  }
367 }
double clusSeedThr_
parameters needed for pizero finding
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
double selePi0BeltDeta_
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
MonitorElement * hIsoPi0EB_
edm::EDGetTokenT< edm::SortedCollection< EcalRecHit, edm::StrictWeakOrdering< EcalRecHit > > > barrelEcalHits_token_
PiZeroAnalyzer(const edm::ParameterSet &)
void makePizero(const edm::EventSetup &es, const edm::Handle< EcalRecHitCollection > eb, const edm::Handle< EcalRecHitCollection > ee)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T const * product() const
Definition: Handle.h:70
MonitorElement * hPtPi0EB_
MonitorElement * hMinvPi0EB_
std::vector< EcalRecHit >::const_iterator const_iterator
double seleS4S9GammaOne_
double seleS4S9GammaTwo_
edm::ParameterSet posCalcParameters_
Log< level::Error, false > LogError
std::stringstream currentFolder_
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
void Fill(long long x)
const edm::ESGetToken< CaloTopology, CaloTopologyRecord > caloTopologyToken_
double seleMinvMaxPi0_
edm::EDGetTokenT< edm::SortedCollection< EcalRecHit, edm::StrictWeakOrdering< EcalRecHit > > > endcapEcalHits_token_
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > caloGeometryToken_
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
math::XYZPoint Calculate_Location(const HitsAndFractions &iDetIds, const edm::SortedCollection< HitType > *iRecHits, const CaloSubdetectorGeometry *iSubGeom, const CaloSubdetectorGeometry *iESGeom=nullptr)
Definition: PositionCalc.h:65
double selePtGammaOne_
double selePtGammaTwo_
const_iterator begin() const
virtual std::vector< DetId > getWindow(const DetId &id, const int &northSouthSize, const int &eastWestSize) const
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
void analyze(const edm::Event &, const edm::EventSetup &) override
const_iterator end() const
Log< level::Info, false > LogInfo
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
unsigned int prescaleFactor_
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
~PiZeroAnalyzer() override
MonitorElement * hPt2Pi0EB_
MonitorElement * hPt1Pi0EB_
double seleXtalMinEnergy_
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
double seleMinvMinPi0_
Definition: Run.h:45
virtual void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)