//____________________________________________________________________
//
// Vertex class for reconstructed vertices
// 

//  
// Author: Oliver Maria Kind <mailto: kind@mail.desy.de>
// Update: $Id: HepVertex.cxx,v 1.9 2012/08/28 14:30:11 herrberg Exp $
// Copyright: 2008 (C) Oliver Maria Kind
//
#ifndef HEP_HepVertex
#include <HepVertex.h>
#endif
#include <HepVtxTrackHelix.h>
#include <iostream>
#include <TSystem.h>

using namespace std;

#ifndef __CINT__
ClassImp(HepVertex);
#endif

//____________________________________________________________________

HepVertex::HepVertex() {
    //
    // Default constructor
    //
    fPos.SetXYZ(0, 0, 1); // unit-vector in Z dircetion (instead of
			  // zero-vector). This is to avoid
			  // difficulties when using polar coordinates
    fDaughters = new TRefArray;
    fErrX = 0;
    fErrY = 0;
    fErrZ = 0;
    fNDaughters = -999;
}

//____________________________________________________________________

HepVertex::HepVertex(Int_t Id, Float_t X, Float_t Y, Float_t Z,
		     Float_t Chi2, Int_t NDoF) :
fId(Id), fChi2(Chi2), fNDoF(NDoF) {
    //
    // Normal constructor
    //
    fPos.SetXYZ(X, Y, Z);
    fDaughters = new TRefArray;
    fType = kInvalid;
    fErrX = 0;
    fErrY = 0;
    fErrZ = 0;
    fNDaughters = -999;
}

//____________________________________________________________________

HepVertex::~HepVertex() {
    //
    // Default destructor
    //
    delete fDaughters; fDaughters = 0;
}

//____________________________________________________________________

void HepVertex::Clear(Option_t *option) {
    //
    // Clear this object
    //
    fPos.SetXYZ(0, 0, 1);
    fType = kInvalid;
    fErrX = 0;
    fErrY = 0;
    fErrZ = 0;
    delete fDaughters; fDaughters = 0;
    fNDaughters = -999;
}

//____________________________________________________________________

void HepVertex::AddDaughter(HepVtxTrackHelix *trk) {
    //
    // Add outgoing helix to the list of daughters
    // and set this vertex as mother in the track object
    //
    fDaughters->Add((TObject*)trk);
    HepVertex *vtx = trk->ProducedAt();
    if ( vtx != 0 ) {
	Error("AddDaughter",
	      "Track points already to a different vertex. Abort");
	gSystem->Abort(0);
    }
    trk->SetVertex(this);
}

//____________________________________________________________________

void HepVertex::Print(Option_t *option) const {
    //
    // Print object information
    //
    // Options available:
    //   "daughters" - Print all daughter tracks also
    //   "nohead"    - No header containing the variable names is
    //                 displayed. Useful when printing a whole table
    //                 for a list of vertices
    //
    TString opt = option;
    opt.ToLower();

    // Print header
    if ( !opt.Contains("nohead") ) PrintHeader();

    // Print
    cout.setf(ios::showpoint | ios::fixed, ios::floatfield);
    cout.precision(3);
    cout.width(4);  cout << fId;
    cout.width(12); cout << X();
    cout.width(12); cout << Y();
    cout.width(12); cout << Z();
    cout.width(15); cout << GetVtxTypeStr()->Data();
    cout.width(6);  cout << GetNDaughters();
    cout.width(9);  cout << fChi2 << "/";
    cout.width(3);  cout << fNDoF;
    cout << endl;
        
    // Print footer
    if ( !opt.Contains("nohead") ) PrintFooter();

    // Print daughter tracks (if wished)
    if ( opt.Contains("daughters") ) {
	TIter next_trk(GetDaughters());
	HepVtxTrackHelix *trk = 0;
	cout << endl << "Vtx daughters:" << endl;
	HepVtxTrackHelix::PrintHeader();
	while ( (trk=(HepVtxTrackHelix*)next_trk()) ) {
	    trk->Print("nohead");
	}
	HepVtxTrackHelix::PrintFooter();
    }
}

//____________________________________________________________________

void HepVertex::PrintHeader() {
    //
    // Print information header
    //
    cout << "---------------------------------------------------------------------------" << endl
         << " Id        X (cm)      Y (cm)      Z (cm)       Type     Ntrk    Chi2/DoF  " << endl
         << "---------------------------------------------------------------------------" << endl; 
}

