//  
// Author: Oliver Maria Kind <mailto: kind@mail.desy.de>
// Update: $Id: HepVtxTrackHelix.h,v 1.14 2015/09/02 06:22:09 mergelm Exp $
// Copyright: 2008 (C) Oliver Maria Kind
//
#ifndef HEP_HepVtxTrackHelix
#define HEP_HepVtxTrackHelix
#ifndef ROOT_HepTrackHelix
#include <HepTrackHelix.h>
#endif
#ifndef HEP_HepVertex
#include <HepVertex.h>
#endif
#ifndef ROOT_TRef
#include <TRef.h>
#endif
#ifndef ROOT_TVector3
#include <TVector3.h>
#endif

class HepVtxTrackHelix : public HepTrackHelix {

protected:
    TVector3  fP_Vtx;        // Momentum at vertex
    Float_t   fQovP_Vtx;     // Charge over momentum magnitude at vertex (e/GeV)
                             // Note that here the full magnitude of
                             // the momentum is used - not just Pt
    Float_t   fD0_Vtx;       // Distance of closest approach (cm) w.r.t. the reference
                             // point in the xy plane at the vtx z position.
                             // This value is 0 for all tracks used in the vertex fit.
                             // It is non-zero for tracks not used for the vertex fit but
                             // which supposed to origin from this vertex.
                             // The sign of D_0 is computef by
                             //   sign(d) = sign((p_vec x Bz_vec) . (pca_vec-ref_vec)
                             // cf ATL-SOFT-PUB-2006-004
    Float_t   fZ0_Vtx;       // Z coordinate of closest approach (cm) w.r.t. the reference.
                             // Should always be 0
    Float_t   fPhi0_Vtx;     // Azimuth (rad) of the helix' tangent at the
			     // point of closest approach in the xy plane
                             // at the z position of this vertex
    Float_t   fTheta0_Vtx;   // Polar angle (rad) of the track at the point of closest
                             // approach in the xy plane at the z position of this vertex
    Float_t   fChi2_Vtx;     // Chi2 of the track after vertex refit
    Int_t     fNDoF_Vtx;     // No. of degrees of freedom of track after vertex refit
    Float_t  fCovMat_Vtx[15];// Covariance matrix, contains covariance-elements of Helix Parameters
                             // w.r.t associated vertex -> See HepTrackHelix    
    TRef     *fVertex;       // Link to vertex
    TRef     *fIsClonedFrom; // Sometimes a track points to multiple
                             // vertices. In this case the track is cloned and given new vtx fit
                             // paramaters. This link stores the pointer to the track from which the
                             // clone had being made of. In case the track is not a clone (most of the
                             // time) the link is the zero pointer
    
public:
    HepVtxTrackHelix();
    HepVtxTrackHelix(Int_t Id, Float_t Chi2, Int_t NDoF,
		     Float_t Xref, Float_t Yref, Float_t Zref,  Float_t Phi0,
		     Float_t QovP, Float_t D0, Float_t Z0, Float_t Theta0, 
		     const Float_t CovMat[15] = 0);
    virtual ~HepVtxTrackHelix();
    
    virtual void Clear(Option_t *option = "");
    virtual Bool_t IsPositive() const;
    virtual Bool_t IsNegative() const;
    virtual Int_t Charge() const;

    virtual const TVector3& P() const;
    virtual Float_t Pt() const;
    virtual Float_t Pmag() const;
    virtual Float_t Pz() const;
    
    virtual Float_t QovP() const;
    virtual Float_t D0() const;
    virtual Float_t Z0() const;
    virtual Float_t Phi0() const;
    virtual Float_t Theta0() const;
    virtual const Float_t* CovMatrix() const;
    
    virtual Float_t Chi2() const;
    virtual Int_t   NDoF() const;
    inline virtual Float_t Chi2ovNDoF() {return Chi2()/NDoF();}
        
    inline const TVector3& GetRef_Vtx() const {return this->GetVertex()->GetPos(); }

    inline Float_t GetQovP_Vtx()      const { return fQovP_Vtx; }
    inline Float_t GetD0_Vtx()        const { return fD0_Vtx; }
    inline Float_t GetZ0_Vtx()        const { return fZ0_Vtx; }
    inline Float_t GetPhi0_Vtx()      const { return fPhi0_Vtx; }
    inline Float_t GetTheta0_Vtx()    const { return fTheta0_Vtx; }
    inline const Float_t* GetCovMatrix_Vtx() const { return fCovMat_Vtx; }
    inline Float_t GetChi2_Vtx()      const { return fChi2_Vtx; }
    inline Float_t GetNDoF_Vtx()      const { return fNDoF_Vtx; }

