#ifndef _PBEAMLINESIMULATION_H_
#define _PBEAMLINESIMULATION_H_

#include "PBeamSmearing.h"
#include "PBulkInterface.h"
#include "PProjector.h"

#define MAX_NUM_BEAM_PARTICLES 1000


class PBeamLineSimulation : public PBeamSmearing  {
  
 public:
    PBeamLineSimulation();
    ~PBeamLineSimulation();
    PBeamLineSimulation(Char_t *id, Char_t *de);
    
    PDistribution *Clone(const char *delme=NULL) const;

    Bool_t Init(void);
    Bool_t Prepare(void);

    Bool_t InitBeamLine(char *filename);
    Int_t  AddDetector (char *name, Double_t distance);

    Bool_t TargetIsElement(Int_t n);
    void   SetGlobalMomentum(Double_t p) {global_p = p;};

    Bool_t AddEquation(char *command);
    Bool_t AddHistogram(TH2 *histo, const char *command = "");
    Bool_t Do(char *command) {return AddEquation(command);};
    Bool_t Do(TH2 *histo, const char *command = "") {return AddHistogram(histo, command);};
    
    void SetBeamtubeSize(Double_t _beamtube_size_x, Double_t _beamtube_size_y) {
	beamtube_size_x = _beamtube_size_x;
	beamtube_size_y = _beamtube_size_y;
    };
    
 private:

    PParticle *beam,   *mybeam;
    PParticle *target, *mytarget;
    PParticle *parent;

    PParticle *beam_particles[MAX_NUM_BEAM_PARTICLES];
    Int_t      beam_id       [MAX_NUM_BEAM_PARTICLES];
    Int_t      beam_counter;

    Double_t  *filter;
    Double_t   beamtube_size_x, beamtube_size_y; //size in mm

    char      *detector_name[MAX_NUM_BEAM_PARTICLES];
    Double_t   detector_distance[MAX_NUM_BEAM_PARTICLES];
    Int_t      detector_counter;
    
    Int_t AddBeamParticle(char *name);
    void MakeVars(void);

    PProjector *projector;

    //taken from the original code:
    long double T11[51],T12[51],T13[51],T14[51],T16[51];
    long double T111[51],T112[51],T113[51],T114[51],T116[51];
    long double T122[51],T123[51],T124[51],T126[51];
    long double T133[51],T134[51],T136[51];
    long double T144[51],T146[51];
    long double T166[51],along[51];

    long double T21[51],T22[51],T23[51],T24[51],T26[51];      
    long double T211[51],T212[51],T213[51],T214[51],T216[51];
    long double T222[51],T223[51],T224[51],T226[51];
    long double T233[51],T234[51],T236[51];
    long double T244[51],T246[51];
    long double T266[51];      

    long double T31[51],T32[51],T33[51],T34[51],T36[51];
    long double T311[51],T312[51],T313[51],T314[51],T316[51];
    long double T322[51],T323[51],T324[51],T326[51];
    long double T333[51],T334[51],T336[51];
    long double T344[51],T346[51];
    long double T366[51];

    long double T41[51],T42[51],T43[51],T44[51],T46[51]; 
    long double T411[51],T412[51],T413[51],T414[51],T416[51];
    long double T422[51],T423[51],T424[51],T426[51];
    long double T433[51],T434[51],T436[51];
    long double T444[51],T446[51];
    long double T466[51];
           
    Int_t nelem; 

    char txt2[3]; 
    char txt5[6]; 
    
    long double z1; 
    long double z2; 
    long double z3; 
    long double z4; 
    long double z5; 
    
    long double xi; 
    long double xpi;
    long double yi; 
    long double ypi; 
    long double dp; 
    
    Int_t iok; 
    long double xf; 
    long double yf; 
    long double xpf; 
    long double ypf; 
    long double xf1; 
    long double yf1; 
    long double xpf1; 
    long double ypf1; 
    long double xf2; 
    long double xpf2; 
    long double yf2; 
    long double ypf2; 

    long double aa;
    long double sig11; 
    long double sig22; 
    long double r12; 
    long double sig33; 
    long double r13; 
    long double r23; 
    long double sig44;
    long double r14; 
    long double r24; 
    long double r34; 
    long double sig55; 
    long double r15; 
    long double r25; 
    long double r35; 
    long double r45; 
    long double sig66; 
    long double r16; 
    long double r26; 
    long double r36; 
    long double r46; 
    long double r56; 

    Int_t ii; 
    Int_t ind1; 
    Int_t ind23; 

    Int_t num_target;

    Double_t *in_xi;
    Double_t *in_yi;
    Double_t *in_dp;
    Double_t *in_xpi;
    Double_t *in_ypi;

    Double_t out_xi[51];
    Double_t out_yi[51];
    Double_t out_dp[51];
    Double_t out_xpi[51];
    Double_t out_ypi[51];
    Double_t global_p;

    ClassDef(PBeamLineSimulation, 0)  // General purpose beam line transport simulation
};

#endif


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