//____________________________________________________________________

void HepVertex::PrintFooter() {
    //
    // Print footer
    //
    cout << "---------------------------------------------------------------------------" << endl;
}

//____________________________________________________________________

TString* HepVertex::GetVtxTypeStr() const {
    //
    // Return character string of vertex type(s)
    //
    TString *type = new TString("");
    if ( fType & kInvalid ) {
	type->Append("invalid");
    } else if ( fType & kDummy ) {
	type->Append("dummy");
    } else if ( fType & kNotSpecified ) {
	type->Append("not specified");
    } else if ( fType & kPrimary ) {
	type->Append("primary");
    } else if ( fType & kPileUp ) {
	type->Append("pile-up");
    } else if ( fType & kSecondary ) {
	type->Append("sec");
	if ( fType & kTrkKink ) type->Append(",kink");
	if ( fType & kConversion ) type->Append(",conv");
	if ( fType & kV0Vtx ) type->Append(",V0");
	if ( fType & kV0K0s ) type->Append(",K0s");
	if ( fType & kV0Lambda ) type->Append(",Lambda");
	if ( fType & kV0LambdaBar ) type->Append(",LambdaBar");
	if ( fType & kV0Rho ) type->Append(",Rho");
    } else {
	Error("GetVtxTypeStr", "Unknown vertex type %d. Abort!", fType);
	gSystem->Abort(0);
    }
    return type;
}

//____________________________________________________________________

