CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FWPFCandidateWithHitsProxyBuilder.cc
Go to the documentation of this file.
1 #include "TEveBoxSet.h"
2 #include "TEveTrack.h"
3 #include "TEveTrackPropagator.h"
4 #include "TEveCompound.h"
5 #include "TEveStraightLineSet.h"
6 #include "TEveProjectionBases.h"
7 
15 
20 
22 
23 namespace {
24  const std::string cname("particleFlowRecHitHCALUpgrade");
25 
26  void addLineToLineSet(TEveStraightLineSet* ls, const float* p, int i1, int i2) {
27  i1 *= 3;
28  i2 *= 3;
29  ls->AddLine(p[i1], p[i1 + 1], p[i1 + 2], p[i2], p[i2 + 1], p[i2 + 2]);
30  }
31 
32  void addBoxAsLines(TEveStraightLineSet* lineset, const float* p) {
33  for (int l = 0; l < 5; l += 4) {
34  addLineToLineSet(lineset, p, 0 + l, 1 + l);
35  addLineToLineSet(lineset, p, 1 + l, 2 + l);
36  addLineToLineSet(lineset, p, 2 + l, 3 + l);
37  addLineToLineSet(lineset, p, 3 + l, 0 + l);
38  }
39  for (int l = 0; l < 4; ++l)
40  addLineToLineSet(lineset, p, 0 + l, 4 + l);
41  }
42 
43  void editLineInLineSet(TEveChunkManager::iterator& li, const float* p, int i1, int i2) {
44  TEveStraightLineSet::Line_t& line = *(TEveStraightLineSet::Line_t*)li();
45  i1 *= 3;
46  i2 *= 3;
47  for (int i = 0; i < 3; ++i) {
48  line.fV1[0 + i] = p[i1 + i];
49  line.fV2[0 + i] = p[i2 + i];
50  }
51 
52  li.next();
53  }
54 
55  void editBoxInLineSet(TEveChunkManager::iterator& li, const float* p) {
56  for (int i = 0; i < 5; i += 4) {
57  editLineInLineSet(li, p, 0 + i, 1 + i);
58 
59  editLineInLineSet(li, p, 1 + i, 2 + i);
60  editLineInLineSet(li, p, 2 + i, 3 + i);
61  editLineInLineSet(li, p, 3 + i, 0 + i);
62  }
63  for (int i = 0; i < 4; ++i)
64  editLineInLineSet(li, p, 0 + i, 4 + i);
65  }
66 } // namespace
67 
68 //______________________________________________________________________________
70  TEveElementList* product,
71  const FWViewContext* vc) {
72  // init PFCandiate collection
73  reco::PFCandidateCollection const* candidates = nullptr;
74  iItem->get(candidates);
75  if (candidates == nullptr)
76  return;
77 
78  Int_t idx = 0;
80  for (reco::PFCandidateCollection::const_iterator it = candidates->begin(), itEnd = candidates->end(); it != itEnd;
81  ++it, ++idx) {
82  TEveCompound* comp = createCompound();
83  setupAddElement(comp, product);
84  // printf("products size %d/%d \n", (int)iItem->size(), product->NumChildren());
85 
86  const reco::PFCandidate& cand = *it;
87 
88  // track
89  {
90  TEveRecTrack t;
91  t.fBeta = 1.;
92  t.fP = TEveVector(cand.px(), cand.py(), cand.pz());
93  t.fV = TEveVector(cand.vertex().x(), cand.vertex().y(), cand.vertex().z());
94  t.fSign = cand.charge();
95  TEveTrack* trk = new TEveTrack(&t, context().getTrackPropagator());
96  trk->MakeTrack();
97  fireworks::setTrackTypePF(cand, trk);
98  setupAddElement(trk, comp);
99  }
100  // hits
101  {
102  comp->SetMainColor(iItem->defaultDisplayProperties().color());
103  addHitsForCandidate(cand, comp, vc);
104  }
105  }
106 }
107 
108 //______________________________________________________________________________
110  // ref hcal collections
112 
113  m_collectionHCAL = nullptr;
114  try {
115  // edm::InputTag tag("particleFlowRecHitHCAL");
116  edm::InputTag tag(cname);
117  item()->getEvent()->getByLabel(tag, handle_hits);
118  if (handle_hits.isValid()) {
119  m_collectionHCAL = &*handle_hits;
120  } else {
121  fwLog(fwlog::kError) << "FWPFCandidateWithHitsProxyBuilder, item " << item()->name()
122  << ": Failed to access collection with name " << cname << "." << std::endl;
123  }
124  } catch (...) {
125  fwLog(fwlog::kError) << "FWPFCandidateWithHitsProxyBuilder, item " << item()->name()
126  << ": Failed to access collection with name " << cname << "." << std::endl;
127  }
128 }
129 
130 //______________________________________________________________________________
132  const float* corners, float scale, bool plotEt, std::vector<float>& scaledCorners, const reco::PFRecHit*) {
133  static TEveVector vtmp;
134  vtmp.Set(0.f, 0.f, 0.f);
135  for (unsigned int i = 0; i < 24; i += 3) {
136  vtmp[0] += corners[i];
137  vtmp[1] += corners[i + 1];
138  vtmp[2] += corners[i + 2];
139  }
140  vtmp *= 1.f / 8.f;
141 
142  if (plotEt) {
143  scale *= vtmp.Perp() / vtmp.Mag();
144  }
145 
146  // Coordinates for a scaled version of the original box
147  for (unsigned int i = 0; i < 24; i += 3) {
148  scaledCorners[i] = vtmp[0] + (corners[i] - vtmp[0]) * scale;
149  scaledCorners[i + 1] = vtmp[1] + (corners[i + 1] - vtmp[1]) * scale;
150  scaledCorners[i + 2] = vtmp[2] + (corners[i + 2] - vtmp[2]) * scale;
151  }
152 }
153 
154 //______________________________________________________________________________
156  for (reco::PFRecHitCollection::const_iterator it = m_collectionHCAL->begin(); it != m_collectionHCAL->end(); ++it) {
157  if (it->detId() == candIdx) {
158  return &(*it);
159  }
160  }
161  return nullptr;
162 }
163 
164 //______________________________________________________________________________
167  const FWViewContext* vc) {
168  std::vector<float> scaledCorners(24);
169 
170  float scale = vc->getEnergyScale()->getScaleFactor3D() / 50;
171  for (TEveElement::List_i i = parent->BeginChildren(); i != parent->EndChildren(); ++i) {
172  if ((*i)->NumChildren() > 1) {
173  TEveElement::List_i xx = (*i)->BeginChildren();
174  ++xx;
175  TEveBoxSet* boxset = dynamic_cast<TEveBoxSet*>(*xx);
176  ++xx;
177  TEveStraightLineSet* lineset = dynamic_cast<TEveStraightLineSet*>(*xx);
178  TEveChunkManager::iterator li(lineset->GetLinePlex());
179  li.next();
180 
181  TEveChunkManager* plex = boxset->GetPlex();
182  if (plex->N()) {
183  for (int atomIdx = 0; atomIdx < plex->Size(); ++atomIdx) {
184  TEveBoxSet::BFreeBox_t* atom = (TEveBoxSet::BFreeBox_t*)boxset->GetPlex()->Atom(atomIdx);
185  reco::PFRecHit* hit = (reco::PFRecHit*)boxset->GetUserData(atomIdx);
186  const float* corners = item()->getGeom()->getCorners(hit->detId());
187  viewContextBoxScale(corners, hit->energy() * scale, vc->getEnergyScale()->getPlotEt(), scaledCorners, hit);
188  memcpy(atom->fVertices, &scaledCorners[0], sizeof(atom->fVertices));
189 
190  editBoxInLineSet(li, &scaledCorners[0]);
191  }
192 
193  for (TEveProjectable::ProjList_i p = lineset->BeginProjecteds(); p != lineset->EndProjecteds(); ++p) {
194  TEveStraightLineSetProjected* projLineSet = (TEveStraightLineSetProjected*)(*p);
195  projLineSet->UpdateProjection();
196  }
197  }
198  }
199  }
200 }
201 //______________________________________________________________________________
202 namespace {
203  TString boxset_tooltip_callback(TEveDigitSet* ds, Int_t idx) {
204  void* ud = ds->GetUserData(idx);
205  if (ud) {
207  // printf("idx %d %p hit data %p\n", idx, (void*)hit, ud);
208  if (hit)
209  return TString::Format("RecHit %d energy '%f'", idx, hit->energy());
210  else
211  return "ERROR";
212  }
213  return "NULL";
214  }
215 } // namespace
216 //______________________________________________________________________________
218  TEveElement* holder,
219  const FWViewContext* vc) {
221 
222  TEveBoxSet* boxset = nullptr;
223  TEveStraightLineSet* lineset = nullptr;
224 
225  for (unsigned elIdx = 0; elIdx < eleInBlocks.size(); elIdx++) {
226  // unsigned ieTrack = 0;
227  // unsigned ieECAL = 0;
228  unsigned ieHCAL = 0;
229 
230  reco::PFBlockRef blockRef = eleInBlocks[elIdx].first;
231  unsigned indexInBlock = eleInBlocks[elIdx].second;
232  edm::Ptr<reco::PFBlock> myBlock(blockRef.id(), blockRef.get(), blockRef.key());
233  /*
234  if (myBlock->elements()[indexInBlock].type() == 1)
235  ieTrack = indexInBlock;
236  if (myBlock->elements()[indexInBlock].type() == 4)
237  ieECAL = indexInBlock;
238  */
239  if (myBlock->elements()[indexInBlock].type() == 5)
240  ieHCAL = indexInBlock;
241 
242  std::vector<float> scaledCorners(24);
243  float scale = vc->getEnergyScale()->getScaleFactor3D() / 50;
244  if (ieHCAL && m_collectionHCAL) {
245  reco::PFClusterRef hcalclusterRef = myBlock->elements()[ieHCAL].clusterRef();
246  edm::Ptr<reco::PFCluster> myCluster(hcalclusterRef.id(), hcalclusterRef.get(), hcalclusterRef.key());
247  if (myCluster.get()) {
248  const std::vector<std::pair<DetId, float> >& hitsandfracs = myCluster->hitsAndFractions();
249 
250  if (!boxset) {
251  boxset = new TEveBoxSet();
252  boxset->Reset(TEveBoxSet::kBT_FreeBox, true, hitsandfracs.size());
253  boxset->SetAntiFlick(true);
254  boxset->SetAlwaysSecSelect(true);
255  boxset->SetPickable(true);
256  boxset->SetTooltipCBFoo(boxset_tooltip_callback);
257  }
258 
259  if (!lineset) {
260  lineset = new TEveStraightLineSet();
261  }
262 
263  bool hitsFound = false;
264  for (int ihandf = 0, lastIdx = (int)(hitsandfracs.size()); ihandf < lastIdx; ihandf++) {
265  unsigned int hitDetId = hitsandfracs[ihandf].first;
266  const float* corners = context().getGeom()->getCorners(hitDetId);
267  const reco::PFRecHit* hit = getHitForDetId(hitDetId);
268  if (hit) {
269  viewContextBoxScale(corners, hit->energy() * scale, vc->getEnergyScale()->getPlotEt(), scaledCorners, hit);
270  boxset->AddBox(&scaledCorners[0]);
271  // setup last box
272  boxset->DigitColor(holder->GetMainColor());
273  boxset->DigitUserData((void*)hit);
274  addBoxAsLines(lineset, &scaledCorners[0]);
275  hitsFound = true;
276  }
277  /*
278  // AMT: don't add lines if hit is not found becuse of unconsistency of scaling.
279  else
280  {
281  addBoxAsLines(lineset, corners);
282  }
283  */
284  }
285  if (!hitsFound)
286  fwLog(fwlog::kWarning) << Form(
287  "Can't find matching hits with for HCAL block %d in %s collection. Number of hits %d.\n",
288  elIdx,
289  cname.c_str(),
290  (int)hitsandfracs.size());
291 
292  } else {
293  fwLog(fwlog::kInfo) << "empty cluster \n";
294  }
295  }
296  } // endloop cand.elementsInBlocks();
297 
298  if (boxset) {
299  boxset->RefitPlex();
300  setupAddElement(boxset, holder);
301  }
302 
303  if (lineset) {
304  setupAddElement(lineset, holder);
305  }
306 }
307 
310  "PFCandidatesWithHits",
const fireworks::Context & context() const
float getScaleFactor3D() const
const FWDisplayProperties & defaultDisplayProperties() const
Definition: FWEventItem.cc:403
#define REGISTER_FWPROXYBUILDER(_name_, _type_, _purpose_, _view_)
double pz() const final
z coordinate of momentum vector
static const int kAllRPZBits
Definition: FWViewType.h:67
unsigned detId() const
rechit detId
Definition: PFRecHit.h:93
const std::string & name() const
Definition: FWEventItem.cc:435
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
void get(const T *&oData) const
Definition: FWEventItem.h:78
FWViewEnergyScale * getEnergyScale() const
def ls
Definition: eostools.py:349
key_type key() const
Accessor for product key.
Definition: Ref.h:250
ProductID id() const
Accessor for product ID.
Definition: Ref.h:244
const FWEventItem * item() const
static const int kAll3DBits
Definition: FWViewType.h:68
Color_t color() const
std::vector< ElementInBlock > ElementsInBlocks
Definition: PFCandidate.h:404
double px() const final
x coordinate of momentum vector
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
void scaleProduct(TEveElementList *parent, FWViewType::EType type, const FWViewContext *vc) override
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
float energy() const
rechit energy
Definition: PFRecHit.h:99
double py() const final
y coordinate of momentum vector
bool isValid() const
Definition: HandleBase.h:70
const edm::EventBase * getEvent() const
Definition: FWEventItem.h:131
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
const float * getCorners(unsigned int id) const
Definition: FWGeometry.cc:461
#define fwLog(_level_)
Definition: fwLog.h:45
void viewContextBoxScale(const float *corners, float scale, bool plotEt, std::vector< float > &scaledCorners, const reco::PFRecHit *)
const reco::PFRecHitCollection * m_collectionHCAL
const Point & vertex() const override
vertex position (overwritten by PF...)
Definition: PFCandidate.cc:60
const reco::PFRecHit * getHitForDetId(unsigned detId)
void setTrackTypePF(const reco::PFCandidate &pfCand, TAttLine *track)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:92
TEveCompound * createCompound(bool set_color=true, bool propagate_color_to_all_children=false) const
void addHitsForCandidate(const reco::PFCandidate &c, TEveElement *holder, const FWViewContext *vc)
const ElementsInBlocks & elementsInBlocks() const
Definition: PFCandidate.cc:658
FWGeometry * getGeom() const
Definition: Context.h:72
const FWGeometry * getGeom() const
Definition: FWEventItem.cc:548
bool getPlotEt() const
int charge() const final
electric charge