#include "TEveTrack.h"
#include "TEveTrackPropagator.h"
#include "TEvePointSet.h"
#include "TPolyLine3D.h"
#include "TMarker.h"
#include "TPolyMarker3D.h"
#include "TColor.h"
#include "TROOT.h"
#include "TEveManager.h"
#include "TEveBrowser.h"
#include "TEveTrackProjected.h"
#include "TCanvas.h"
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
ClassImp(TEveTrack)
TEveTrack::TEveTrack() :
   TEveLine(),
   fV(),
   fP(),
   fBeta(0),
   fPdg(0),
   fCharge(0),
   fLabel(kMinInt),
   fIndex(kMinInt),
   fPathMarks(),
   fPropagator(0)
{
   
}
TEveTrack::TEveTrack(TParticle* t, Int_t label, TEveTrackPropagator* rs):
   TEveLine(),
   fV(t->Vx(), t->Vy(), t->Vz()),
   fP(t->Px(), t->Py(), t->Pz()),
   fBeta(t->P()/t->Energy()),
   fPdg(0),
   fCharge(0),
   fLabel(label),
   fIndex(kMinInt),
   fPathMarks(),
   fPropagator(0)
{
   
   SetPropagator(rs);
   fMainColorPtr = &fLineColor;
   TParticlePDG* pdgp = t->GetPDG();
   if (pdgp) {
      fPdg    = pdgp->PdgCode();
      fCharge = (Int_t) TMath::Nint(pdgp->Charge()/3);
   }
   SetName(t->GetName());
}
TEveTrack::TEveTrack(TEveMCTrack* t, TEveTrackPropagator* rs):
   TEveLine(),
   fV(t->Vx(), t->Vy(), t->Vz()),
   fP(t->Px(), t->Py(), t->Pz()),
   fBeta(t->P()/t->Energy()),
   fPdg(0),
   fCharge(0),
   fLabel(t->fLabel),
   fIndex(t->fIndex),
   fPathMarks(),
   fPropagator(0)
{
   
   SetPropagator(rs);
   fMainColorPtr = &fLineColor;
   TParticlePDG* pdgp = t->GetPDG();
   if (pdgp == 0) {
      t->ResetPdgCode(); pdgp = t->GetPDG();
   }
   fCharge = (Int_t) TMath::Nint(pdgp->Charge()/3);
   SetName(t->GetName());
}
TEveTrack::TEveTrack(TEveRecTrack* t, TEveTrackPropagator* rs) :
   TEveLine(),
   fV(t->fV),
   fP(t->fP),
   fBeta(t->fBeta),
   fPdg(0),
   fCharge(t->fSign),
   fLabel(t->fLabel),
   fIndex(t->fIndex),
   fPathMarks(),
   fPropagator(0)
{
   
   SetPropagator(rs);
   fMainColorPtr = &fLineColor;
   SetName(t->GetName());
}
TEveTrack::TEveTrack(const TEveTrack& t) :
   TEveLine(),
   fV(t.fV),
   fP(t.fP),
   fBeta(t.fBeta),
   fPdg(t.fPdg),
   fCharge(t.fCharge),
   fLabel(t.fLabel),
   fIndex(t.fIndex),
   fPathMarks(),
   fPropagator(0)
{
   
   SetMainColor(t.GetMainColor());
   
   fRnrLine   = t.fRnrLine;
   fRnrPoints = t.fRnrPoints;
   
   fLineColor = t.fLineColor;
   fLineStyle = t.fLineStyle;
   fLineWidth = t.fLineWidth;
   SetPathMarks(t);
   SetPropagator (t.fPropagator);
}
TEveTrack::~TEveTrack()
{
   
   SetPropagator(0);
   for (vpPathMark_i i=fPathMarks.begin(); i!=fPathMarks.end(); ++i)
      delete *i;
}
const TGPicture*
TEveTrack::GetListTreeIcon()
{
   
   
   return fgListTreeIcons[4];
}
void TEveTrack::SetStdTitle()
{
   
   TString idx(fIndex == kMinInt ? "<undef>" : Form("%d", fIndex));
   TString lbl(fLabel == kMinInt ? "<undef>" : Form("%d", fLabel));
   SetTitle(Form("Index=%s, Label=%s\nChg=%d, Pdg=%d\n"
                 "pT=%.3f, pZ=%.3f\nV=(%.3f, %.3f, %.3f)",
                 idx.Data(), lbl.Data(), fCharge, fPdg,
                 fP.Perp(), fP.fZ, fV.fX, fV.fY, fV.fZ));
}
void TEveTrack::SetTrackParams(const TEveTrack& t)
{
   
   
   fV         = t.fV;
   fP         = t.fP;
   fBeta      = t.fBeta;
   fPdg       = t.fPdg;
   fCharge    = t.fCharge;
   fLabel     = t.fLabel;
   fIndex     = t.fIndex;
   SetMainColor(t.GetMainColor());
   
   fRnrLine   = t.fRnrLine;
   fRnrPoints = t.fRnrPoints;
   
   fLineColor = t.fLineColor;
   fLineStyle = t.fLineStyle;
   fLineWidth = t.fLineWidth;
   fPathMarks.clear();
   SetPropagator(t.fPropagator);
}
void TEveTrack::SetPathMarks(const TEveTrack& t)
{
   
   const std::vector<TEvePathMark*>& refs = t.GetPathMarksRef();
   for (std::vector<TEvePathMark*>::const_iterator i=refs.begin(); i!=refs.end(); ++i)
   {
      fPathMarks.push_back(new TEvePathMark(**i));
   }
}
void TEveTrack::SetPropagator(TEveTrackPropagator* rs)
{
   
   
   if (fPropagator == rs) return;
   if (fPropagator) fPropagator->DecRefCount(this);
   fPropagator = rs;
   if (fPropagator) rs->IncRefCount(this);
}
void TEveTrack::SetAttLineAttMarker(TEveTrackList* tl)
{
   
   SetRnrLine(tl->GetRnrLine());
   SetLineColor(tl->GetLineColor());
   SetLineStyle(tl->GetLineStyle());
   SetLineWidth(tl->GetLineWidth());
   SetRnrPoints(tl->GetRnrPoints());
   SetMarkerColor(tl->GetMarkerColor());
   SetMarkerStyle(tl->GetMarkerStyle());
   SetMarkerSize(tl->GetMarkerSize());
}
void TEveTrack::MakeTrack(Bool_t recurse)
{
   
   
   
   Reset(0);
   TEveTrackPropagator& rTP((fPropagator != 0) ? *fPropagator : TEveTrackPropagator::fgDefStyle);
   const Float_t maxRsq = rTP.GetMaxR() * rTP.GetMaxR();
   if (TMath::Abs(fV.fZ) < rTP.GetMaxZ() && fV.fX*fV.fX + fV.fY*fV.fY < maxRsq)
   {
      TEveVector currP = fP;
      Bool_t decay = kFALSE;
      fPropagator->InitTrack(fV, fP, fBeta, fCharge);
      for (std::vector<TEvePathMark*>::iterator i=fPathMarks.begin(); i!=fPathMarks.end(); ++i)
      {
         TEvePathMark* pm = *i;
         if (rTP.GetFitReferences() && pm->fType == TEvePathMark::kReference)
         {
            if (TMath::Abs(pm->fV.fZ) > rTP.GetMaxZ() ||
               pm->fV.fX*pm->fV.fX + pm->fV.fY*pm->fV.fY > maxRsq)
               goto bounds;
            
            if (fPropagator->GoToVertex(pm->fV, currP)) {
               currP.fX = pm->fP.fX; currP.fY = pm->fP.fY; currP.fZ = pm->fP.fZ;
            }
         }
         else if (rTP.GetFitDaughters() &&  pm->fType == TEvePathMark::kDaughter)
         {
            if (TMath::Abs(pm->fV.fZ) > rTP.GetMaxZ() ||
                pm->fV.fX*pm->fV.fX + pm->fV.fY*pm->fV.fY > maxRsq)
               goto bounds;
            
            if (fPropagator->GoToVertex(pm->fV, currP)) {
               currP.fX -= pm->fP.fX; currP.fY -= pm->fP.fY; currP.fZ -= pm->fP.fZ;
            }
         }
         else if (rTP.GetFitDecay() &&  pm->fType == TEvePathMark::kDecay)
         {
            if (TMath::Abs(pm->fV.fZ) > rTP.GetMaxZ() ||
                pm->fV.fX*pm->fV.fX + pm->fV.fY*pm->fV.fY > maxRsq)
               goto bounds;
            
            fPropagator->GoToVertex(pm->fV, currP);
            decay = true;
            break;
         }
      } 
   bounds:
      if (!decay || rTP.GetFitDecay() == kFALSE)
      {
         
         fPropagator->GoToBounds(currP);
      }
      
      fPropagator->FillPointSet(this);
      fPropagator->ResetTrack();
   }
   if (recurse)
   {
      for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
      {
         TEveTrack* t = dynamic_cast<TEveTrack*>(*i);
         if (t) t->MakeTrack(recurse);
      }
   }
}
TClass* TEveTrack::ProjectedClass() const
{
   
   return TEveTrackProjected::Class();
}
namespace {
struct Cmp_pathmark_t
{
   bool operator()(TEvePathMark* const & a, TEvePathMark* const & b)
   { return a->fTime < b->fTime; }
};
}
void TEveTrack::SortPathMarksByTime()
{
   
   std::sort(fPathMarks.begin(), fPathMarks.end(), Cmp_pathmark_t());
}
void TEveTrack::ImportHits()
{
   
   
   TEveUtil::LoadMacro("hits_from_label.C");
   gROOT->ProcessLine(Form("hits_from_label(%d, (TEveElement*)%p);",
                           fLabel, this));
}
void TEveTrack::ImportClusters()
{
   
   
   TEveUtil::LoadMacro("clusters_from_label.C");
   gROOT->ProcessLine(Form("clusters_from_label(%d, (TEveElement*)%p);",
                           fLabel, this));
}
void TEveTrack::ImportClustersFromIndex()
{
   
   
   static const TEveException eh("TEveTrack::ImportClustersFromIndex ");
   if (fIndex == kMinInt)
      throw(eh + "index not set.");
   TEveUtil::LoadMacro("clusters_from_index.C");
   gROOT->ProcessLine(Form("clusters_from_index(%d, (TEveElement*)%p);",
                           fIndex, this));
}
void TEveTrack::ImportKine()
{
   
   
   static const TEveException eh("TEveTrack::ImportKine ");
   if (fLabel == kMinInt)
      throw(eh + "label not set.");
   Int_t label;
   if (fLabel < 0) {
      Warning(eh, "label negative, taking absolute value.");
      label = -fLabel;
   } else {
      label = fLabel;
   }
   TEveUtil::LoadMacro("kine_tracks.C");
   gROOT->ProcessLine(Form("kine_track(%d, kTRUE, kTRUE, kTRUE, kTRUE, (TEveElement*)%p);",
                           label, this));
}
void TEveTrack::ImportKineWithArgs(Bool_t importMother, Bool_t importDaugters,
                                   Bool_t colorPdg,     Bool_t recurse)
{
   
   
   
   
   
   
   
   static const TEveException eh("TEveTrack::ImportKineWithArgs ");
   if (fLabel == kMinInt)
      throw(eh + "label not set.");
   Int_t label;
   if (fLabel < 0) {
      Warning(eh, "label negative, taking absolute value.");
      label = -fLabel;
   } else {
      label = fLabel;
   }
   TEveUtil::LoadMacro("kine_tracks.C");
   gROOT->ProcessLine(Form("kine_track(%d, %d, %d, %d, %d, (TEveElement*)%p);",
                           label, importMother, importDaugters, colorPdg, recurse, this));
}
void TEveTrack::PrintKineStack()
{
   
   
   static const TEveException eh("TEveTrack::PrintKineStack ");
   if (fLabel == kMinInt)
      throw(eh + "label not set.");
   Int_t label;
   if (fLabel < 0) {
      Warning(eh, "label negative, taking absolute value.");
      label = -fLabel;
   } else {
      label = fLabel;
   }
   TEveUtil::LoadMacro("print_kine_from_label.C");
   gROOT->ProcessLine(Form("print_kine_from_label(%d);", label));
}
void TEveTrack::PrintPathMarks()
{
   
   static const TEveException eh("TEveTrack::PrintPathMarks ");
   printf("TEveTrack '%s', number of path marks %d, label %d\n",
          GetName(), (Int_t)fPathMarks.size(), fLabel);
   TEvePathMark* pm;
   for (vpPathMark_i i=fPathMarks.begin(); i!=fPathMarks.end(); i++)
   {
      pm = *i;
      printf("  %-9s  p: %8f %8f %8f Vertex: %8e %8e %8e %g \n",
             pm->TypeName(),
             pm->fP.fX,  pm->fP.fY, pm->fP.fZ,
             pm->fV.fX,  pm->fV.fY, pm->fV.fZ,
             pm->fTime);
   }
}
void TEveTrack::CtrlClicked(TEveTrack* track)
{
   
   
   Emit("CtrlClicked(TEveTrack*)", (Long_t)track);
}
void TEveTrack::SetLineStyle(Style_t lstyle)
{
   
   
   TAttLine::SetLineStyle(lstyle);
   std::list<TEveProjected*>::iterator pi = fProjectedList.begin();
   while (pi != fProjectedList.end())
   {
      TEveTrack* pt = dynamic_cast<TEveTrack*>(*pi);
      if (pt)
      {
         pt->SetLineStyle(lstyle);
         pt->ElementChanged();
      }
      ++pi;
   }
}
ClassImp(TEveTrackList)
TEveTrackList::TEveTrackList(TEveTrackPropagator* rs) :
   TEveElementList(),
   TAttMarker(1, 20, 1),
   TAttLine(1,1,1),
   fRecurse(kTRUE),
   fPropagator(0),
   fRnrLine(kTRUE),
   fRnrPoints(kFALSE),
   fMinPt (0), fMaxPt (0), fLimPt (0),
   fMinP  (0), fMaxP  (0), fLimP  (0)
{
   
   
   fChildClass = TEveTrack::Class(); 
   fMainColorPtr = &fLineColor;
   if (fPropagator== 0) rs = new TEveTrackPropagator;
   SetPropagator(rs);
}
TEveTrackList::TEveTrackList(const Text_t* name, TEveTrackPropagator* rs) :
   TEveElementList(name),
   TAttMarker(1, 20, 1),
   TAttLine(1,1,1),
   fRecurse(kTRUE),
   fPropagator(0),
   fRnrLine(kTRUE),
   fRnrPoints(kFALSE),
   fMinPt (0), fMaxPt (0), fLimPt (0),
   fMinP  (0), fMaxP  (0), fLimP  (0)
{
   
   
   fChildClass = TEveTrack::Class(); 
   fMainColorPtr = &fLineColor;
   if (fPropagator== 0) rs = new TEveTrackPropagator;
   SetPropagator(rs);
}
TEveTrackList::~TEveTrackList()
{
   
   SetPropagator(0);
}
void TEveTrackList::SetPropagator(TEveTrackPropagator* rs)
{
   
   
   
   if (fPropagator == rs) return;
   if (fPropagator) fPropagator->DecRefCount();
   fPropagator = rs;
   if (fPropagator) rs->IncRefCount();
}
void TEveTrackList::MakeTracks(Bool_t recurse)
{
   
   
   fLimPt = fLimP = 0;
   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      track->MakeTrack(recurse);
      fLimPt = TMath::Max(fLimPt, track->fP.Perp());
      fLimP  = TMath::Max(fLimP,  track->fP.Mag());
      if (recurse)
         FindMomentumLimits(*i, recurse);
   }
   fLimPt = RoundMomentumLimit(fLimPt);
   fLimP  = RoundMomentumLimit(fLimP);
   if (fMaxPt == 0) fMaxPt = fLimPt;
   if (fMaxP  == 0) fMaxP  = fLimP;
   gEve->Redraw3D();
}
void TEveTrackList::FindMomentumLimits(TEveElement* el, Bool_t recurse)
{
   
   
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      TEveTrack* track = dynamic_cast<TEveTrack*>(*i);
      if (track)
      {
         fLimPt = TMath::Max(fLimPt, track->fP.Perp());
         fLimP  = TMath::Max(fLimP,  track->fP.Mag());
         if (recurse)
            FindMomentumLimits(*i, recurse);
      }
   }
}
Float_t TEveTrackList::RoundMomentumLimit(Float_t x)
{
   
   using namespace TMath;
   Double_t fac = Power(10, 1 - Floor(Log10(x)));
   return Ceil(fac*x) / fac;
}
void TEveTrackList::SetRnrLine(Bool_t rnr)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetRnrLine() == fRnrLine)
         track->SetRnrLine(rnr);
      if (fRecurse)
         SetRnrLine(rnr, *i);
   }
   fRnrLine = rnr;
}
void TEveTrackList::SetRnrLine(Bool_t rnr, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && (track->GetRnrLine() == fRnrLine))
         track->SetRnrLine(rnr);
      if (fRecurse)
         SetRnrLine(rnr, *i);
   }
}
void TEveTrackList::SetRnrPoints(Bool_t rnr)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetRnrPoints() == fRnrPoints)
         track->SetRnrPoints(rnr);
      if (fRecurse)
         SetRnrPoints(rnr, *i);
   }
   fRnrPoints = rnr;
}
void TEveTrackList::SetRnrPoints(Bool_t rnr, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track)
         if (track->GetRnrPoints() == fRnrPoints)
            track->SetRnrPoints(rnr);
      if (fRecurse)
         SetRnrPoints(rnr, *i);
   }
}
void TEveTrackList::SetMainColor(Color_t col)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetLineColor() == fLineColor)
         track->SetLineColor(col);
      if (fRecurse)
         SetLineColor(col, *i);
   }
   TEveElement::SetMainColor(col);
}
void TEveTrackList::SetLineColor(Color_t col, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetLineColor() == fLineColor)
         track->SetLineColor(col);
      if (fRecurse)
         SetLineColor(col, *i);
   }
}
void TEveTrackList::SetLineWidth(Width_t width)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetLineWidth() == fLineWidth)
         track->SetLineWidth(width);
      if (fRecurse)
         SetLineWidth(width, *i);
   }
   fLineWidth=width;
}
void TEveTrackList::SetLineWidth(Width_t width, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetLineWidth() == fLineWidth)
         track->SetLineWidth(width);
      if (fRecurse)
         SetLineWidth(width, *i);
   }
}
void TEveTrackList::SetLineStyle(Style_t style)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetLineStyle() == fLineStyle)
         track->SetLineStyle(style);
      if (fRecurse)
         SetLineStyle(style, *i);
   }
   fLineStyle=style;
}
void TEveTrackList::SetLineStyle(Style_t style, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetLineStyle() == fLineStyle)
         track->SetLineStyle(style);
      if (fRecurse)
         SetLineStyle(style, *i);
   }
}
void TEveTrackList::SetMarkerStyle(Style_t style)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetMarkerStyle() == fMarkerStyle)
         track->SetMarkerStyle(style);
      if (fRecurse)
         SetMarkerStyle(style, *i);
   }
   fMarkerStyle=style;
}
void TEveTrackList::SetMarkerStyle(Style_t style, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetMarkerStyle() == fMarkerStyle)
         track->SetMarkerStyle(style);
      if (fRecurse)
         SetMarkerStyle(style, *i);
   }
}
void TEveTrackList::SetMarkerColor(Color_t col)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetMarkerColor() == fMarkerColor)
         track->SetMarkerColor(col);
      if (fRecurse)
         SetMarkerColor(col, *i);
   }
   fMarkerColor=col;
}
void TEveTrackList::SetMarkerColor(Color_t col, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetMarkerColor() == fMarkerColor)
         track->SetMarkerColor(col);
      if (fRecurse)
         SetMarkerColor(col, *i);
   }
}
void TEveTrackList::SetMarkerSize(Size_t size)
{
   
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      TEveTrack* track = (TEveTrack*)(*i);
      if (track->GetMarkerSize() == fMarkerSize)
         track->SetMarkerSize(size);
      if (fRecurse)
         SetMarkerSize(size, *i);
   }
   fMarkerSize=size;
}
void TEveTrackList::SetMarkerSize(Size_t size, TEveElement* el)
{
   
   TEveTrack* track;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      track = dynamic_cast<TEveTrack*>(*i);
      if (track && track->GetMarkerSize() == fMarkerSize)
         track->SetMarkerSize(size);
      if (fRecurse)
         SetMarkerSize(size, *i);
   }
}
void TEveTrackList::SelectByPt(Float_t min_pt, Float_t max_pt)
{
   
   
   
   fMinPt = min_pt;
   fMaxPt = max_pt;
   const Float_t minptsq = min_pt*min_pt;
   const Float_t maxptsq = max_pt*max_pt;
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      const Float_t ptsq = ((TEveTrack*)(*i))->fP.Perp2();
      Bool_t on = ptsq >= minptsq && ptsq <= maxptsq;
      (*i)->SetRnrState(on);
      if (on && fRecurse)
         SelectByPt(min_pt, max_pt, *i);
   }
}
void TEveTrackList::SelectByPt(Float_t min_pt, Float_t max_pt, TEveElement* el)
{
   
   const Float_t minptsq = min_pt*min_pt;
   const Float_t maxptsq = max_pt*max_pt;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      TEveTrack* track = dynamic_cast<TEveTrack*>(*i);
      if (track)
      {
         const Float_t ptsq = track->fP.Perp2();
         Bool_t on = ptsq >= minptsq && ptsq <= maxptsq;
         track->SetRnrState(on);
         if (on && fRecurse)
            SelectByPt(min_pt, max_pt, *i);
      }
   }
}
void TEveTrackList::SelectByP(Float_t min_p, Float_t max_p)
{
   
   
   
   fMinP = min_p;
   fMaxP = max_p;
   const Float_t minpsq = min_p*min_p;
   const Float_t maxpsq = max_p*max_p;
   for (List_i i=BeginChildren(); i!=EndChildren(); ++i)
   {
      const Float_t psq  = ((TEveTrack*)(*i))->fP.Mag2();
      Bool_t on = psq >= minpsq && psq <= maxpsq;
      (*i)->SetRnrState(psq >= minpsq && psq <= maxpsq);
      if (on && fRecurse)
         SelectByP(min_p, max_p, *i);
   }
}
void TEveTrackList::SelectByP(Float_t min_p, Float_t max_p, TEveElement* el)
{
   
   const Float_t minpsq = min_p*min_p;
   const Float_t maxpsq = max_p*max_p;
   for (List_i i=el->BeginChildren(); i!=el->EndChildren(); ++i)
   {
      TEveTrack* track = dynamic_cast<TEveTrack*>(*i);
      if (track)
      {
         const Float_t psq  = ((TEveTrack*)(*i))->fP.Mag2();
         Bool_t on = psq >= minpsq && psq <= maxpsq;
         track->SetRnrState(on);
         if (on && fRecurse)
            SelectByP(min_p, max_p, *i);
      }
   }
}
TEveTrack* TEveTrackList::FindTrackByLabel(Int_t label)
{
   
   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
      if (((TEveTrack*)(*i))->GetLabel() == label) {
         TGListTree     *lt   = gEve->GetLTEFrame()->GetListTree();
         TGListTreeItem *mlti = lt->GetSelected();
         if (mlti->GetUserData() != this)
            mlti = FindListTreeItem(lt);
         TGListTreeItem *tlti = (*i)->FindListTreeItem(lt, mlti);
         lt->HighlightItem(tlti);
         lt->SetSelected(tlti);
         gEve->EditElement(*i);
         return (TEveTrack*) *i;
      }
   }
   return 0;
}
TEveTrack* TEveTrackList::FindTrackByIndex(Int_t index)
{
   
   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
      if (((TEveTrack*)(*i))->GetIndex() == index) {
         TGListTree     *lt   = gEve->GetLTEFrame()->GetListTree();
         TGListTreeItem *mlti = lt->GetSelected();
         if (mlti->GetUserData() != this)
            mlti = FindListTreeItem(lt);
         TGListTreeItem *tlti = (*i)->FindListTreeItem(lt, mlti);
         lt->HighlightItem(tlti);
         lt->SetSelected(tlti);
         gEve->EditElement(*i);
         return (TEveTrack*) *i;
      }
   }
   return 0;
}
void TEveTrackList::ImportHits()
{
   
   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
      ((TEveTrack*)(*i))->ImportHits();
   }
}
void TEveTrackList::ImportClusters()
{
   
   for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i) {
      ((TEveTrack*)(*i))->ImportClusters();
   }
}
TClass* TEveTrackList::ProjectedClass() const
{
   
   return TEveTrackListProjected::Class();
}
#include "TEveGedEditor.h"
ClassImp(TEveTrackCounter)
TEveTrackCounter* TEveTrackCounter::fgInstance = 0;
TEveTrackCounter::TEveTrackCounter(const Text_t* name, const Text_t* title) :
   TEveElement(),
   TNamed(name, title),
   fBadLineStyle (6),
   fClickAction  (kCA_ToggleTrack),
   fAllTracks    (0),
   fGoodTracks   (0),
   fTrackLists   ()
{
   
   
   if (fgInstance == 0) fgInstance = this;
   TQObject::Connect("TEveTrack", "CtrlClicked(TEveTrack*)",
                     "TEveTrackCounter", this, "DoTrackAction(TEveTrack*)");
}
TEveTrackCounter::~TEveTrackCounter()
{
   
   
   TQObject::Disconnect("TEveTrack", "DoTrackAction(TEveTrack*)");
   if (fgInstance == this) fgInstance = 0;
}
void TEveTrackCounter::Reset()
{
   
   printf("TEveTrackCounter::Reset()\n");
   fAllTracks  = 0;
   fGoodTracks = 0;
   TIter next(&fTrackLists);
   TEveTrackList* tlist;
   while ((tlist = dynamic_cast<TEveTrackList*>(next())))
      tlist->DecDenyDestroy();
   fTrackLists.Clear("nodelete");
}
void TEveTrackCounter::RegisterTracks(TEveTrackList* tlist, Bool_t goodTracks)
{
   
   
   
   tlist->IncDenyDestroy();
   fTrackLists.Add(tlist);
   List_i i = tlist->BeginChildren();
   while (i != tlist->EndChildren())
   {
      TEveTrack* t = dynamic_cast<TEveTrack*>(*i);
      if (t != 0)
      {
         if (goodTracks)
         {
            ++fGoodTracks;
         } else {
            t->SetLineStyle(fBadLineStyle);
         }
         ++fAllTracks;
      }
      ++i;
   }
}
void TEveTrackCounter::DoTrackAction(TEveTrack* track)
{
   
   
   
   
   
   
   
   
   static const TEveException eh("TEveTrackCounter::DoTrackAction ");
   switch (fClickAction)
   {
      case kCA_PrintTrackInfo:
      {
         printf("TEveTrack '%s'\n", track->GetObject(eh)->GetName());
         TEveVector &v = track->fV, &p = track->fP;
         printf("  Vx=%f, Vy=%f, Vz=%f; Pt=%f, Pz=%f, phi=%f)\n",
                v.fX, v.fY, v.fZ, p.Perp(), p.fZ, TMath::RadToDeg()*p.Phi());
         printf("  <other information should be printed ... full AliESDtrack>\n");
         break;
      }
      case kCA_ToggleTrack:
      {
         if (track->GetLineStyle() == 1)
         {
            track->SetLineStyle(fBadLineStyle);
            --fGoodTracks;
         } else {
            track->SetLineStyle(1);
            ++fGoodTracks;
         }
         track->ElementChanged();
         gEve->Redraw3D();
         printf("TEveTrackCounter::CountTrack All=%d, Good=%d, Bad=%d\n",
                fAllTracks, fGoodTracks, fAllTracks-fGoodTracks);
         if (gEve->GetEditor()->GetModel() == GetObject(eh))
            gEve->EditElement(this);
         break;
      }
   } 
}
void TEveTrackCounter::OutputEventTracks(FILE* out)
{
   
   
   
   
   if (out == 0)
   {
      out = stdout;
      fprintf(out, "TEveTrackCounter::FinalizeEvent()\n");
   }
   fprintf(out, "Event = %d  Ntracks = %d\n", fEventId, fGoodTracks);
   TIter tlists(&fTrackLists);
   TEveTrackList* tlist;
   Int_t cnt = 0;
   while ((tlist = (TEveTrackList*) tlists()) != 0)
   {
      List_i i = tlist->BeginChildren();
      while (i != tlist->EndChildren())
      {
         TEveTrack* t = dynamic_cast<TEveTrack*>(*i);
         if (t != 0 && t->GetLineStyle() == 1)
         {
            ++cnt;
            fprintf(out, " %2d: chg=%+2d  pt=%8.5f  eta=%+8.5f\n",
                    cnt, t->fCharge, t->fP.Perp(), t->fP.Eta());
         }
         ++i;
      }
   }
}
Last update: Thu Jan 17 08:49:24 2008
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.