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