////////////////////////////////////////////////////////
//  Author:  Jochen Markert
//  Written: 11/28/2007
//  Revised:
//
//  vertex information in a file. 
//  format : ntuple name "vertex" leafs "vX:vY:vZ".
//      or : ntuple name "vertex" leafs "vX:vY:vZ:seqNr".
//  Can be used e.g. for embedded particles. The
//  eventloop will be stopped latest at the last entry of the ntuple
//  no matter if a larger number was given to the loop.
////////////////////////////////////////////////////////

#include "PVertexFile.h"
#include "PChannel.h"
#include "PStaticData.h"

PVertexFile::PVertexFile() {
    nEvent       = 0;
    input        = NULL;
    vertex_tuple = NULL; 

    vertex_x = makeStaticData()->GetBatchValue("_event_vertex_x");
    vertex_y = makeStaticData()->GetBatchValue("_event_vertex_y");
    vertex_z = makeStaticData()->GetBatchValue("_event_vertex_z");
    seqnr    = makeStaticData()->GetBatchValue("_event_seqnr");
}

PVertexFile::~PVertexFile() {
    if(input) {
	input->Close();
	input        = NULL;
        vertex_tuple = NULL;
    }
}


Bool_t PVertexFile::OpenFile(TString inputfile) {
    // Inputfile : root ntuple file for the vertex point.
    // If no file is provided vertex will be 0,0,0.


    if(inputfile.CompareTo("") != 0 ){
	input = new TFile(inputfile.Data(), "READ");
	if(!input){
	    Error("OpenFile", "Could not open root file for vertex input! Vertex will be 0,0,0!");
	}
        vertex_tuple = (TNtuple*)input->Get("vertex");
	if(!vertex_tuple){
	    Error("OpenFile", "Could not read vertex ntuple from root file! Vertex will be 0,0,0!");
	    input -> Close();
            input = NULL;
	}
    }
    return kTRUE;
}

Bool_t PVertexFile::Modify(PParticle **, int *, int *, int) {
    // Read the particles from the defined stack and copy this to the official
    // particle stream

    Float_t xVertex, yVertex, zVertex;
    xVertex = yVertex = zVertex = 0.0;
    Int_t   seqNr = -1;

    //-----------------------------------------------------
    // reading vertex coordinates from ntuple for embedding
    if(vertex_tuple)
    {
	if(!vertex_tuple->GetEntry(nEvent)) {
	    return kFALSE;
	} else {
	    Float_t *vertex = vertex_tuple->GetArgs();

	    if(vertex_tuple->GetNvar() == 4) {
		xVertex   = vertex[0];
		yVertex   = vertex[1];
		zVertex   = vertex[2];
		seqNr = (Int_t) vertex[3];
		nEvent++;
	    } else if(vertex_tuple->GetNvar() == 3) {
		xVertex   = vertex[0];
		yVertex   = vertex[1];
		zVertex   = vertex[2];
		seqNr = -1;
		nEvent++;
	    } else {
		Error("Modify()", "Ntuple has wrong number of variables!");
                return kFALSE;
	    }
	}
    }

    *vertex_x = xVertex;
    *vertex_y = yVertex;
    *vertex_z = zVertex;
    *seqnr    = (Double_t) seqNr;

    return kTRUE;
};

ClassImp(PVertexFile) 
 PVertexFile.cc:1
 PVertexFile.cc:2
 PVertexFile.cc:3
 PVertexFile.cc:4
 PVertexFile.cc:5
 PVertexFile.cc:6
 PVertexFile.cc:7
 PVertexFile.cc:8
 PVertexFile.cc:9
 PVertexFile.cc:10
 PVertexFile.cc:11
 PVertexFile.cc:12
 PVertexFile.cc:13
 PVertexFile.cc:14
 PVertexFile.cc:15
 PVertexFile.cc:16
 PVertexFile.cc:17
 PVertexFile.cc:18
 PVertexFile.cc:19
 PVertexFile.cc:20
 PVertexFile.cc:21
 PVertexFile.cc:22
 PVertexFile.cc:23
 PVertexFile.cc:24
 PVertexFile.cc:25
 PVertexFile.cc:26
 PVertexFile.cc:27
 PVertexFile.cc:28
 PVertexFile.cc:29
 PVertexFile.cc:30
 PVertexFile.cc:31
 PVertexFile.cc:32
 PVertexFile.cc:33
 PVertexFile.cc:34
 PVertexFile.cc:35
 PVertexFile.cc:36
 PVertexFile.cc:37
 PVertexFile.cc:38
 PVertexFile.cc:39
 PVertexFile.cc:40
 PVertexFile.cc:41
 PVertexFile.cc:42
 PVertexFile.cc:43
 PVertexFile.cc:44
 PVertexFile.cc:45
 PVertexFile.cc:46
 PVertexFile.cc:47
 PVertexFile.cc:48
 PVertexFile.cc:49
 PVertexFile.cc:50
 PVertexFile.cc:51
 PVertexFile.cc:52
 PVertexFile.cc:53
 PVertexFile.cc:54
 PVertexFile.cc:55
 PVertexFile.cc:56
 PVertexFile.cc:57
 PVertexFile.cc:58
 PVertexFile.cc:59
 PVertexFile.cc:60
 PVertexFile.cc:61
 PVertexFile.cc:62
 PVertexFile.cc:63
 PVertexFile.cc:64
 PVertexFile.cc:65
 PVertexFile.cc:66
 PVertexFile.cc:67
 PVertexFile.cc:68
 PVertexFile.cc:69
 PVertexFile.cc:70
 PVertexFile.cc:71
 PVertexFile.cc:72
 PVertexFile.cc:73
 PVertexFile.cc:74
 PVertexFile.cc:75
 PVertexFile.cc:76
 PVertexFile.cc:77
 PVertexFile.cc:78
 PVertexFile.cc:79
 PVertexFile.cc:80
 PVertexFile.cc:81
 PVertexFile.cc:82
 PVertexFile.cc:83
 PVertexFile.cc:84
 PVertexFile.cc:85
 PVertexFile.cc:86
 PVertexFile.cc:87
 PVertexFile.cc:88
 PVertexFile.cc:89
 PVertexFile.cc:90
 PVertexFile.cc:91
 PVertexFile.cc:92
 PVertexFile.cc:93
 PVertexFile.cc:94
 PVertexFile.cc:95
 PVertexFile.cc:96
 PVertexFile.cc:97
 PVertexFile.cc:98
 PVertexFile.cc:99
 PVertexFile.cc:100
 PVertexFile.cc:101
 PVertexFile.cc:102