ROOT logo
// @(#)root/eve:$Id: TEveTrackPropagator.h 27157 2009-01-15 14:05:12Z brun $
// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TEveTrackPropagator
#define ROOT_TEveTrackPropagator

#include "TEveVSDStructs.h"
#include "TEveUtil.h"
#include "TEveElement.h"
#include "TMarker.h"

#include <vector>

class TEvePointSet;


//==============================================================================
// TEveMagField
//==============================================================================

class TEveMagField
{
protected:
   Bool_t  fFieldConstant;

public:
   TEveMagField(): fFieldConstant(kFALSE){}
   virtual ~TEveMagField(){}

   virtual Bool_t IsConst() const {return fFieldConstant;};

   virtual void  PrintField(Float_t x, Float_t y, Float_t z) const
   {
      TEveVector b = GetField(x, y, z);
      printf("v(%f, %f, %f) B(%f, %f, %f) \n", x, y, z, b.fX, b.fY, b.fZ);
   }

   virtual TEveVector GetField(const TEveVector &v) const { return GetField(v.fX, v.fY, v.fZ);}
   virtual TEveVector GetField(Float_t x, Float_t y, Float_t z) const = 0;

   ClassDef(TEveMagField, 0); // Abstract interface to magnetic field
};


//==============================================================================
// TEveMagFieldConst
//==============================================================================

class TEveMagFieldConst : public TEveMagField
{
protected:
   TEveVector fB;

public:
   TEveMagFieldConst(Float_t x, Float_t y, Float_t z) : TEveMagField(), fB(x, y, z)
   { fFieldConstant = kTRUE; }
   virtual ~TEveMagFieldConst() {}

   using   TEveMagField::GetField;
   virtual TEveVector GetField(Float_t /*x*/, Float_t /*y*/, Float_t /*z*/) const { return fB; }

   ClassDef(TEveMagFieldConst, 0); // Interface to constant magnetic field.
};


//==============================================================================
// TEveMagFieldDuo
//==============================================================================

class TEveMagFieldDuo : public TEveMagField
{
protected:
   TEveVector fBIn;
   TEveVector fBOut;
   Float_t    fR2;

public:
   TEveMagFieldDuo(Float_t r, Float_t bIn, Float_t bOut) : TEveMagField(),
     fBIn(0,0,bIn), fBOut(0,0,bOut), fR2(r*r)
   {
      fFieldConstant = kFALSE;
   }
   virtual ~TEveMagFieldDuo() {}

   using   TEveMagField::GetField;
   virtual TEveVector GetField(Float_t x, Float_t y, Float_t /*z*/) const
   { return  ((x*x+y*y)<fR2) ? fBIn : fBOut; }

   ClassDef(TEveMagFieldDuo, 0); // Interface to magnetic field with two different values depending of radius.
};


//==============================================================================
// TEveTrackPropagator
//==============================================================================

