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 
18 
23 
25 
26 namespace
27 {
28  const static std::string cname("particleFlowRecHitHCALUpgrade");
29 
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 void
82 FWPFCandidateWithHitsProxyBuilder::build(const FWEventItem* iItem, TEveElementList* product, const FWViewContext* vc)
83 {
84  // init PFCandiate collection
85  reco::PFCandidateCollection const * candidates = 0;
86  iItem->get( candidates );
87  if( candidates == 0 ) return;
88 
89 
90  Int_t idx = 0;
92  for( reco::PFCandidateCollection::const_iterator it = candidates->begin(), itEnd = candidates->end(); it != itEnd; ++it, ++idx)
93  {
94  TEveCompound* comp = createCompound();
95  setupAddElement( comp, product );
96  // printf("products size %d/%d \n", (int)iItem->size(), product->NumChildren());
97 
98  const reco::PFCandidate& cand = *it;
99 
100  // track
101  {
102  TEveRecTrack t;
103  t.fBeta = 1.;
104  t.fP = TEveVector( cand.px(), cand.py(), cand.pz() );
105  t.fV = TEveVector( cand.vertex().x(), cand.vertex().y(), cand.vertex().z() );
106  t.fSign = cand.charge();
107  TEveTrack* trk = new TEveTrack(&t, context().getTrackPropagator() );
108  trk->MakeTrack();
109  fireworks::setTrackTypePF( cand, trk );
110  setupAddElement( trk, comp);
111  }
112  // hits
113  {
114  comp->SetMainColor(iItem->defaultDisplayProperties().color());
115  addHitsForCandidate(cand, comp, vc);
116  }
117 
118  }
119 }
120 
121 //______________________________________________________________________________
123 {
124  // ref hcal collections
126 
127 
128  m_collectionHCAL =0;
129  try
130  {
131  // edm::InputTag tag("particleFlowRecHitHCAL");
132  edm::InputTag tag(cname);
133  item()->getEvent()->getByLabel(tag, handle_hits);
134  if (handle_hits.isValid())
135  {
136  m_collectionHCAL = &*handle_hits;
137  }
138  else
139  {
140  fwLog(fwlog::kError) <<"FWPFCandidateWithHitsProxyBuilder, item " << item()->name() <<": Failed to access collection with name " << cname << "." << std::endl;
141  }
142  }
143  catch (...)
144  {
145  fwLog(fwlog::kError) <<"FWPFCandidateWithHitsProxyBuilder, item " << item()->name() <<": Failed to access collection with name " << cname << "." << std::endl;
146  }
147 }
148 
149 //______________________________________________________________________________
150 void FWPFCandidateWithHitsProxyBuilder::viewContextBoxScale( const float* corners, float scale, bool plotEt, std::vector<float>& scaledCorners, const reco::PFRecHit*)
151 {
152  static TEveVector vtmp;
153  vtmp.Set(0.f, 0.f, 0.f);
154  for( unsigned int i = 0; i < 24; i += 3 )
155  {
156  vtmp[0] += corners[i];
157  vtmp[1] += corners[i + 1];
158  vtmp[2] += corners[i + 2];
159  }
160  vtmp *= 1.f/8.f;
161 
162  if (plotEt)
163  {
164  scale *= vtmp.Perp()/vtmp.Mag();
165  }
166 
167  // Coordinates for a scaled version of the original box
168  for( unsigned int i = 0; i < 24; i += 3 )
169  {
170  scaledCorners[i] = vtmp[0] + ( corners[i] - vtmp[0] ) * scale;
171  scaledCorners[i + 1] = vtmp[1] + ( corners[i + 1] - vtmp[1] ) * scale;
172  scaledCorners[i + 2] = vtmp[2] + ( corners[i + 2] - vtmp[2] ) * scale;
173  }
174 }
175 
176 //______________________________________________________________________________
178 {
179 
180  for (reco::PFRecHitCollection::const_iterator it = m_collectionHCAL->begin(); it != m_collectionHCAL->end(); ++it)
181  {
182 
183  if ( it->detId() == candIdx)
184  {
185  return &(*it);
186  }
187  }
188  return 0;
189 }
190 
191 //______________________________________________________________________________
193 {
194  std::vector<float> scaledCorners(24);
195 
196  float scale = vc->getEnergyScale()->getScaleFactor3D()/50;
197  for (TEveElement::List_i i=parent->BeginChildren(); i!=parent->EndChildren(); ++i)
198  {
199  if ((*i)->NumChildren() > 1)
200  {
201  TEveElement::List_i xx = (*i)->BeginChildren(); ++xx;
202  TEveBoxSet* boxset = dynamic_cast<TEveBoxSet*>(*xx);
203  ++xx;
204  TEveStraightLineSet* lineset = dynamic_cast<TEveStraightLineSet*>(*xx);
205  TEveChunkManager::iterator li(lineset->GetLinePlex());
206  li.next();
207 
208 
209  TEveChunkManager* plex = boxset->GetPlex();
210  if (plex->N())
211  {
212  for (int atomIdx=0; atomIdx < plex->Size(); ++atomIdx)
213  {
214 
215  TEveBoxSet::BFreeBox_t* atom = (TEveBoxSet::BFreeBox_t*)boxset->GetPlex()->Atom(atomIdx);
216  reco::PFRecHit* hit = (reco::PFRecHit*)boxset->GetUserData(atomIdx);
217  const float* corners = item()->getGeom()->getCorners(hit->detId());
218  viewContextBoxScale(corners, hit->energy()*scale, vc->getEnergyScale()->getPlotEt(), scaledCorners, hit);
219  memcpy(atom->fVertices, &scaledCorners[0], sizeof(atom->fVertices));
220 
221  editBoxInLineSet(li, &scaledCorners[0]);
222  }
223 
224  for (TEveProjectable::ProjList_i p = lineset->BeginProjecteds(); p != lineset->EndProjecteds(); ++p)
225  {
226  TEveStraightLineSetProjected* projLineSet = (TEveStraightLineSetProjected*)(*p);
227  projLineSet->UpdateProjection();
228  }
229  }
230  }
231  }
232 }
233 //______________________________________________________________________________
234 namespace {
235 TString boxset_tooltip_callback(TEveDigitSet* ds, Int_t idx)
236 {
237  void* ud = ds->GetUserData(idx);
238  if (ud);
239  {
241  // printf("idx %d %p hit data %p\n", idx, (void*)hit, ud);
242  if (hit)
243  return TString::Format("RecHit %d energy '%f'", idx, hit->energy());
244  else
245  return "ERROR";
246  }
247 }
248 }
249 //______________________________________________________________________________
251 {
253 
254  TEveBoxSet* boxset = 0;
255  TEveStraightLineSet* lineset = 0;
256 
257  for(unsigned elIdx=0; elIdx<eleInBlocks.size(); elIdx++)
258  {
259  // unsigned ieTrack = 0;
260  // unsigned ieECAL = 0;
261  unsigned ieHCAL = 0;
262 
263  reco::PFBlockRef blockRef = eleInBlocks[elIdx].first;
264  unsigned indexInBlock = eleInBlocks[elIdx].second;
265  edm::Ptr<reco::PFBlock> myBlock(blockRef.id(),blockRef.get(), blockRef.key());
266  /*
267  if (myBlock->elements()[indexInBlock].type() == 1)
268  ieTrack = indexInBlock;
269  if (myBlock->elements()[indexInBlock].type() == 4)
270  ieECAL = indexInBlock;
271  */
272  if (myBlock->elements()[indexInBlock].type() == 5)
273  ieHCAL = indexInBlock;
274 
275 
276  std::vector<float> scaledCorners(24);
277  float scale = vc->getEnergyScale()->getScaleFactor3D()/50;
278  if (ieHCAL && m_collectionHCAL) {
279  reco::PFClusterRef hcalclusterRef=myBlock->elements()[ieHCAL].clusterRef();
280  edm::Ptr<reco::PFCluster> myCluster(hcalclusterRef.id(),hcalclusterRef.get(), hcalclusterRef.key());
281  if (myCluster.get())
282  {
283  const std::vector< std::pair<DetId, float> > & hitsandfracs = myCluster->hitsAndFractions();
284 
285  if (!boxset)
286  {
287  boxset = new TEveBoxSet();
288  boxset->Reset(TEveBoxSet::kBT_FreeBox, true, hitsandfracs.size());
289  boxset->SetAntiFlick(true);
290  boxset->SetAlwaysSecSelect(1);
291  boxset->SetPickable(1);
292  boxset->SetTooltipCBFoo(boxset_tooltip_callback);
293  }
294 
295  if (!lineset)
296  {
297  lineset = new TEveStraightLineSet();
298  }
299 
300  bool hitsFound = false;
301  for ( int ihandf=0, lastIdx=(int)(hitsandfracs.size()); ihandf<lastIdx; ihandf++)
302  {
303  unsigned int hitDetId = hitsandfracs[ihandf].first;
304  const float* corners = context().getGeom()->getCorners(hitDetId);
305  const reco::PFRecHit* hit = getHitForDetId(hitDetId);
306  if (hit)
307  {
308  viewContextBoxScale( corners, hit->energy()*scale, vc->getEnergyScale()->getPlotEt(), scaledCorners, hit);
309  boxset->AddBox( &scaledCorners[0]);
310  // setup last box
311  boxset->DigitColor(holder->GetMainColor());
312  boxset->DigitUserData((void*)hit);
313  addBoxAsLines(lineset, &scaledCorners[0]);
314  hitsFound = true;
315  }
316  /*
317  // AMT: don't add lines if hit is not found becuse of unconsistency of scaling.
318  else
319  {
320  addBoxAsLines(lineset, corners);
321  }
322  */
323  }
324  if (!hitsFound)
325  fwLog(fwlog::kWarning) << Form("Can't find matching hits with for HCAL block %d in %s collection. Number of hits %d.\n", elIdx, cname.c_str(), (int)hitsandfracs.size());
326 
327 
328  }
329  else
330  {
331  fwLog(fwlog::kInfo) << "empty cluster \n";
332  }
333  }
334  } // endloop cand.elementsInBlocks();
335 
336 
337  if (boxset) {
338  boxset->RefitPlex();
339  setupAddElement(boxset, holder);
340  }
341 
342  if (lineset) {
343  setupAddElement(lineset, holder);
344  }
345 }
346 
type
Definition: HCALResponse.h:21
const fireworks::Context & context() const
float getScaleFactor3D() const
int i
Definition: DBlmapReader.cc:9
const FWDisplayProperties & defaultDisplayProperties() const
Definition: FWEventItem.cc:451
#define REGISTER_FWPROXYBUILDER(_name_, _type_, _purpose_, _view_)
list parent
Definition: dbtoconf.py:74
static const int kAllRPZBits
Definition: FWViewType.h:58
unsigned detId() const
rechit detId
Definition: PFRecHit.h:101
const std::string & name() const
Definition: FWEventItem.cc:500
const FWGeometry * getGeom() const
Definition: Context.h:83
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
void get(const T *&oData) const
Definition: FWEventItem.h:84
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:368
const FWEventItem * item() const
static const int kAll3DBits
Definition: FWViewType.h:59
Color_t color() const
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:35
virtual double px() const GCC11_FINAL
x coordinate of momentum vector
virtual const Point & vertex() const
vertex position (overwritten by PF...)
Definition: PFCandidate.cc:643
double f[11][100]
bool isValid() const
Definition: HandleBase.h:76
const edm::EventBase * getEvent() const
Definition: FWEventItem.h:146
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:50
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:38
bool getByLabel(InputTag const &, Handle< T > &) const
Definition: EventBase.h:87
double energy() const
rechit energy
Definition: PFRecHit.h:107
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 ElementsInBlocks & elementsInBlocks() const
Definition: PFCandidate.cc:675
const FWGeometry * getGeom() const
Definition: FWEventItem.cc:681
bool getPlotEt() const