////////////////////////////////////////////////////////
//  File input interface implementation file
//
//  This class serves as interface to event input from ascii file, e.g.
//  HGeant, UrQMD, etc. files.
//  The constructor opens the file in read-only mode.
//  The PChannel::decay() function reads then one event and fills ptcls[]
//
//                    Author:  Romain Holzmann
//                    Written: 08/11/2002
//                    Revised: 08/11/2002 RH
//                    Revised: 22/03/2005 RH
//                    Revised: 25/08/2012 RH
//
////////////////////////////////////////////////////////

#include "PUtils.h"
#include "PFileInput.h"

ClassImp(PFileInput)

PFileInput::PFileInput(Char_t *mode, Char_t *filename) : PParticle("File") {
// 
// presently mode can be "HGeant" 
//
    if (strcmp(mode,"HGeant")==0) 
	imode = 1;  // HGeant event input format 
    else if (strcmp(mode,"UrQMD")==0) 
	imode = 2;  // UrQMD format (not impl.)
    else if (strcmp(mode,"HSM")==0) 
	imode = 3;  // HSM format (not impl.)
    else 
	imode = 0;
    name = mode;
    part = NULL;
    fp   = fopen(filename,"r");   // open event file
    
    if (imode==0 || fp==NULL)
	Error("PFileInput", "Error in PFileInput constructor");
    SetPxPyPzE(0., 0., 0., 1.);
    wt   = 1.;
    file = filename;
}

void PFileInput::setToMidrapidity(float agev) {  // cm->lab transformation
    // midrapidity with agev the energy/nucleon of the projectile (in GeV/u)
    if (agev <= 0.0) return;
    double bx = 0.;
    double by = 0.;
    double bz = sqrt(agev/(agev+2.*0.9315));
    Boost(bx, by, bz);
}

Int_t PFileInput::readEventHeader(Float_t &b) {  // read event header
   Int_t ret, evNb, nPar;
   Float_t Ebeam;
   switch (imode) {
   case 1:
       ret = fscanf(fp,"%i %i %f %f %i", &evNb, &nPar, &Ebeam, &b, &flag);
       if (ret == EOF) return -1;
       else return nPar;
       break;
   case 2:  // not implemented
       break;
   case 3:  // not implemented
       break;
   default: ;
   }
   return -1;
}

Int_t PFileInput::readParticle(Double_t &px, Double_t &py, Double_t &pz, Double_t &E,  
			       Double_t &vx, Double_t &vy, Double_t& vz, Double_t &vt,
			       Int_t &Id, Int_t &srcId, Int_t &parId, Int_t &parInd, Double_t &weight) {
    Int_t ret = -1;
    switch (imode) {
    case 1:
	vx = vy = vz = vt = 0.;
	// flag decides how much info is read per particle
	if (flag == 0) 
	    ret = fscanf(fp,"%le %le %le %le %i", &E, &px, &py, &pz, &Id);
	else if (flag == 1) 
	    ret = fscanf(fp,"%le %le %le %le %i %le",
			 &E, &px, &py, &pz, &Id, &weight); // + weight
	else if (flag == 2) 
	    ret = fscanf(fp,"%le %le %le %le %i %i %i %le",
			 &E, &px, &py, &pz, &Id, &srcId, &parId, &weight); // + ids
	else if (flag == -2) 
	    ret = fscanf(fp,"%le %le %le %le %i %i %i %i %le",
			 &E, &px, &py, &pz, &Id, &srcId, &parId, &parInd, &weight); // + ids
	else if (flag == 3) 
	    ret = fscanf(fp,"%le %le %le %le %le %le %le %i %i %i %le",
			 &E, &px, &py, &pz, &vx, &vy, &vz, &Id, &srcId, &parId, &weight); // + vertex
	else if (flag == -3) 
	    ret = fscanf(fp,"%le %le %le %le %le %le %le %i %i %i %i %le",
			 &E, &px, &py, &pz, &vx, &vy, &vz, &Id, &srcId, &parId, &parInd, &weight); // + vertex
	else if (flag == 4) 
	    ret = fscanf(fp,"%le %le %le %le %le %le %le %le %i %i %i %le",
			 &E, &px, &py, &pz, &vx, &vy, &vz, &vt, &Id, &srcId, &parId, &weight); // + time
	else if (flag == -4) 
	    ret = fscanf(fp,"%le %le %le %le %le %le %le %le %i %i %i %i %le",
			 &E, &px, &py, &pz, &vx, &vy, &vz, &vt, &Id, &srcId, &parId, &parInd, &weight); // + time
	break;
    case 2:  // not implemented
	break;
    case 3:  // not implemented
	break;
    default: ;
    }
    if (E < 0.0) 
	E = -E;
    if (ret == EOF) 
	ret = -1;
    return ret;
}