class TEveTrackPropagator : public TEveElementList,
                            public TEveRefBackPtr
{
   friend class TEveTrackPropagatorSubEditor;

public:
   struct Helix_t
   {
      Int_t   fCharge;   // Charge of tracked particle.
      Float_t fMinAng;   // Minimal angular step between two helix points.
      Float_t fDelta;    // Maximal error at the mid-point of the line connecting two helix points.
      Float_t fMaxStep;  // Maximum allowed step size.
      Float_t fCurrentStep;

      Float_t fPhi;      // Accumulated angle to check fMaxOrbs by propagator.
      Bool_t  fValid;    // Corner case pT~0 or B~0, possible in variable mag field.

      // ----------------------------------------------------------------

      // helix parameters
      Float_t fLam;         // Momentum ratio pT/pZ.
      Float_t fR;           // Helix radius in cm.
      Float_t fPhiStep;     // Caluclated from fMinAng and fDelta.
      Float_t fSin, fCos;   // Current sin/cos(phistep).

      // cached
      TEveVector fB;        // Current magnetic field, cached.
      TEveVector fE1, fE2, fE3; // Base vectors: E1 -> B dir, E2->pT dir, E3 = E1xE2.
      TEveVector fPt, fPl;  // Transverse and longitudinal momentum.
      Float_t fPtMag;       // Magnitude of pT
      Float_t fPlDir;       // Momentum parallel to mag field.
      Float_t fLStep;       // Transverse step arc-length in cm.

      // ----------------------------------------------------------------

      Helix_t();

      void UpdateHelix(const TEveVector & p, const TEveVector& b, Bool_t fullUpdate);
      void UpdateRK   (const TEveVector & p, const TEveVector& b);
      void Step  (const TEveVector4& v, const TEveVector& p, TEveVector4& vOut, TEveVector& pOut);

      Float_t GetStep()  { return fLStep * TMath::Sqrt(1 + fLam*fLam); }
      Float_t GetStep2() { return fLStep * fLStep * (1 + fLam*fLam);   }
   };

   enum EStepper_e    { kHelix, kRungeKutta };
private:
   TEveTrackPropagator(const TEveTrackPropagator&);            // Not implemented
   TEveTrackPropagator& operator=(const TEveTrackPropagator&); // Not implemented

protected:
   EStepper_e               fStepper;

   TEveMagField*            fMagFieldObj;

   // Track extrapolation limits
   Float_t                  fMaxR;          // Max radius for track extrapolation
   Float_t                  fMaxZ;          // Max z-coordinate for track extrapolation.
   Int_t                    fNMax;          // max steps
   // Helix limits
   Float_t                  fMaxOrbs;       // Maximal angular path of tracks' orbits (1 ~ 2Pi).

   // Path-mark / first-vertex control
   Bool_t                   fEditPathMarks; // Show widgets for path-mark control in GUI editor.
   Bool_t                   fFitDaughters;  // Pass through daughter creation points when extrapolating a track.
   Bool_t                   fFitReferences; // Pass through given track-references when extrapolating a track.
   Bool_t                   fFitDecay;      // Pass through decay point when extrapolating a track.
   Bool_t                   fFitCluster2Ds; // Pass through 2D-clusters when extrapolating a track.
   Bool_t                   fRnrDaughters;  // Render daughter path-marks.
   Bool_t                   fRnrReferences; // Render track-reference path-marks.
   Bool_t                   fRnrDecay;      // Render decay path-marks.
   Bool_t                   fRnrCluster2Ds; // Render 2D-clusters.
   Bool_t                   fRnrFV;         // Render first vertex.
   TMarker                  fPMAtt;         // Marker attributes for rendering of path-marks.
   TMarker                  fFVAtt;         // Marker attributes for fits vertex.

   // ----------------------------------------------------------------

   // Propagation, state of current track
   std::vector<TEveVector4> fPoints;        // Calculated point.
   TEveVector               fV;             // Start vertex.
   Helix_t                  fH;             // Helix.

   void    RebuildTracks();
   void    Step(TEveVector4 &v, TEveVector &p, TEveVector4 &vOut, TEveVector &pOut);

   Bool_t  LoopToVertex(TEveVector& v, TEveVector& p);
   void    LoopToBounds(TEveVector& p);

   Bool_t  LineToVertex (TEveVector& v);
   void    LineToBounds (TEveVector& p);

   void    OneStepRungeKutta(Double_t charge, Double_t step, Double_t* vect, Double_t* vout);

   Bool_t  HelixIntersectPlane(const TEveVector& p, const TEveVector& point, const TEveVector& normal,
                               TEveVector& itsect);
   Bool_t  LineIntersectPlane(const TEveVector& p, const TEveVector& point, const TEveVector& normal,
                              TEveVector& itsect);

   Bool_t PointOverVertex(const TEveVector4& v0, const TEveVector4& v);

public:
   TEveTrackPropagator(const char* n="TEveTrackPropagator", const char* t="",
                       TEveMagField* field=0);
   virtual ~TEveTrackPropagator();

   virtual void OnZeroRefCount();

   virtual void CheckReferenceCount(const TEveException& eh="TEveElement::CheckReferenceCount ");

   virtual void ElementChanged(Bool_t update_scenes=kTRUE, Bool_t redraw=kFALSE);

   // propagation
   void   InitTrack(TEveVector& v, Int_t charge);
   void   ResetTrack();
   void   GoToBounds(TEveVector& p);
   Bool_t GoToVertex(TEveVector& v, TEveVector& p);

   Bool_t IntersectPlane(const TEveVector& p, const TEveVector& point, const TEveVector& normal,
                         TEveVector& itsect);

   void   FillPointSet(TEvePointSet* ps) const;

   void   SetStepper(EStepper_e s) { fStepper = s; }

   void   SetMagField(Float_t bX, Float_t bY, Float_t bZ);
   void   SetMagField(Float_t b) { SetMagField(0.f, 0.f, b); }
   void   SetMagFieldObj(TEveMagField * x);

   void   SetMaxR(Float_t x);
   void   SetMaxZ(Float_t x);
   void   SetMaxOrbs(Float_t x);
   void   SetMinAng(Float_t x);
   void   SetDelta(Float_t x);
   void   SetMaxStep(Float_t x);

   void   SetEditPathMarks(Bool_t x) { fEditPathMarks = x; }
   void   SetRnrDaughters(Bool_t x);
   void   SetRnrReferences(Bool_t x);
   void   SetRnrDecay(Bool_t x);
   void   SetRnrCluster2Ds(Bool_t x);
   void   SetFitDaughters(Bool_t x);
   void   SetFitReferences(Bool_t x);
   void   SetFitDecay(Bool_t x);
   void   SetFitCluster2Ds(Bool_t x);
   void   SetRnrFV(Bool_t x) { fRnrFV = x; }

   TEveVector GetMagField(Float_t x, Float_t y, Float_t z) { return fMagFieldObj->GetField(x, y, z); }
   void PrintMagField(Float_t x, Float_t y, Float_t z) const;

   EStepper_e   GetStepper()  const { return fStepper;}

   Float_t GetMaxR()     const { return fMaxR;     }
   Float_t GetMaxZ()     const { return fMaxZ;     }
   Float_t GetMaxOrbs()  const { return fMaxOrbs;  }
   Float_t GetMinAng()   const { return fH.fMinAng;   }
   Float_t GetDelta()    const { return fH.fDelta;    }
   Float_t GetMaxStep()  const { return fH.fMaxStep;  }

   Bool_t  GetEditPathMarks() const { return fEditPathMarks; }
   Bool_t  GetRnrDaughters()  const { return fRnrDaughters;  }
   Bool_t  GetRnrReferences() const { return fRnrReferences; }
   Bool_t  GetRnrDecay()      const { return fRnrDecay;      }
   Bool_t  GetRnrCluster2Ds() const { return fRnrCluster2Ds; }
   Bool_t  GetFitDaughters()  const { return fFitDaughters;  }
   Bool_t  GetFitReferences() const { return fFitReferences; }
   Bool_t  GetFitDecay()      const { return fFitDecay;      }
   Bool_t  GetFitCluster2Ds() const { return fFitCluster2Ds; }
   Bool_t  GetRnrFV()         const { return fRnrFV;         }

   TMarker& RefPMAtt() { return fPMAtt; }
   TMarker& RefFVAtt() { return fFVAtt; }

   static Bool_t IsOutsideBounds(const TEveVector& point, Float_t maxRsqr, Float_t maxZ);

   static Float_t             fgDefMagField; // Default value for constant solenoid magnetic field.
   static const Float_t       fgkB2C;        // Constant for conversion of momentum to curvature.
   static TEveTrackPropagator fgDefStyle;    // Default track render-style.

   ClassDef(TEveTrackPropagator, 0); // Calculates path of a particle taking into account special path-marks and imposed boundaries.
};