Int_t HepVertex::Compare(const TObject *obj) const {
    //
    // Compares chi2/ndof of this vertex with another.
    // Necessary for sorting
    //
    Float_t chi2 = GetChi2overNDoF();
    if ( chi2 >  ((HepVertex*)obj)->GetChi2overNDoF() ) return -1;
    if ( chi2 == ((HepVertex*)obj)->GetChi2overNDoF() ) return  0;
    if ( chi2 <  ((HepVertex*)obj)->GetChi2overNDoF() ) return  1;
    return 0;
}
 HepVertex.cxx:1
 HepVertex.cxx:2
 HepVertex.cxx:3
 HepVertex.cxx:4
 HepVertex.cxx:5
 HepVertex.cxx:6
 HepVertex.cxx:7
 HepVertex.cxx:8
 HepVertex.cxx:9
 HepVertex.cxx:10
 HepVertex.cxx:11
 HepVertex.cxx:12
 HepVertex.cxx:13
 HepVertex.cxx:14
 HepVertex.cxx:15
 HepVertex.cxx:16
 HepVertex.cxx:17
 HepVertex.cxx:18
 HepVertex.cxx:19
 HepVertex.cxx:20
 HepVertex.cxx:21
 HepVertex.cxx:22
 HepVertex.cxx:23
 HepVertex.cxx:24
 HepVertex.cxx:25
 HepVertex.cxx:26
 HepVertex.cxx:27
 HepVertex.cxx:28
 HepVertex.cxx:29
 HepVertex.cxx:30
 HepVertex.cxx:31
 HepVertex.cxx:32
 HepVertex.cxx:33
 HepVertex.cxx:34
 HepVertex.cxx:35
 HepVertex.cxx:36
 HepVertex.cxx:37
 HepVertex.cxx:38
 HepVertex.cxx:39
 HepVertex.cxx:40
 HepVertex.cxx:41
 HepVertex.cxx:42
 HepVertex.cxx:43
 HepVertex.cxx:44
 HepVertex.cxx:45
 HepVertex.cxx:46
 HepVertex.cxx:47
 HepVertex.cxx:48
 HepVertex.cxx:49
 HepVertex.cxx:50
 HepVertex.cxx:51
 HepVertex.cxx:52
 HepVertex.cxx:53
 HepVertex.cxx:54
 HepVertex.cxx:55
 HepVertex.cxx:56
 HepVertex.cxx:57
 HepVertex.cxx:58
 HepVertex.cxx:59
 HepVertex.cxx:60
 HepVertex.cxx:61
 HepVertex.cxx:62
 HepVertex.cxx:63
 HepVertex.cxx:64
 HepVertex.cxx:65
 HepVertex.cxx:66
 HepVertex.cxx:67
 HepVertex.cxx:68
 HepVertex.cxx:69
 HepVertex.cxx:70
 HepVertex.cxx:71
 HepVertex.cxx:72
 HepVertex.cxx:73
 HepVertex.cxx:74
 HepVertex.cxx:75
 HepVertex.cxx:76
 HepVertex.cxx:77
 HepVertex.cxx:78
 HepVertex.cxx:79
 HepVertex.cxx:80
 HepVertex.cxx:81
 HepVertex.cxx:82
 HepVertex.cxx:83
 HepVertex.cxx:84
 HepVertex.cxx:85
 HepVertex.cxx:86
 HepVertex.cxx:87
 HepVertex.cxx:88
 HepVertex.cxx:89
 HepVertex.cxx:90
 HepVertex.cxx:91
 HepVertex.cxx:92
 HepVertex.cxx:93
 HepVertex.cxx:94
 HepVertex.cxx:95
 HepVertex.cxx:96
 HepVertex.cxx:97
 HepVertex.cxx:98
 HepVertex.cxx:99
 HepVertex.cxx:100
 HepVertex.cxx:101
 HepVertex.cxx:102
 HepVertex.cxx:103
 HepVertex.cxx:104
 HepVertex.cxx:105
 HepVertex.cxx:106
 HepVertex.cxx:107
 HepVertex.cxx:108
 HepVertex.cxx:109
 HepVertex.cxx:110
 HepVertex.cxx:111
 HepVertex.cxx:112
 HepVertex.cxx:113
 HepVertex.cxx:114
 HepVertex.cxx:115
 HepVertex.cxx:116
 HepVertex.cxx:117
 HepVertex.cxx:118
 HepVertex.cxx:119
 HepVertex.cxx:120
 HepVertex.cxx:121
 HepVertex.cxx:122
 HepVertex.cxx:123
 HepVertex.cxx:124
 HepVertex.cxx:125
 HepVertex.cxx:126
 HepVertex.cxx:127
 HepVertex.cxx:128
 HepVertex.cxx:129
 HepVertex.cxx:130
 HepVertex.cxx:131
 HepVertex.cxx:132
 HepVertex.cxx:133
 HepVertex.cxx:134
 HepVertex.cxx:135
 HepVertex.cxx:136
 HepVertex.cxx:137
 HepVertex.cxx:138
 HepVertex.cxx:139
 HepVertex.cxx:140
 HepVertex.cxx:141
 HepVertex.cxx:142
 HepVertex.cxx:143
 HepVertex.cxx:144
 HepVertex.cxx:145
 HepVertex.cxx:146
 HepVertex.cxx:147
 HepVertex.cxx:148
 HepVertex.cxx:149
 HepVertex.cxx:150
 HepVertex.cxx:151
 HepVertex.cxx:152
 HepVertex.cxx:153
 HepVertex.cxx:154
 HepVertex.cxx:155
 HepVertex.cxx:156
 HepVertex.cxx:157
 HepVertex.cxx:158
 HepVertex.cxx:159
 HepVertex.cxx:160
 HepVertex.cxx:161
 HepVertex.cxx:162
 HepVertex.cxx:163
 HepVertex.cxx:164
 HepVertex.cxx:165
 HepVertex.cxx:166
 HepVertex.cxx:167
 HepVertex.cxx:168
 HepVertex.cxx:169
 HepVertex.cxx:170
 HepVertex.cxx:171
 HepVertex.cxx:172
 HepVertex.cxx:173
 HepVertex.cxx:174
 HepVertex.cxx:175
 HepVertex.cxx:176
 HepVertex.cxx:177
 HepVertex.cxx:178
 HepVertex.cxx:179
 HepVertex.cxx:180
 HepVertex.cxx:181
 HepVertex.cxx:182
 HepVertex.cxx:183
 HepVertex.cxx:184
 HepVertex.cxx:185
 HepVertex.cxx:186
 HepVertex.cxx:187
 HepVertex.cxx:188
 HepVertex.cxx:189
 HepVertex.cxx:190
 HepVertex.cxx:191
 HepVertex.cxx:192
 HepVertex.cxx:193
 HepVertex.cxx:194
 HepVertex.cxx:195
 HepVertex.cxx:196
 HepVertex.cxx:197
 HepVertex.cxx:198
 HepVertex.cxx:199
 HepVertex.cxx:200
 HepVertex.cxx:201
 HepVertex.cxx:202
 HepVertex.cxx:203
 HepVertex.cxx:204
 HepVertex.cxx:205
 HepVertex.cxx:206
 HepVertex.cxx:207
 HepVertex.cxx:208
 HepVertex.cxx:209
 HepVertex.cxx:210