/////////////////////////////////////////////////////////////////////
//
// Scattering angular distribution
// This algorithm evaluates the angular distribution of
// the particle "primary" which must be a "DAUGHTER"
//
// It works similar to AngularDistrubution, but the reference vector
// is the scattering OBE between the beam and the target.
//
//                                  Author:  I. Froehlich
/////////////////////////////////////////////////////////////////////


using namespace std;
#include <sstream>
#include <iostream>
#include <iomanip>

#include "PScatterDistribution.h"
#include "PKinematics.h"


ClassImp(PScatterDistribution)

PScatterDistribution::PScatterDistribution()  {
};

PScatterDistribution::PScatterDistribution(const Char_t *id, const Char_t *de) :
    PDistribution(id, de) {

    angles1 = NULL;
    angles2 = NULL;
    beam    = NULL;
    target  = NULL;
    parent  = NULL;
};

PDistribution *PScatterDistribution::Clone(const char *) const {
    return new PScatterDistribution((const PScatterDistribution &)* this);
};


Bool_t PScatterDistribution::Init(void) {

    //is the needed function set?
    if ((angles1 == NULL ) && (angles2 == NULL )) {
	Warning("Init","Angular distribution not found");
	return kFALSE;
    }

    //looking for primary. This is mandatory
    primary = GetParticle("primary");
    if (!primary) {
	Warning("Init", "Primary not found");
	return kFALSE;
    }
    //primary->Print();

    beam   = GetParticle("beam");
    target = GetParticle("target");

    //now get the parent
    for (int i=0; i<position; i++) {
	if (particle_flag[i] == PARTICLE_LIST_PARENT)
	    parent=particle[i];
    }

    if (!parent) {
	Warning("Init", "Parent not found");
	return kFALSE;
    }
    return kTRUE;    
};


Bool_t PScatterDistribution::IsNotRejected(void) {
       
    PParticle tmp_primary(primary);  //particle under investigation. Make better a copy
    PParticle tmp_parent(parent);    //particle under investigation. Make better a copy

    tmp_primary.Boost(parent->BoostVector());  // go back to lab frame

    //Thats OK if we are in t-channel:
    PParticle tmp_beam(beam);
    PParticle tmp_target(target);

    double sam = 0.5, sum = 1.;
    double t, u, tu, i;
    t  = 0.5;
    tu = 0;


    if (parent->GetValue(T_MATRIX, &t) && 
	parent->GetValue(U_MATRIX, &u) &&
	parent->GetValue(TU_MATRIX, &tu)&&
	parent->GetValue(CHANNEL_POS, &i) ) {
		
	//Calculate which process is more likely
	sum = t+u+tu;
    } 
    
    sam = PUtils::sampleFlat()*sum;
    
    if (sam > (t+0.5*tu)) { //u-channel      
	tmp_beam   = *target;
	tmp_target = *beam;
    }

    tmp_parent.Boost(-tmp_target.BoostVector()); //go into target system
    tmp_primary.Boost(-tmp_target.BoostVector()); //go into target system
    tmp_beam.Boost(-tmp_target.BoostVector());

    //now we have to make sure that the "beam" is pointing to z direction
    Double_t Phi   = tmp_beam.Phi();
    Double_t Theta = tmp_beam.Theta();
    tmp_parent.RotateZ(-Phi);
    tmp_parent.RotateY(-Theta);
    tmp_primary.RotateZ(-Phi);
    tmp_primary.RotateY(-Theta);

    //rotate such that parent (e.g. the Resonance) points to z
    Phi   = tmp_parent.Phi();
    Theta = tmp_parent.Theta();
    tmp_parent.RotateZ(-Phi);
    tmp_parent.RotateY(-Theta);
    tmp_primary.RotateZ(-Phi);
    tmp_primary.RotateY(-Theta);

    //now we have to boost the primary
    tmp_primary.Boost(-tmp_parent.BoostVector());

    Double_t tmp_c0 = cos(tmp_primary.Theta());
    Double_t f;

    

    if (angles1)
	f = angles1->Eval(tmp_c0);
    else 
	f = angles2->Eval(tmp_c0,tmp_parent.M());
 
    if (f > PUtils::sampleFlat()) 
	return kTRUE; // sample now angular distribution
    

    return kFALSE;

};


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