]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Reve/PointSet.cxx
Dummy methods DefineParticle required by the interface added.
[u/mrichter/AliRoot.git] / EVE / Reve / PointSet.cxx
1 // $Header$
2
3 #include "PointSet.h"
4
5 #include <Reve/ReveManager.h>
6 #include <Reve/NLTProjector.h>
7
8 #include <TTree.h>
9 #include <TTreePlayer.h>
10 #include <TF3.h>
11
12 #include <TColor.h>
13 #include <TCanvas.h>
14
15 using namespace Reve;
16
17 //______________________________________________________________________
18 // PointSet
19 //
20 // PointSet is a render-element holding a collection of 3D points with
21 // optional per-point TRef and an arbitrary number of integer ids (to
22 // be used for signal, volume-id, track-id, etc).
23 //
24 // 3D point representation is implemented in base-class TPolyMarker3D.
25 // Per-point TRef is implemented in base-class TPointSet3D.
26 //
27 // By using the TPointSelector the points and integer ids can be
28 // filled directly from a TTree holding the source data.
29 // Setting of per-point TRef's is not supported.
30 //
31 // PointSet is a NLTProjectable: it can be projected by using the
32 // NLTProjector class.
33
34 ClassImp(PointSet)
35
36 //______________________________________________________________________________
37 PointSet::PointSet(Int_t n_points, TreeVarType_e tv_type) :
38   RenderElement(fMarkerColor),
39   TPointSet3D(n_points),
40   TPointSelectorConsumer(tv_type),
41
42   fTitle          (),
43   fIntIds         (0),
44   fIntIdsPerPoint (0)
45 {
46   // Constructor.
47
48   fMarkerStyle = 20;
49 }
50
51 //______________________________________________________________________________
52 PointSet::PointSet(const Text_t* name, Int_t n_points, TreeVarType_e tv_type) :
53   RenderElement(fMarkerColor),
54   TPointSet3D(n_points),
55   TPointSelectorConsumer(tv_type),
56
57   fTitle          (),
58   fIntIds         (0),
59   fIntIdsPerPoint (0)
60 {
61   // Constructor.
62
63   fMarkerStyle = 20;
64   SetName(name);
65 }
66
67 //______________________________________________________________________________
68 PointSet::~PointSet()
69 {
70   // Destructor.
71
72   delete fIntIds;
73 }
74
75 /**************************************************************************/
76
77 //______________________________________________________________________________
78 void PointSet::ComputeBBox()
79 {
80   // Override of virtual method from TAttBBox.
81
82   TPointSet3D::ComputeBBox();
83   AssertBBoxExtents(0.1);
84 }
85
86 //______________________________________________________________________________
87 void PointSet::Reset(Int_t n_points, Int_t n_int_ids)
88 {
89   // Drop all data and set-up the data structures to recive new data.
90   // n_points   specifies the initial size of the arrays.
91   // n_int_ids  specifies the number of integer ids per point.
92
93   delete [] fP; fP = 0;
94   fN = n_points;
95   if(fN) fP = new Float_t [3*fN];
96   memset(fP, 0, 3*fN*sizeof(Float_t));
97   fLastPoint = -1;
98   ClearIds();
99   delete fIntIds; fIntIds = 0;
100   fIntIdsPerPoint = n_int_ids;
101   if (fIntIdsPerPoint > 0) fIntIds = new TArrayI(fIntIdsPerPoint*fN);
102   ResetBBox();
103 }
104
105 //______________________________________________________________________________
106 Int_t PointSet::GrowFor(Int_t n_points)
107 {
108   // Resizes internal array to allow additional n_points to be stored.
109   // Returns the old size which is also the location where one can
110   // start storing new data.
111   // The caller is *obliged* to fill the new point slots.
112
113   Int_t old_size = Size();
114   Int_t new_size = old_size + n_points;
115   SetPoint(new_size - 1, 0, 0, 0);
116   if (fIntIds)
117     fIntIds->Set(fIntIdsPerPoint * new_size);
118   return old_size;
119 }
120
121 /**************************************************************************/
122
123 //______________________________________________________________________________
124 inline void PointSet::AssertIntIdsSize()
125 {
126   // Assert that size of IntId array is compatible with the size of
127   // the point array.
128
129   Int_t exp_size = GetN()*fIntIdsPerPoint;
130   if (fIntIds->GetSize() < exp_size)
131     fIntIds->Set(exp_size);
132 }
133
134 //______________________________________________________________________________
135 Int_t* PointSet::GetPointIntIds(Int_t p) const
136 {
137   // Return a pointer to integer ids of point with index p.
138   // Existence of integer id array is checked, 0 is returned if it
139   // does not exist.
140   // Validity of p is *not* checked.
141
142   if (fIntIds)
143     return fIntIds->GetArray() + p*fIntIdsPerPoint;
144   return 0;
145 }
146
147 //______________________________________________________________________________
148 Int_t PointSet::GetPointIntId(Int_t p, Int_t i) const
149 {
150   // Return i-th integer id of point with index p.
151   // Existence of integer id array is checked, kMinInt is returned if
152   // it does not exist.
153   // Validity of p and i is *not* checked.
154
155   if (fIntIds)
156     return * (fIntIds->GetArray() + p*fIntIdsPerPoint + i);
157   return kMinInt;
158 }
159
160 //______________________________________________________________________________
161 void PointSet::SetPointIntIds(Int_t* ids)
162 {
163   // Set integer ids for the last point that was registerd (most
164   // probably via TPolyMarker3D::SetNextPoint(x,y,z)).
165
166   SetPointIntIds(fLastPoint, ids);
167 }
168
169 //______________________________________________________________________________
170 void PointSet::SetPointIntIds(Int_t n, Int_t* ids)
171 {
172   // Set integer ids for point with index n.
173
174   if (!fIntIds) return;
175   AssertIntIdsSize();
176   Int_t* x = fIntIds->GetArray() + n*fIntIdsPerPoint;
177   for (Int_t i=0; i<fIntIdsPerPoint; ++i)
178     x[i] = ids[i];
179 }
180
181 /**************************************************************************/
182
183 //______________________________________________________________________________
184 void PointSet::SetRnrElNameTitle(const Text_t* name, const Text_t* title)
185 {
186   // Set name and title of point-set.
187   // Virtual in RenderElement.
188
189   SetName(name);
190   SetTitle(title);
191 }
192
193 /******************************************************************************/
194
195 //______________________________________________________________________________
196 void PointSet::Paint(Option_t* option)
197 {
198   // Paint point-set.
199
200   if(fRnrSelf == kFALSE) return;
201
202   TPointSet3D::Paint(option);
203 }
204
205 /**************************************************************************/
206
207 //______________________________________________________________________________
208 void PointSet::InitFill(Int_t subIdNum)
209 {
210   // Initialize point-set for new filling.
211   // subIdNum gives the number of integer ids that can be assigned to
212   // each point.
213
214   if (subIdNum > 0) {
215     fIntIdsPerPoint = subIdNum;
216     if (!fIntIds)
217       fIntIds = new TArrayI(fIntIdsPerPoint*GetN());
218     else
219       fIntIds->Set(fIntIdsPerPoint*GetN());
220   } else {
221     delete fIntIds; fIntIds = 0;
222     fIntIdsPerPoint = 0;
223   }
224 }
225
226 //______________________________________________________________________________
227 void PointSet::TakeAction(TPointSelector* sel)
228 {
229   // Called from TPointSelector when internal arrays of the tree-selector
230   // are filled up and need to be processed.
231   // Virtual from TPointSelectorConsumer.
232
233   static const Exc_t eH("PointSet::TakeAction ");
234
235   if(sel == 0)
236     throw(eH + "selector is <null>.");
237
238   Int_t    n = sel->GetNfill();
239   Int_t  beg = GrowFor(n);
240
241   // printf("PointSet::TakeAction beg=%d n=%d size=%d nsubid=%d dim=%d\n",
242   //        beg, n, Size(), sel->GetSubIdNum(), sel->GetDimension());
243
244   Double_t *vx = sel->GetV1(), *vy = sel->GetV2(), *vz = sel->GetV3();
245   Float_t  *p  = fP + 3*beg;
246
247   switch(fSourceCS) {
248   case TVT_XYZ:
249     while(n-- > 0) {
250       p[0] = *vx; p[1] = *vy; p[2] = *vz;
251       p += 3;
252       ++vx; ++vy; ++vz;
253     }
254     break;
255   case TVT_RPhiZ:
256     while(n-- > 0) {
257       p[0] = *vx * TMath::Cos(*vy); p[1] = *vx * TMath::Sin(*vy); p[2] = *vz;
258       p += 3;
259       ++vx; ++vy; ++vz;
260     }
261     break;
262   default:
263     throw(eH + "unknown tree variable type.");
264   }
265
266   if (fIntIds) {
267     Double_t** subarr = new Double_t* [fIntIdsPerPoint];
268     for (Int_t i=0; i<fIntIdsPerPoint; ++i) {
269       subarr[i] = sel->GetVal(sel->GetDimension() - fIntIdsPerPoint + i);
270       if (subarr[i] == 0)
271         throw(eH + "sub-id array not available.");
272     }
273     Int_t* ids = fIntIds->GetArray() + fIntIdsPerPoint*beg;
274     n = sel->GetNfill();
275     while (n-- > 0) {
276       for (Int_t i=0; i<fIntIdsPerPoint; ++i) {
277         ids[i] = TMath::Nint(*subarr[i]);
278         ++subarr[i];
279       }
280       ids += fIntIdsPerPoint;
281     }
282     delete [] subarr;
283   }
284 }
285
286 /**************************************************************************/
287
288 //______________________________________________________________________________
289 TClass* PointSet::ProjectedClass() const
290 {
291   // Virtual from NLTProjectable, returns NLTPointSet class.
292
293   return NLTPointSet::Class();
294 }
295
296
297 /**************************************************************************/
298 /**************************************************************************/
299
300 //______________________________________________________________________________
301 // PointSetArray
302 //
303 // An array of point-sets with each point-set playing a role of a bin
304 // in a histogram. When a new point is added to a PointSetArray, an
305 // additional separating quantity needs to be specified: it determines
306 // into which PointSet (bin) the point will actually be stored.
307 //
308 // By using the TPointSelector the points and the separating
309 // quantities can be filled directly from a TTree holding the source
310 // data.
311 // Setting of per-point TRef's is not supported.
312 //
313 // After the filling, the range of separating variable can be
314 // controlled with a slider to choose a sub-set of PointSets that are
315 // actually shown.
316 //
317
318 ClassImp(PointSetArray)
319
320 //______________________________________________________________________________
321 PointSetArray::PointSetArray(const Text_t* name,
322                              const Text_t* title) :
323   RenderElement(fMarkerColor),
324   TNamed(name, title),
325
326   fBins(0), fDefPointSetCapacity(128), fNBins(0), fLastBin(-1),
327   fMin(0), fCurMin(0), fMax(0), fCurMax(0),
328   fBinWidth(0),
329   fQuantName()
330 {
331   // Constructor.
332 }
333
334 //______________________________________________________________________________
335 PointSetArray::~PointSetArray()
336 {
337   // Destructor: deletes the fBins array. Actual removal of
338   // elements done by RenderElement.
339
340   // printf("PointSetArray::~PointSetArray()\n");
341   delete [] fBins; fBins = 0;
342 }
343
344 //______________________________________________________________________________
345 void PointSetArray::Paint(Option_t* option)
346 {
347   // Paint the subjugated PointSet's.
348
349   if (fRnrSelf) {
350     for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
351       if ((*i)->GetRnrSelf())
352         (*i)->GetObject()->Paint(option);
353     }
354   }
355 }
356
357 //______________________________________________________________________________
358 void PointSetArray::RemoveElementLocal(RenderElement* el)
359 {
360   // Virtual from RenderElement, provide bin management.
361
362   for (Int_t i=0; i<fNBins; ++i) {
363     if (fBins[i] == el) {
364       fBins[i] = 0;
365       break;
366     }
367   }
368 }
369
370 //______________________________________________________________________________
371 void PointSetArray::RemoveElementsLocal()
372 {
373   // Virtual from RenderElement, provide bin management.
374
375   delete [] fBins; fBins = 0; fLastBin = -1;
376 }
377
378 /**************************************************************************/
379
380 //______________________________________________________________________________
381 void PointSetArray::SetMarkerColor(Color_t tcolor)
382 {
383   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
384     TAttMarker* m = dynamic_cast<TAttMarker*>((*i)->GetObject());
385     if (m && m->GetMarkerColor() == fMarkerColor)
386       m->SetMarkerColor(tcolor);
387   }
388   TAttMarker::SetMarkerColor(tcolor);
389 }
390
391 //______________________________________________________________________________
392 void PointSetArray::SetMarkerStyle(Style_t mstyle)
393 {
394   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
395     TAttMarker* m = dynamic_cast<TAttMarker*>((*i)->GetObject());
396     if (m && m->GetMarkerStyle() == fMarkerStyle)
397       m->SetMarkerStyle(mstyle);
398   }
399   TAttMarker::SetMarkerStyle(mstyle);
400 }
401
402 //______________________________________________________________________________
403 void PointSetArray::SetMarkerSize(Size_t msize)
404 {
405   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
406     TAttMarker* m = dynamic_cast<TAttMarker*>((*i)->GetObject());
407     if (m && m->GetMarkerSize() == fMarkerSize)
408       m->SetMarkerSize(msize);
409   }
410   TAttMarker::SetMarkerSize(msize);
411 }
412
413 /**************************************************************************/
414
415 //______________________________________________________________________________
416 void PointSetArray::TakeAction(TPointSelector* sel)
417 {
418   // Called from TPointSelector when internal arrays of the tree-selector
419   // are filled up and need to be processed.
420   // Virtual from TPointSelectorConsumer.
421
422   static const Exc_t eH("PointSetArray::TakeAction ");
423
424   if (sel == 0)
425     throw(eH + "selector is <null>.");
426
427   Int_t n = sel->GetNfill();
428
429   // printf("PointSetArray::TakeAction n=%d\n", n);
430
431   Double_t *vx = sel->GetV1(), *vy = sel->GetV2(), *vz = sel->GetV3();
432   Double_t *qq = sel->GetV4();
433
434   if(qq == 0)
435     throw(eH + "requires 4-d varexp.");
436
437   switch(fSourceCS) {
438   case TVT_XYZ:
439     while(n-- > 0) {
440       Fill(*vx, *vy, *vz, *qq);
441       ++vx; ++vy; ++vz; ++qq;
442     }
443     break;
444   case TVT_RPhiZ:
445     while(n-- > 0) {
446       Fill(*vx * TMath::Cos(*vy), *vx * TMath::Sin(*vy), *vz, *qq);
447       ++vx; ++vy; ++vz; ++qq;
448     }
449     break;
450   default:
451     throw(eH + "unknown tree variable type.");
452   }
453 }
454
455 /**************************************************************************/
456
457 //______________________________________________________________________________
458 void PointSetArray::InitBins(const Text_t* quant_name,
459                              Int_t nbins, Double_t min, Double_t max,
460                              Bool_t addRe)
461 {
462   static const Exc_t eH("PointSetArray::InitBins ");
463
464   if (nbins < 1) throw(eH + "nbins < 1.");
465   if (min > max) throw(eH + "min > max.");
466
467   RemoveElements();
468
469   fQuantName = quant_name;
470   fNBins     = nbins;
471   fLastBin   = -1;
472   fMin = fCurMin = min;
473   fMax = fCurMax = max;
474   fBinWidth  = (fMax - fMin)/fNBins;
475
476   fBins = new Reve::PointSet*[fNBins];
477   for (Int_t i=0; i<fNBins; ++i) {
478     fBins[i] = new Reve::PointSet
479       (Form("Slice %d [%4.3lf, %4.3lf]", i, fMin + i*fBinWidth, fMin + (i+1)*fBinWidth),
480        fDefPointSetCapacity);
481     fBins[i]->SetMarkerColor(fMarkerColor);
482     fBins[i]->SetMarkerStyle(fMarkerStyle);
483     fBins[i]->SetMarkerSize(fMarkerSize);
484     if (addRe)
485       gReve->AddRenderElement(fBins[i], this);
486     else
487       AddElement(fBins[i]);
488   }
489 }
490
491 //______________________________________________________________________________
492 void PointSetArray::Fill(Double_t x, Double_t y, Double_t z, Double_t quant)
493 {
494   fLastBin = Int_t( (quant - fMin)/fBinWidth );
495   if (fLastBin >= 0 && fLastBin < fNBins && fBins[fLastBin] != 0)
496     fBins[fLastBin]->SetNextPoint(x, y, z);
497   else
498     fLastBin = -1;
499 }
500
501 //______________________________________________________________________________
502 void PointSetArray::SetPointId(TObject* id)
503 {
504   if (fLastBin >= 0)
505     fBins[fLastBin]->SetPointId(id);
506 }
507
508 //______________________________________________________________________________
509 void PointSetArray::CloseBins()
510 {
511   for (Int_t i=0; i<fNBins; ++i) {
512     if (fBins[i] != 0) {
513       // HACK! PolyMarker3D does half-management of array size.
514       // In fact, the error is mine, in pointset3d(gl) i use fN instead of Size().
515       // Fixed in my root, but not elsewhere.
516       fBins[i]->fN = fBins[i]->fLastPoint;
517
518       fBins[i]->ComputeBBox();
519     }
520   }
521   fLastBin = -1;
522 }
523
524 /**************************************************************************/
525
526 //______________________________________________________________________________
527 void PointSetArray::SetOwnIds(Bool_t o)
528 {
529   for (Int_t i=0; i<fNBins; ++i)
530   {
531     if (fBins[i] != 0)
532       fBins[i]->SetOwnIds(o);
533   }
534 }
535
536 /**************************************************************************/
537
538 //______________________________________________________________________________
539 void PointSetArray::SetRange(Double_t min, Double_t max)
540 {
541   using namespace TMath;
542
543   fCurMin = min; fCurMax = max;
544   Int_t  low_b = (Int_t) Max(Double_t(0),       Floor((min-fMin)/fBinWidth));
545   Int_t high_b = (Int_t) Min(Double_t(fNBins-1), Ceil((max-fMin)/fBinWidth));
546   for (Int_t i=0; i<fNBins; ++i) {
547     if (fBins[i] != 0)
548       fBins[i]->SetRnrSelf(i>=low_b && i<=high_b);
549   }
550 }
551
552
553 /******************************************************************************/
554 /******************************************************************************/
555
556 //______________________________________________________________________________
557 // NLTPointSet
558 //
559
560 ClassImp(NLTPointSet)
561
562 //______________________________________________________________________________
563 NLTPointSet::NLTPointSet() :
564   PointSet     (),
565   NLTProjected ()
566 {
567   // Default contructor.
568 }
569
570 //______________________________________________________________________________
571 void NLTPointSet::SetProjection(NLTProjector* proj, NLTProjectable* model)
572 {
573   NLTProjected::SetProjection(proj, model);
574
575   * (TAttMarker*)this = * dynamic_cast<TAttMarker*>(fProjectable);
576 }
577
578 //______________________________________________________________________________
579 void NLTPointSet::UpdateProjection()
580 {
581   NLTProjection& proj = * fProjector->GetProjection();
582   PointSet     & ps   = * dynamic_cast<PointSet*>(fProjectable);
583
584   Int_t n = ps.GetN();
585   Reset(n);
586   Float_t *o = ps.GetP(), *p = GetP();
587   for (Int_t i = 0; i < n; ++i, o+=3, p+=3)
588   {
589     p[0] = o[0]; p[1] = o[1]; p[2] = o[2];
590     proj.ProjectPoint(p[0], p[1], p[2]);
591     p[2] = fDepth;
592   }
593   fLastPoint = n - 1;
594 }