void PFileInput:: Print(const Option_t *) const {
    printf(" Input type: %s\n", name);
    printf(" File name: %s\n",  file);
    printf(" rapidity=%f  weight=%f\n", Rapidity(), W());
}

PChannel *PFileInput::makeChannel(Int_t nMax, Float_t Ebeam) {
    //
    // set up a reaction channel for this interface
    //
    if (Ebeam > 0.) setToMidrapidity(Ebeam);
    
    part = new PParticle*[nMax+1];
    part[0] = this;
    for (Int_t i=1; i<=nMax; i++) {
	part[i] = new PParticle((char*)makeStaticData()->GetParticleName(7)); 
    }
    PChannel *chan = new PChannel(part, nMax, 1);
    return chan;
}




 PFileInput.cc:1
 PFileInput.cc:2
 PFileInput.cc:3
 PFileInput.cc:4
 PFileInput.cc:5
 PFileInput.cc:6
 PFileInput.cc:7
 PFileInput.cc:8
 PFileInput.cc:9
 PFileInput.cc:10
 PFileInput.cc:11
 PFileInput.cc:12
 PFileInput.cc:13
 PFileInput.cc:14
 PFileInput.cc:15
 PFileInput.cc:16
 PFileInput.cc:17
 PFileInput.cc:18
 PFileInput.cc:19
 PFileInput.cc:20
 PFileInput.cc:21
 PFileInput.cc:22
 PFileInput.cc:23
 PFileInput.cc:24
 PFileInput.cc:25
 PFileInput.cc:26
 PFileInput.cc:27
 PFileInput.cc:28
 PFileInput.cc:29
 PFileInput.cc:30
 PFileInput.cc:31
 PFileInput.cc:32
 PFileInput.cc:33
 PFileInput.cc:34
 PFileInput.cc:35
 PFileInput.cc:36
 PFileInput.cc:37
 PFileInput.cc:38
 PFileInput.cc:39
 PFileInput.cc:40
 PFileInput.cc:41
 PFileInput.cc:42
 PFileInput.cc:43
 PFileInput.cc:44
 PFileInput.cc:45
 PFileInput.cc:46
 PFileInput.cc:47
 PFileInput.cc:48
 PFileInput.cc:49
 PFileInput.cc:50
 PFileInput.cc:51
 PFileInput.cc:52
 PFileInput.cc:53
 PFileInput.cc:54
 PFileInput.cc:55
 PFileInput.cc:56
 PFileInput.cc:57
 PFileInput.cc:58
 PFileInput.cc:59
 PFileInput.cc:60
 PFileInput.cc:61
 PFileInput.cc:62
 PFileInput.cc:63
 PFileInput.cc:64
 PFileInput.cc:65
 PFileInput.cc:66
 PFileInput.cc:67
 PFileInput.cc:68
 PFileInput.cc:69
 PFileInput.cc:70
 PFileInput.cc:71
 PFileInput.cc:72
 PFileInput.cc:73
 PFileInput.cc:74
 PFileInput.cc:75
 PFileInput.cc:76
 PFileInput.cc:77
 PFileInput.cc:78
 PFileInput.cc:79
 PFileInput.cc:80
 PFileInput.cc:81
 PFileInput.cc:82
 PFileInput.cc:83
 PFileInput.cc:84
 PFileInput.cc:85
 PFileInput.cc:86
 PFileInput.cc:87
 PFileInput.cc:88
 PFileInput.cc:89
 PFileInput.cc:90
 PFileInput.cc:91
 PFileInput.cc:92
 PFileInput.cc:93
 PFileInput.cc:94
 PFileInput.cc:95
 PFileInput.cc:96
 PFileInput.cc:97
 PFileInput.cc:98
 PFileInput.cc:99
 PFileInput.cc:100
 PFileInput.cc:101
 PFileInput.cc:102
 PFileInput.cc:103
 PFileInput.cc:104
 PFileInput.cc:105
 PFileInput.cc:106
 PFileInput.cc:107
 PFileInput.cc:108
 PFileInput.cc:109
 PFileInput.cc:110
 PFileInput.cc:111
 PFileInput.cc:112
 PFileInput.cc:113
 PFileInput.cc:114
 PFileInput.cc:115
 PFileInput.cc:116
 PFileInput.cc:117
 PFileInput.cc:118
 PFileInput.cc:119
 PFileInput.cc:120
 PFileInput.cc:121
 PFileInput.cc:122
 PFileInput.cc:123
 PFileInput.cc:124
 PFileInput.cc:125
 PFileInput.cc:126
 PFileInput.cc:127
 PFileInput.cc:128
 PFileInput.cc:129
 PFileInput.cc:130
 PFileInput.cc:131
 PFileInput.cc:132
 PFileInput.cc:133
 PFileInput.cc:134
 PFileInput.cc:135
 PFileInput.cc:136
 PFileInput.cc:137
 PFileInput.cc:138
 PFileInput.cc:139
 PFileInput.cc:140