//______________________________________________________________________________
inline Bool_t TEveTrackPropagator::IsOutsideBounds(const TEveVector& point,
                                                   Float_t           maxRsqr,
                                                   Float_t           maxZ)
{
   // Return true if point% is outside of cylindrical bounds detrmined by
   // square radius and z.

   return TMath::Abs(point.fZ) > maxZ ||
          point.fX*point.fX + point.fY*point.fY > maxRsqr;
}

//______________________________________________________________________________
inline Bool_t TEveTrackPropagator::PointOverVertex(const TEveVector4 &v0,
                                                   const TEveVector4 &v)
{
   Float_t dotV = fH.fB.fX*(v0.fX-v.fX)
                + fH.fB.fY*(v0.fY-v.fY)
                + fH.fB.fZ*(v0.fZ-v.fZ);

   return (fH.fPlDir > 0 && dotV < 0) || (fH.fPlDir < 0 && dotV >0);
}

#endif
 TEveTrackPropagator.h:1
 TEveTrackPropagator.h:2
 TEveTrackPropagator.h:3
 TEveTrackPropagator.h:4
 TEveTrackPropagator.h:5
 TEveTrackPropagator.h:6
 TEveTrackPropagator.h:7
 TEveTrackPropagator.h:8
 TEveTrackPropagator.h:9
 TEveTrackPropagator.h:10
 TEveTrackPropagator.h:11
 TEveTrackPropagator.h:12
 TEveTrackPropagator.h:13
 TEveTrackPropagator.h:14
 TEveTrackPropagator.h:15
 TEveTrackPropagator.h:16
 TEveTrackPropagator.h:17
 TEveTrackPropagator.h:18
 TEveTrackPropagator.h:19
 TEveTrackPropagator.h:20
 TEveTrackPropagator.h:21
 TEveTrackPropagator.h:22
 TEveTrackPropagator.h:23
 TEveTrackPropagator.h:24
 TEveTrackPropagator.h:25
 TEveTrackPropagator.h:26
 TEveTrackPropagator.h:27
 TEveTrackPropagator.h:28
 TEveTrackPropagator.h:29
 TEveTrackPropagator.h:30
 TEveTrackPropagator.h:31
 TEveTrackPropagator.h:32
 TEveTrackPropagator.h:33
 TEveTrackPropagator.h:34
 TEveTrackPropagator.h:35
 TEveTrackPropagator.h:36
 TEveTrackPropagator.h:37
 TEveTrackPropagator.h:38
 TEveTrackPropagator.h:39
 TEveTrackPropagator.h:40
 TEveTrackPropagator.h:41
 TEveTrackPropagator.h:42
 TEveTrackPropagator.h:43
 TEveTrackPropagator.h:44
 TEveTrackPropagator.h:45
 TEveTrackPropagator.h:46
 TEveTrackPropagator.h:47
 TEveTrackPropagator.h:48
 TEveTrackPropagator.h:49
 TEveTrackPropagator.h:50
 TEveTrackPropagator.h:51
 TEveTrackPropagator.h:52
 TEveTrackPropagator.h:53
 TEveTrackPropagator.h:54
 TEveTrackPropagator.h:55
 TEveTrackPropagator.h:56
 TEveTrackPropagator.h:57
 TEveTrackPropagator.h:58
 TEveTrackPropagator.h:59
 TEveTrackPropagator.h:60
 TEveTrackPropagator.h:61
 TEveTrackPropagator.h:62
 TEveTrackPropagator.h:63
 TEveTrackPropagator.h:64
 TEveTrackPropagator.h:65
 TEveTrackPropagator.h:66
 TEveTrackPropagator.h:67
 TEveTrackPropagator.h:68
 TEveTrackPropagator.h:69
 TEveTrackPropagator.h:70
 TEveTrackPropagator.h:71
 TEveTrackPropagator.h:72
 TEveTrackPropagator.h:73
 TEveTrackPropagator.h:74
 TEveTrackPropagator.h:75
 TEveTrackPropagator.h:76
 TEveTrackPropagator.h:77
 TEveTrackPropagator.h:78
 TEveTrackPropagator.h:79
 TEveTrackPropagator.h:80
 TEveTrackPropagator.h:81
 TEveTrackPropagator.h:82
 TEveTrackPropagator.h:83
 TEveTrackPropagator.h:84
 TEveTrackPropagator.h:85
 TEveTrackPropagator.h:86
 TEveTrackPropagator.h:87
 TEveTrackPropagator.h:88
 TEveTrackPropagator.h:89
 TEveTrackPropagator.h:90
 TEveTrackPropagator.h:91
 TEveTrackPropagator.h:92
 TEveTrackPropagator.h:93
 TEveTrackPropagator.h:94
 TEveTrackPropagator.h:95
 TEveTrackPropagator.h:96
 TEveTrackPropagator.h:97
 TEveTrackPropagator.h:98
 TEveTrackPropagator.h:99
 TEveTrackPropagator.h:100
 TEveTrackPropagator.h:101
 TEveTrackPropagator.h:102
 TEveTrackPropagator.h:103
 TEveTrackPropagator.h:104
 TEveTrackPropagator.h:105
 TEveTrackPropagator.h:106
 TEveTrackPropagator.h:107
 TEveTrackPropagator.h:108
 TEveTrackPropagator.h:109
 TEveTrackPropagator.h:110
 TEveTrackPropagator.h:111
 TEveTrackPropagator.h:112
 TEveTrackPropagator.h:113
 TEveTrackPropagator.h:114
 TEveTrackPropagator.h:115
 TEveTrackPropagator.h:116
 TEveTrackPropagator.h:117
 TEveTrackPropagator.h:118
 TEveTrackPropagator.h:119
 TEveTrackPropagator.h:120
 TEveTrackPropagator.h:121
 TEveTrackPropagator.h:122
 TEveTrackPropagator.h:123
 TEveTrackPropagator.h:124
 TEveTrackPropagator.h:125
 TEveTrackPropagator.h:126
 TEveTrackPropagator.h:127
 TEveTrackPropagator.h:128
 TEveTrackPropagator.h:129
 TEveTrackPropagator.h:130
 TEveTrackPropagator.h:131
 TEveTrackPropagator.h:132
 TEveTrackPropagator.h:133
 TEveTrackPropagator.h:134
 TEveTrackPropagator.h:135
 TEveTrackPropagator.h:136
 TEveTrackPropagator.h:137
 TEveTrackPropagator.h:138
 TEveTrackPropagator.h:139
 TEveTrackPropagator.h:140
 TEveTrackPropagator.h:141
 TEveTrackPropagator.h:142
 TEveTrackPropagator.h:143
 TEveTrackPropagator.h:144
 TEveTrackPropagator.h:145
 TEveTrackPropagator.h:146
 TEveTrackPropagator.h:147
 TEveTrackPropagator.h:148
 TEveTrackPropagator.h:149
 TEveTrackPropagator.h:150
 TEveTrackPropagator.h:151
 TEveTrackPropagator.h:152
 TEveTrackPropagator.h:153
 TEveTrackPropagator.h:154
 TEveTrackPropagator.h:155
 TEveTrackPropagator.h:156
 TEveTrackPropagator.h:157
 TEveTrackPropagator.h:158
 TEveTrackPropagator.h:159
 TEveTrackPropagator.h:160
 TEveTrackPropagator.h:161
 TEveTrackPropagator.h:162
 TEveTrackPropagator.h:163
 TEveTrackPropagator.h:164
 TEveTrackPropagator.h:165
 TEveTrackPropagator.h:166
 TEveTrackPropagator.h:167
 TEveTrackPropagator.h:168
 TEveTrackPropagator.h:169
 TEveTrackPropagator.h:170
 TEveTrackPropagator.h:171
 TEveTrackPropagator.h:172
 TEveTrackPropagator.h:173
 TEveTrackPropagator.h:174
 TEveTrackPropagator.h:175
 TEveTrackPropagator.h:176
 TEveTrackPropagator.h:177
 TEveTrackPropagator.h:178
 TEveTrackPropagator.h:179
 TEveTrackPropagator.h:180
 TEveTrackPropagator.h:181
 TEveTrackPropagator.h:182
 TEveTrackPropagator.h:183
 TEveTrackPropagator.h:184
 TEveTrackPropagator.h:185
 TEveTrackPropagator.h:186
 TEveTrackPropagator.h:187
 TEveTrackPropagator.h:188
 TEveTrackPropagator.h:189
 TEveTrackPropagator.h:190
 TEveTrackPropagator.h:191
 TEveTrackPropagator.h:192
 TEveTrackPropagator.h:193
 TEveTrackPropagator.h:194
 TEveTrackPropagator.h:195
 TEveTrackPropagator.h:196
 TEveTrackPropagator.h:197
 TEveTrackPropagator.h:198
 TEveTrackPropagator.h:199
 TEveTrackPropagator.h:200
 TEveTrackPropagator.h:201
 TEveTrackPropagator.h:202
 TEveTrackPropagator.h:203
 TEveTrackPropagator.h:204
 TEveTrackPropagator.h:205
 TEveTrackPropagator.h:206
 TEveTrackPropagator.h:207
 TEveTrackPropagator.h:208
 TEveTrackPropagator.h:209
 TEveTrackPropagator.h:210
 TEveTrackPropagator.h:211
 TEveTrackPropagator.h:212
 TEveTrackPropagator.h:213
 TEveTrackPropagator.h:214
 TEveTrackPropagator.h:215
 TEveTrackPropagator.h:216
 TEveTrackPropagator.h:217
 TEveTrackPropagator.h:218
 TEveTrackPropagator.h:219
 TEveTrackPropagator.h:220
 TEveTrackPropagator.h:221
 TEveTrackPropagator.h:222
 TEveTrackPropagator.h:223
 TEveTrackPropagator.h:224
 TEveTrackPropagator.h:225
 TEveTrackPropagator.h:226
 TEveTrackPropagator.h:227
 TEveTrackPropagator.h:228
 TEveTrackPropagator.h:229
 TEveTrackPropagator.h:230
 TEveTrackPropagator.h:231
 TEveTrackPropagator.h:232
 TEveTrackPropagator.h:233
 TEveTrackPropagator.h:234
 TEveTrackPropagator.h:235
 TEveTrackPropagator.h:236
 TEveTrackPropagator.h:237
 TEveTrackPropagator.h:238
 TEveTrackPropagator.h:239
 TEveTrackPropagator.h:240
 TEveTrackPropagator.h:241
 TEveTrackPropagator.h:242
 TEveTrackPropagator.h:243
 TEveTrackPropagator.h:244
 TEveTrackPropagator.h:245
 TEveTrackPropagator.h:246
 TEveTrackPropagator.h:247
 TEveTrackPropagator.h:248
 TEveTrackPropagator.h:249
 TEveTrackPropagator.h:250
 TEveTrackPropagator.h:251
 TEveTrackPropagator.h:252
 TEveTrackPropagator.h:253
 TEveTrackPropagator.h:254
 TEveTrackPropagator.h:255
 TEveTrackPropagator.h:256
 TEveTrackPropagator.h:257
 TEveTrackPropagator.h:258
 TEveTrackPropagator.h:259
 TEveTrackPropagator.h:260
 TEveTrackPropagator.h:261
 TEveTrackPropagator.h:262
 TEveTrackPropagator.h:263
 TEveTrackPropagator.h:264
 TEveTrackPropagator.h:265
 TEveTrackPropagator.h:266
 TEveTrackPropagator.h:267
 TEveTrackPropagator.h:268
 TEveTrackPropagator.h:269
 TEveTrackPropagator.h:270
 TEveTrackPropagator.h:271
 TEveTrackPropagator.h:272
 TEveTrackPropagator.h:273
 TEveTrackPropagator.h:274
 TEveTrackPropagator.h:275
 TEveTrackPropagator.h:276
 TEveTrackPropagator.h:277
 TEveTrackPropagator.h:278
 TEveTrackPropagator.h:279
 TEveTrackPropagator.h:280
 TEveTrackPropagator.h:281
 TEveTrackPropagator.h:282
 TEveTrackPropagator.h:283
 TEveTrackPropagator.h:284
 TEveTrackPropagator.h:285
 TEveTrackPropagator.h:286
 TEveTrackPropagator.h:287
 TEveTrackPropagator.h:288
 TEveTrackPropagator.h:289
 TEveTrackPropagator.h:290
 TEveTrackPropagator.h:291
 TEveTrackPropagator.h:292
 TEveTrackPropagator.h:293
 TEveTrackPropagator.h:294
 TEveTrackPropagator.h:295
 TEveTrackPropagator.h:296
 TEveTrackPropagator.h:297
 TEveTrackPropagator.h:298
 TEveTrackPropagator.h:299
 TEveTrackPropagator.h:300
 TEveTrackPropagator.h:301
 TEveTrackPropagator.h:302
 TEveTrackPropagator.h:303
 TEveTrackPropagator.h:304
 TEveTrackPropagator.h:305
 TEveTrackPropagator.h:306
 TEveTrackPropagator.h:307
 TEveTrackPropagator.h:308
 TEveTrackPropagator.h:309
 TEveTrackPropagator.h:310