CMS 3D CMS Logo

FWECALDetailViewBuilder.cc
Go to the documentation of this file.
1 // FIXME - needed to set fixed eta-phi limits. Without the
2 // visible area may change widely depending on energy
3 // deposition availability
4 
5 #include "TEveCaloData.h"
6 #include "TEveViewer.h"
7 #include "TEvePointSet.h"
8 #include "TEveCalo.h"
9 #include "TEveCompound.h"
10 #include "TAxis.h"
11 #include "TMath.h"
12 #include "THLimitsFinder.h"
13 #include "TLatex.h"
14 
22 
26 
27 #include "TGeoMatrix.h"
28 #include "TEveTrans.h"
29 
30 #include <utility>
31 
33  const edm::EventBase *event, const FWGeometry *geom, float eta, float phi, int size, Color_t defaultColor)
34 
35  : m_event(event),
36  m_geom(geom),
37  m_eta(eta),
38  m_phi(phi),
39  m_size(size),
40  m_defaultColor(defaultColor),
41  m_towerList(nullptr) {}
42 
44  // get the hits from the event
45 
46  // data
47  TEveCaloDataVec *data = new TEveCaloDataVec(1);
48  data->SetWrapTwoPi(false);
49  data->RefSliceInfo(0).Setup("hits (not clustered)", 0.0, m_defaultColor);
50 
51  fillData(data);
52 
53  // axis
54  float etaMin = m_eta - sizeRad();
55  float etaMax = m_eta + sizeRad();
56  float phiMin = m_phi - sizeRad();
57  float phiMax = m_phi + sizeRad();
58 
59  data->AddTower(m_eta - sizeRad(), m_eta + sizeRad(), m_phi - sizeRad(), m_phi + sizeRad());
60 
61  data->FillSlice(0, 0.1);
62 
63  TAxis *eta_axis = nullptr;
64  TAxis *phi_axis = nullptr;
65 
66  // printf("data rng %f %f %f %f\n",etaMin, etaMax, phiMin, phiMax );
67  std::vector<double> etaBinsWithinLimits;
68  etaBinsWithinLimits.push_back(etaMin);
69  for (unsigned int i = 0; i < 83; ++i)
71  etaBinsWithinLimits.push_back(fw3dlego::xbins[i]);
72  etaBinsWithinLimits.push_back(etaMax);
73 
74  std::vector<double> phiBinsWithinLimits;
75  phiBinsWithinLimits.push_back(phiMin);
76  for (double phi = -M_PI; phi < M_PI; phi += M_PI / 36)
77  if (phi > phiMin && phi < phiMax)
78  phiBinsWithinLimits.push_back(phi);
79  phiBinsWithinLimits.push_back(phiMax);
80 
81  eta_axis = new TAxis((int)etaBinsWithinLimits.size() - 1, &etaBinsWithinLimits[0]);
82  phi_axis = new TAxis((int)phiBinsWithinLimits.size() - 1, &phiBinsWithinLimits[0]);
83 
84  eta_axis->SetTitleFont(122);
85  eta_axis->SetTitle("h");
86  eta_axis->SetTitleSize(0.07);
87  phi_axis->SetTitleFont(122);
88  phi_axis->SetTitle("f");
89  phi_axis->SetTitleSize(0.07);
90 
91  eta_axis->SetNdivisions(510);
92  phi_axis->SetNdivisions(510);
93  data->SetEtaBins(eta_axis);
94  data->SetPhiBins(phi_axis);
95  return data;
96 }
97 
98 //_______________________________________________________________
100  // axis
101  float etaMin = m_eta - sizeRad();
102  float etaMax = m_eta + sizeRad();
103  float phiMin = m_phi - sizeRad();
104  float phiMax = m_phi + sizeRad();
105 
106  m_towerList = new TEveElementList("TowerHolder");
107  TEveCaloData *data = buildCaloData(true);
108 
109  // lego
110  TEveCaloLego *lego = new TEveCaloLego();
111  lego->SetData(data);
112  lego->AddElement(m_towerList);
113  lego->SetAutoRange(false);
114  lego->SetDrawNumberCellPixels(100);
115  // scale and translate to real world coordinates
116  lego->SetEta(etaMin, etaMax);
117  lego->SetPhiWithRng((phiMin + phiMax) * 0.5, (phiMax - phiMin) * 0.5); // phi range = 2* phiOffset
118  Double_t legoScale = sizeRad() * 2;
119  lego->InitMainTrans();
120  lego->RefMainTrans().SetScale(legoScale, legoScale, legoScale * 0.5);
121  lego->RefMainTrans().SetPos(m_eta, m_phi, -0.01);
122  lego->SetAutoRebin(kFALSE);
123  lego->SetName("ECALDetail Lego");
124 
125  // cut & paste from FWLegoViewBase
126  lego->SetScaleAbs(true);
127  lego->SetHasFixedHeightIn2DMode(true);
128  lego->SetFixedHeightValIn2DMode(0.001);
129 
130  TEvePointSet *ps = new TEvePointSet("origin");
131  ps->SetNextPoint(m_eta, m_phi, 0.01);
132  ps->SetMarkerSize(0.05);
133  ps->SetMarkerStyle(2);
134  ps->SetMainColor(kGreen);
135  ps->SetMarkerColor(kGreen);
136  lego->AddElement(ps);
137 
138  return lego;
139 }
140 
141 void FWECALDetailViewBuilder::setColor(Color_t color, const std::vector<DetId> &detIds) {
142  for (size_t i = 0; i < detIds.size(); ++i)
143  m_detIdsToColor[detIds[i]] = color;
144 }
145 
147  std::vector<DetId> clusterDetIds;
148  const std::vector<std::pair<DetId, float> > &hitsAndFractions = cluster.hitsAndFractions();
149  for (size_t j = 0; j < hitsAndFractions.size(); ++j) {
150  clusterDetIds.push_back(hitsAndFractions[j].first);
151  }
152 
153  setColor(color, clusterDetIds);
154 }
155 
156 void FWECALDetailViewBuilder::showSuperClusters(Color_t color1, Color_t color2) {
157  // get the superclusters from the event
159 
160  if (fabs(m_eta) < 1.5) {
161  try {
162  m_event->getByLabel(edm::InputTag("correctedHybridSuperClusters"), collection);
163  } catch (...) {
164  fwLog(fwlog::kWarning) << "no barrel superclusters are available" << std::endl;
165  }
166  } else {
167  try {
168  m_event->getByLabel(edm::InputTag("correctedMulti5x5SuperClustersWithPreshower"), collection);
169  } catch (...) {
170  fwLog(fwlog::kWarning) << "no endcap superclusters are available" << std::endl;
171  }
172  }
173  if (collection.isValid()) {
174  unsigned int colorIndex = 0;
175  // sort clusters in eta so neighboring clusters have distinct colors
176  reco::SuperClusterCollection sorted = *collection.product();
177  std::sort(sorted.begin(), sorted.end(), superClusterEtaLess);
178  for (size_t i = 0; i < sorted.size(); ++i) {
179  if (!(fabs(sorted[i].eta() - m_eta) < sizeRad() && fabs(sorted[i].phi() - m_phi) < sizeRad()))
180  continue;
181 
182  if (colorIndex % 2 == 0)
183  showSuperCluster(sorted[i], color1);
184  else
185  showSuperCluster(sorted[i], color2);
186  ++colorIndex;
187  }
188  }
189 }
190 
191 namespace {
192  float calculateEt(const TEveVector &centre, float e) {
193  TEveVector vec = centre;
194  float et;
195 
196  vec.Normalize();
197  vec *= e;
198  et = vec.Perp();
199 
200  return et;
201  }
202 
203 } // namespace
204 //------------------------------------------------------------------
206  // printf("filletaphi \n");
207  const float area = sizeRad(); // barrel cell range, AMT this is available in context
208 
209  double eta1 = m_eta - area;
210  double eta2 = m_eta + area;
211  double phi1 = m_phi - area;
212  double phi2 = m_phi + area;
213 
214  std::vector<FWBoxRecHit *> boxes;
215  for (EcalRecHitCollection::const_iterator hitIt = hits->begin(); hitIt != hits->end(); ++hitIt) {
216  const float *corners = m_geom->getCorners(hitIt->detid());
217  float energy, et;
218  std::vector<TEveVector> etaphiCorners(8);
219 
220  if (corners == nullptr)
221  continue;
222 
223  for (int i = 0; i < 4; ++i) {
224  TEveVector cv = TEveVector(corners[i * 3], corners[i * 3 + 1], corners[i * 3 + 2]);
225  etaphiCorners[i].fX = cv.Eta(); // Conversion of rechit X/Y values for plotting in Eta/Phi
226  etaphiCorners[i].fY = cv.Phi();
227  etaphiCorners[i].fZ = 0.0;
228 
229  etaphiCorners[i + 4].fX =
230  etaphiCorners[i].fX; // Top can simply be plotted exactly over the top of the bottom face
231  etaphiCorners[i + 4].fY = etaphiCorners[i].fY;
232  etaphiCorners[i + 4].fZ = 0.001;
233  // printf("%f %f %d \n", etaphiCorners[i].fX, etaphiCorners[i].fY, i);
234  }
235 
236  TEveVector center;
237  for (int i = 0; i < 4; ++i)
238  center += etaphiCorners[i];
239  center *= 1.f / 4.f;
240 
241  if (center.fX < eta1 || center.fX > eta2)
242  continue;
243  if (center.fY < phi1 || center.fY > phi2)
244  continue;
245 
246  // Stop phi wrap
247  float dPhi1 = etaphiCorners[2].fY - etaphiCorners[1].fY;
248  float dPhi2 = etaphiCorners[3].fY - etaphiCorners[0].fY;
249  float dPhi3 = etaphiCorners[1].fY - etaphiCorners[2].fY;
250  float dPhi4 = etaphiCorners[0].fY - etaphiCorners[3].fY;
251 
252  if (dPhi1 > 1)
253  etaphiCorners[2].fY = etaphiCorners[2].fY - (2 * TMath::Pi());
254  if (dPhi2 > 1)
255  etaphiCorners[3].fY = etaphiCorners[3].fY - (2 * TMath::Pi());
256  if (dPhi3 > 1)
257  etaphiCorners[2].fY = etaphiCorners[2].fY + (2 * TMath::Pi());
258  if (dPhi4 > 1)
259  etaphiCorners[3].fY = etaphiCorners[3].fY + (2 * TMath::Pi());
260 
261  energy = hitIt->energy();
262  et = calculateEt(center, energy);
263  Color_t bcolor = m_defaultColor;
264  std::map<DetId, int>::const_iterator itr = m_detIdsToColor.find(hitIt->id());
265  if (itr != m_detIdsToColor.end())
266  bcolor = itr->second;
267 
268  m_boxes.push_back(new FWBoxRecHit(etaphiCorners, m_towerList, energy, et));
269  TEveElement::List_i pIt = m_boxes.back()->getTower()->BeginParents();
270  TEveCompound *comp = dynamic_cast<TEveCompound *>(*pIt);
271  comp->SetMainColor(bcolor);
272  m_boxes.back()->getTower()->SetPickable(true);
273  m_boxes.back()->getTower()->SetElementTitle(Form("rawId = %d, et = %f", hitIt->id().rawId(), et));
274  } // loop hits
275 }
276 
277 //---------------------------------------------------------------------------------------
278 
279 void FWECALDetailViewBuilder::fillData(TEveCaloDataVec *data) {
280  { // barrel
281  const EcalRecHitCollection *hitsEB = nullptr;
282  edm::Handle<EcalRecHitCollection> handle_hitsEB;
283 
284  // RECO
285  try {
286  edm::InputTag tag("ecalRecHit", "EcalRecHitsEB");
287  m_event->getByLabel(tag, handle_hitsEB);
288  if (handle_hitsEB.isValid()) {
289  hitsEB = &*handle_hitsEB;
290  }
291  } catch (...) {
292  fwLog(fwlog::kWarning) << "FWECALDetailViewBuilder::fillData():: Failed to access EcalRecHitsEB collection."
293  << std::endl;
294  }
295 
296  // AOD
297  if (!handle_hitsEB.isValid()) {
298  try {
299  edm::InputTag tag("reducedEcalRecHitsEB");
300  m_event->getByLabel(tag, handle_hitsEB);
301  if (handle_hitsEB.isValid()) {
302  hitsEB = &*handle_hitsEB;
303  }
304 
305  } catch (...) {
307  << "FWECALDetailViewBuilder::filData():: Failed to access reducedEcalRecHitsEB collection." << std::endl;
308  }
309  }
310 
311  // MINIAOD
312  if (!handle_hitsEB.isValid()) {
313  try {
314  edm::InputTag tag("reducedEgamma", "reducedEBRecHits");
315  m_event->getByLabel(tag, handle_hitsEB);
316  if (handle_hitsEB.isValid()) {
317  hitsEB = &*handle_hitsEB;
318  }
319 
320  } catch (...) {
321  fwLog(fwlog::kWarning) << "FWECALDetailViewBuilder::filData():: Failed to access reducedEgamma collection."
322  << std::endl;
323  }
324  }
325 
326  if (handle_hitsEB.isValid()) {
327  fillEtaPhi(hitsEB, data);
328  }
329  }
330 
331  { // endcap
332 
333  const EcalRecHitCollection *hitsEE = nullptr;
334  edm::Handle<EcalRecHitCollection> handle_hitsEE;
335 
336  // RECO
337  try {
338  edm::InputTag tag("ecalRecHit", "EcalRecHitsEE");
339  m_event->getByLabel(tag, handle_hitsEE);
340  if (handle_hitsEE.isValid())
341  hitsEE = &*handle_hitsEE;
342  } catch (...) {
343  fwLog(fwlog::kWarning) << "FWECALDetailViewBuilder::fillData():: Failed to access ecalRecHitsEE collection."
344  << std::endl;
345  }
346 
347  // AOD
348  if (!handle_hitsEE.isValid()) {
349  try {
350  edm::InputTag tag("reducedEcalRecHitsEE");
351  m_event->getByLabel(tag, handle_hitsEE);
352  if (handle_hitsEE.isValid()) {
353  hitsEE = &*handle_hitsEE;
354  }
355 
356  } catch (...) {
358  << "FWECALDetailViewBuilder::fillData():: Failed to access reducedEcalRecHitsEE collection." << std::endl;
359  }
360 
361  // MINIAOD
362  if (!handle_hitsEE.isValid()) {
363  try {
364  edm::InputTag tag("reducedEgamma", "reducedEERecHits");
365  m_event->getByLabel(tag, handle_hitsEE);
366  if (handle_hitsEE.isValid()) {
367  hitsEE = &*handle_hitsEE;
368  }
369 
370  } catch (...) {
372  << "FWECALDetailViewBuilder::fillData():: Failed to access reducedEcalRecHitsEE collection." << std::endl;
373  }
374  }
375  }
376 
377  if (handle_hitsEE.isValid()) {
378  fillEtaPhi(hitsEE, data);
379  }
380  }
381 
382  if (m_boxes.empty())
383  return;
384 
385  bool plotEt = true;
386  float maxEnergy = 0;
387  int maxEnergyIdx = 0;
388  // get max energy in EE and EB
389 
390  int cnt = 0;
391  for (auto &i : m_boxes) {
392  if (i->getEnergy(plotEt) > maxEnergy) {
393  maxEnergy = i->getEnergy(plotEt);
394  maxEnergyIdx = cnt;
395  }
396  cnt++;
397  }
398 
399  m_boxes[maxEnergyIdx]->setIsTallest();
400 
401  // AMT ... max size can be an external parameter
402  float scale = 0.3 / maxEnergy;
403  for (auto &i : m_boxes) {
404  i->updateScale(scale, log(maxEnergy + 1), plotEt);
405  i->getTower()->SetDrawFrame(true);
406  }
407  data->DataChanged();
408 }
409 
411  double x0, double y0, Color_t clustered1, Color_t clustered2, Color_t supercluster) {
412  Double_t fontsize = 0.07;
413  TLatex *latex = new TLatex();
414  Double_t x = x0;
415  Double_t y = y0;
416  Double_t boxH = 0.25 * fontsize;
417  Double_t yStep = 0.04;
418 
419  y -= yStep;
420  latex->DrawLatex(x, y, "Energy types:");
421  y -= yStep;
422 
423  Double_t pos[4];
424  pos[0] = x + 0.05;
425  pos[2] = x + 0.20;
426 
427  pos[1] = y;
428  pos[3] = pos[1] + boxH;
430  latex->DrawLatex(x + 0.25, y, "unclustered");
431  y -= yStep;
432  if (clustered1 < 0)
433  return y;
434 
435  pos[1] = y;
436  pos[3] = pos[1] + boxH;
438  latex->DrawLatex(x + 0.25, y, "clustered");
439  y -= yStep;
440  if (clustered2 < 0)
441  return y;
442 
443  pos[1] = y;
444  pos[3] = pos[1] + boxH;
446  latex->DrawLatex(x + 0.25, y, "clustered");
447  y -= yStep;
448  if (supercluster < 0)
449  return y;
450 
451  pos[1] = y;
452  pos[3] = pos[1] + boxH;
453  FWDetailViewBase::drawCanvasBox(pos, supercluster);
454  latex->DrawLatex(x + 0.25, y, "super-cluster");
455  y -= yStep;
456 
457  return y;
458 }
459 //______________________________________________________________________________
460 
462  float rs = m_size * TMath::DegToRad();
463  return rs;
464 }
size
Write out results.
const double Pi
void showSuperCluster(const reco::SuperCluster &cluster, Color_t color=kYellow)
const std::vector< std::pair< DetId, float > > & hitsAndFractions() const
Definition: CaloCluster.h:210
const double xbins[]
double makeLegend(double x0=0.02, double y0=0.95, Color_t clustered1=kGreen+1, Color_t clustered2=kTeal, Color_t supercluster=kYellow)
void fillData(TEveCaloDataVec *data)
std::vector< EcalRecHit >::const_iterator const_iterator
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:92
void fillEtaPhi(const EcalRecHitCollection *hits, TEveCaloDataVec *data)
cv
Definition: cuy.py:363
const edm::EventBase * m_event
std::map< DetId, int > m_detIdsToColor
FWECALDetailViewBuilder(const edm::EventBase *event, const FWGeometry *geom, float eta, float phi, int size=50, Color_t defaultColor=kMagenta+1)
const float * getCorners(unsigned int id) const
Definition: FWGeometry.cc:461
std::vector< SuperCluster > SuperClusterCollection
collection of SuperCluser objectr
TEveCaloData * buildCaloData(bool xyEE)
float calculateEt(const TEveVector &centre, float e)
Definition: FWPFMaths.cc:115
#define M_PI
void showSuperClusters(Color_t color1=kGreen+2, Color_t color2=kTeal)
#define fwLog(_level_)
Definition: fwLog.h:45
void setColor(Color_t color, const std::vector< DetId > &detIds)
bool isValid() const
Definition: HandleBase.h:70
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static bool superClusterEtaLess(const reco::CaloCluster &lhs, const reco::CaloCluster &rhs)
std::vector< FWBoxRecHit * > m_boxes
Definition: event.py:1
static void drawCanvasBox(Double_t *pos, Color_t fillCol, Int_t fillType=0, bool bg=kTRUE)