    inline HepVertex* GetVertex() const
	{ return (HepVertex*)fVertex->GetObject(); }
    inline HepVtxTrackHelix* IsClonedFrom() const
        { return (HepVtxTrackHelix*)fIsClonedFrom->GetObject(); }
    inline Bool_t IsInvalidVtxTrack()   const { return ProducedAt()->IsInvalid(); }
    Bool_t IsVtxTrack() const;
    Bool_t IsPrimaryVtxTrack()   const;
    Bool_t IsPileUpVtxTrack()    const; 
    Bool_t IsSecondaryVtxTrack() const;
    inline HepVertex* ProducedAt() const { return GetVertex(); }
    inline void SetVertex(HepVertex *vtx) { *fVertex = (TObject*)vtx; }
    inline void SetIsClonedFrom(HepVtxTrackHelix* trk) { *fIsClonedFrom = (TObject*)trk; }

    inline void SetCovMatrix_Vtx(Float_t* CovMat_Vtx) {
	for(Int_t i=0; i<15; i++) fCovMat_Vtx[i]= CovMat_Vtx[i];
    }
    inline void SetPVtx(Float_t Px_Vtx, Float_t Py_Vtx, Float_t Pz_Vtx) {
	// Set momentum 3-vector from vtx-fit (in global frame)
	// will be filled while running the a++converter !!!
	fP_Vtx.SetXYZ(Px_Vtx, Py_Vtx, Pz_Vtx); 
    }
    inline void SetPtEtaPhiVtx(Float_t Pt_Vtx, Float_t Eta_Vtx, Float_t Phi_Vtx) {
	// Set momentum 3-vector from vtx-fit (in global frame)
	// will be filled while running the a++converter !!!
	fP_Vtx.SetPtEtaPhi(Pt_Vtx, Eta_Vtx, Phi_Vtx); 
    }

    inline virtual Float_t Theta() const {
	// Polar angle of momentum vector
	return P().Theta();

    }
    inline virtual Float_t CosTheta() const {
	// Cosine of the momentum polar angle
	return P().CosTheta();
	
    }
    inline virtual Float_t Eta() const {
	// Pseudo-rapidity of the momentum vector
	Float_t cos_theta = CosTheta();
	if ( cos_theta*cos_theta < 1. ) {
	    return P().Eta();
	} else {
	    return ( Pz() > 0. ) ? 10e10 : -10e10;
	}
    }    
    inline virtual Float_t Phi() const {
	// Momentum vector azimuth
	return P().Phi();
    }
    
    void SetVtxParameters(Float_t Chi2_Vtx, Int_t NDoF_Vtx,
			  Float_t Phi0_Vtx, Float_t QovP_Vtx, Float_t D0_Vtx,
			  Float_t Z0_Vtx, Float_t Theta0_Vtx, Float_t CovMat_Vtx[15]);
    
  protected:
    virtual void PrintVtxType() const;
    virtual void PrintClone() const;
    
