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