CMS 3D CMS Logo

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