/////////////////////////////////////////////////////////////////////
//
//  Angular distributions in pi+p -> omega + X
//
//                                  Author: Johan Messchendorp
//                                  Reimplemented I. Froehlich
/////////////////////////////////////////////////////////////////////


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

#include "PPiOmegaAngularDistribution.h"

const long double pi = 3.14159265358979323846;

ClassImp(PPiOmegaAngularDistribution)

PPiOmegaAngularDistribution::PPiOmegaAngularDistribution() {
};

PPiOmegaAngularDistribution::PPiOmegaAngularDistribution(const Char_t *id, const Char_t *de) :
    PAngularDistribution(id, de) {
    beam    = NULL;
    target  = NULL;
    e_cm    = 0;
    version = 1;
};

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

Bool_t PPiOmegaAngularDistribution::IsNotRejected(void) {
    
    if (!direct_sampling_done) {
	Fatal("IsNotRejected", "Sampling not finished");
    }
    
    return kTRUE;
};

Bool_t PPiOmegaAngularDistribution::Init(void) {
    if (!PAngularDistribution::Init()) return kFALSE;

    //In addition we need the beam and target
    //done already in PAngularDistribution

    if (!beam || !target) {
	Warning("Init", "beam or target not found");
	return kFALSE;
    }

    return kTRUE;
}

Bool_t PPiOmegaAngularDistribution::Prepare(void) {
    getPiN_wN_param();
    return kTRUE;
}


void PPiOmegaAngularDistribution::getPiN_wN_param() {
    // energy-dependent parameters of the cm angular distribution 
    // for w production in pi+N --> N+w
    if (parent->M() != e_cm) 
	e_cm = parent->M();
    else return;                         // no change in values
    
    const double y[] = {exp(-8.),exp(-4.),exp(-3.2),exp(-2.4),1.},
	dx[] = {1.,.2,.2,.6}, dy[]={y[1]-y[0],y[2]-y[1],y[3]-y[2],y[4]-y[3]};
	int i;
	double h = .0069*exp(2.873*e_cm), // h parametrized by J.Messchendorp
	    a = 0.;   
	PiN_w_y[0] = 1.+h*y[0];
	PiN_w_h    = h;
	for (i=0; i<4; ++i) {
	    PiN_w_y[i+1]   = 1. + h*y[i+1];
	    PiN_w_slope[i] = h*dy[i] / dx[i];
	    a += dx[i] * (0.5*h*dy[i]+PiN_w_y[i]);
	    PiN_w_area[i] = a;
	}
}


double PPiOmegaAngularDistribution::SamplePolarAngle(double r) {
    const double xj[5] = {-1., 0., .2, .4, 1.};
    double rr, f=-1, tf=-1, r2, c0=-1, r1=r, a=-1, b=-1, area=-1, x1, x2, delta;
    int i, fl=0 ;

 again:
    if (version == PI_OMEGA_piNNw) {          // pi + N --> N + w, PRD2 (1970) 2564
	fl = 2;
	rr = r1*PiN_w_area[3];
	for (i=0; i<4; ++i) 
	    if (rr < PiN_w_area[i]) 
		break;
	area  = (i)?PiN_w_area[i-1]:0.;
	x1    = PiN_w_y[i];
	x2    = x1/PiN_w_slope[i];
	delta = 1.-2.*(area-rr)/(x1*x2);
	c0    = -x2*(1.-sqrt(delta))+xj[i];
	f     = 1.+PiN_w_h*exp(-4.+4.*c0);
	tf    = (c0-xj[i])*PiN_w_slope[i]+x1;
    } else if (version == PI_OMEGA_piPPpiw) {  // pi+ + P --> pi+ P + w,
	// Alff PR 145 (1966) 361
	fl = 3;
	c0 = log((r1*21.1545+3.31)/9.);
	f  = 3. + exp(3.*c0);
	tf = 9. * exp(c0); c0 = -c0;
    } else if (version==PI_OMEGA_piPDw) {      // pi+ + P --> pi+ P + w,
	// Alff PR 145 (1966) 361
	fl = 4;         
	c0 = log((r1*23.504+3.679)/10.);
	f  = 2. + exp(2.3*c0);
	tf = 10. * exp(c0); c0 = -c0;
    } else 
	return c0 = PUtils::sampleFlat()*2.-1.; // leave it isotropic for the moment
    
    r2 = PUtils::sampleFlat();
    if (r2 > f/tf) {                        // reject
	r1 = PUtils::sampleFlat();
	goto again;
    }
    if (tf < f) {                           // diagnostics
	Error("SamplePolarAngle", "algorithm failed %d\n", fl);
	Error("SamplePolarAngle", "a=%f b=%f: c0=%f tf=%f f=%f\n", a, b, c0, tf, f);
	r1 = PUtils::sampleFlat();
	goto again;
    }
    return c0;                            // accept
}

void PPiOmegaAngularDistribution::Print(const Option_t *) const{
    PAngularDistribution::Print();
}


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