    ClassDef(HepVtxTrackHelix,9) // Track helix attached to a vertex
};
#endif
 HepVtxTrackHelix.h:1
 HepVtxTrackHelix.h:2
 HepVtxTrackHelix.h:3
 HepVtxTrackHelix.h:4
 HepVtxTrackHelix.h:5
 HepVtxTrackHelix.h:6
 HepVtxTrackHelix.h:7
 HepVtxTrackHelix.h:8
 HepVtxTrackHelix.h:9
 HepVtxTrackHelix.h:10
 HepVtxTrackHelix.h:11
 HepVtxTrackHelix.h:12
 HepVtxTrackHelix.h:13
 HepVtxTrackHelix.h:14
 HepVtxTrackHelix.h:15
 HepVtxTrackHelix.h:16
 HepVtxTrackHelix.h:17
 HepVtxTrackHelix.h:18
 HepVtxTrackHelix.h:19
 HepVtxTrackHelix.h:20
 HepVtxTrackHelix.h:21
 HepVtxTrackHelix.h:22
 HepVtxTrackHelix.h:23
 HepVtxTrackHelix.h:24
 HepVtxTrackHelix.h:25
 HepVtxTrackHelix.h:26
 HepVtxTrackHelix.h:27
 HepVtxTrackHelix.h:28
 HepVtxTrackHelix.h:29
 HepVtxTrackHelix.h:30
 HepVtxTrackHelix.h:31
 HepVtxTrackHelix.h:32
 HepVtxTrackHelix.h:33
 HepVtxTrackHelix.h:34
 HepVtxTrackHelix.h:35
 HepVtxTrackHelix.h:36
 HepVtxTrackHelix.h:37
 HepVtxTrackHelix.h:38
 HepVtxTrackHelix.h:39
 HepVtxTrackHelix.h:40
 HepVtxTrackHelix.h:41
 HepVtxTrackHelix.h:42
 HepVtxTrackHelix.h:43
 HepVtxTrackHelix.h:44
 HepVtxTrackHelix.h:45
 HepVtxTrackHelix.h:46
 HepVtxTrackHelix.h:47
 HepVtxTrackHelix.h:48
 HepVtxTrackHelix.h:49
 HepVtxTrackHelix.h:50
 HepVtxTrackHelix.h:51
 HepVtxTrackHelix.h:52
 HepVtxTrackHelix.h:53
 HepVtxTrackHelix.h:54
 HepVtxTrackHelix.h:55
 HepVtxTrackHelix.h:56
 HepVtxTrackHelix.h:57
 HepVtxTrackHelix.h:58
 HepVtxTrackHelix.h:59
 HepVtxTrackHelix.h:60
 HepVtxTrackHelix.h:61
 HepVtxTrackHelix.h:62
 HepVtxTrackHelix.h:63
 HepVtxTrackHelix.h:64
 HepVtxTrackHelix.h:65
 HepVtxTrackHelix.h:66
 HepVtxTrackHelix.h:67
 HepVtxTrackHelix.h:68
 HepVtxTrackHelix.h:69
 HepVtxTrackHelix.h:70
 HepVtxTrackHelix.h:71
 HepVtxTrackHelix.h:72
 HepVtxTrackHelix.h:73
 HepVtxTrackHelix.h:74
 HepVtxTrackHelix.h:75
 HepVtxTrackHelix.h:76
 HepVtxTrackHelix.h:77
 HepVtxTrackHelix.h:78
 HepVtxTrackHelix.h:79
 HepVtxTrackHelix.h:80
 HepVtxTrackHelix.h:81
 HepVtxTrackHelix.h:82
 HepVtxTrackHelix.h:83
 HepVtxTrackHelix.h:84
 HepVtxTrackHelix.h:85
 HepVtxTrackHelix.h:86
 HepVtxTrackHelix.h:87
 HepVtxTrackHelix.h:88
 HepVtxTrackHelix.h:89
 HepVtxTrackHelix.h:90
 HepVtxTrackHelix.h:91
 HepVtxTrackHelix.h:92
 HepVtxTrackHelix.h:93
 HepVtxTrackHelix.h:94
 HepVtxTrackHelix.h:95
 HepVtxTrackHelix.h:96
 HepVtxTrackHelix.h:97
 HepVtxTrackHelix.h:98
 HepVtxTrackHelix.h:99
 HepVtxTrackHelix.h:100
 HepVtxTrackHelix.h:101
 HepVtxTrackHelix.h:102
 HepVtxTrackHelix.h:103
 HepVtxTrackHelix.h:104
 HepVtxTrackHelix.h:105
 HepVtxTrackHelix.h:106
 HepVtxTrackHelix.h:107
 HepVtxTrackHelix.h:108
 HepVtxTrackHelix.h:109
 HepVtxTrackHelix.h:110
 HepVtxTrackHelix.h:111
 HepVtxTrackHelix.h:112
 HepVtxTrackHelix.h:113
 HepVtxTrackHelix.h:114
 HepVtxTrackHelix.h:115
 HepVtxTrackHelix.h:116
 HepVtxTrackHelix.h:117
 HepVtxTrackHelix.h:118
 HepVtxTrackHelix.h:119
 HepVtxTrackHelix.h:120
 HepVtxTrackHelix.h:121
 HepVtxTrackHelix.h:122
 HepVtxTrackHelix.h:123
 HepVtxTrackHelix.h:124
 HepVtxTrackHelix.h:125
 HepVtxTrackHelix.h:126
 HepVtxTrackHelix.h:127
 HepVtxTrackHelix.h:128
 HepVtxTrackHelix.h:129
 HepVtxTrackHelix.h:130
 HepVtxTrackHelix.h:131
 HepVtxTrackHelix.h:132
 HepVtxTrackHelix.h:133
 HepVtxTrackHelix.h:134
 HepVtxTrackHelix.h:135
 HepVtxTrackHelix.h:136
 HepVtxTrackHelix.h:137
 HepVtxTrackHelix.h:138
 HepVtxTrackHelix.h:139
 HepVtxTrackHelix.h:140
 HepVtxTrackHelix.h:141
 HepVtxTrackHelix.h:142
 HepVtxTrackHelix.h:143
 HepVtxTrackHelix.h:144
 HepVtxTrackHelix.h:145
 HepVtxTrackHelix.h:146
 HepVtxTrackHelix.h:147
 HepVtxTrackHelix.h:148
 HepVtxTrackHelix.h:149
 HepVtxTrackHelix.h:150
 HepVtxTrackHelix.h:151
 HepVtxTrackHelix.h:152
 HepVtxTrackHelix.h:153
 HepVtxTrackHelix.h:154
 HepVtxTrackHelix.h:155