/////////////////////////////////////////////////////////////////////
//
// Calculates the total cross section weights for 
// a decay (a+b) -> something
//
//                                  Author:  I. Froehlich
/////////////////////////////////////////////////////////////////////


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

#include "PTCrossWeight.h"


ClassImp(PTCrossWeight)

PTCrossWeight::PTCrossWeight() {
};

PTCrossWeight::PTCrossWeight(const Char_t *id, const Char_t *de, Int_t key) :
    PChannelModel (id, de, key) {

    tcross   = NULL;
    tcrossg  = NULL;
    tcrossc  = 1.;
    spline   = kFALSE;
    g_spline =NULL;
    scaling  = 1.;
    isExcessEnergy = isMevEnergy = kFALSE;

    daughter_pos = 0;
    for (int i=0; i<TCROSS_MAX_DAUGHTERS; i++) {
	daughters[i] = NULL;
    }
    EnableWeighting();
    SetExpectedWeightMean(-1); //switch off re-normalization
};

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

Bool_t PTCrossWeight::Init(void) {

    //Get beam and target
    beam   = GetParticle("beam");
    target = GetParticle("target");
    parent = GetParticle("parent");
//     if (!beam || !target) {
// 	Warning("Init","Beam/target not found");
// 	return kFALSE;
//     }

    //Now get all daugthers
    for (int i=0; i<TCROSS_MAX_DAUGHTERS; i++) {
	daughters[i] = GetParticle("daughter");
	if (daughters[i]) {	    
	    daughter_pos++;
	}
    }
  
//    if (!tcross && !tcrossg) Fatal("Init","No function found");

    return kTRUE;    
};


Double_t PTCrossWeight::GetWeight(void) {

    Double_t q = parent->M();
    if (beam && target)
	q = ( *(TLorentzVector*)beam + *(TLorentzVector*)target).M();

    if (isExcessEnergy) {
	for (int i=0; i<daughter_pos; i++)
	    q -= daughters[i]->M();
    }
    
    if (isMevEnergy) 
	q *= 1000.;
    Double_t ret = GetWeight(&q);

//    cout << "Q:"  << q << ":" << ret << endl;
    return ret;
};

Double_t PTCrossWeight::GetWeight(Double_t *mass, Int_t *) {
    Double_t ret = 0;
    if (tcrossg) {
	ret = tcrossg->Eval(mass[0], g_spline, "");
	//We have to check that we are not below the first and the last point
	if (mass[0]<tcrossg->GetX()[0]) {
	    ret = tcrossg->GetY()[0];
	}
	if (mass[0]>((tcrossg->GetX())[tcrossg->GetN()-1])) {
	    ret = (tcrossg->GetY())[tcrossg->GetN()-1];
	}
    } else if (tcross)
	ret = tcross->Eval(mass[0]);
    else 
	ret = tcrossc;
    // cout << "ret:" << ret << endl;
    if (ret>0) return ret*scaling;
    return 0.;
}
 PTCrossWeight.cc:1
 PTCrossWeight.cc:2
 PTCrossWeight.cc:3
 PTCrossWeight.cc:4
 PTCrossWeight.cc:5
 PTCrossWeight.cc:6
 PTCrossWeight.cc:7
 PTCrossWeight.cc:8
 PTCrossWeight.cc:9
 PTCrossWeight.cc:10
 PTCrossWeight.cc:11
 PTCrossWeight.cc:12
 PTCrossWeight.cc:13
 PTCrossWeight.cc:14
 PTCrossWeight.cc:15
 PTCrossWeight.cc:16
 PTCrossWeight.cc:17
 PTCrossWeight.cc:18
 PTCrossWeight.cc:19
 PTCrossWeight.cc:20
 PTCrossWeight.cc:21
 PTCrossWeight.cc:22
 PTCrossWeight.cc:23
 PTCrossWeight.cc:24
 PTCrossWeight.cc:25
 PTCrossWeight.cc:26
 PTCrossWeight.cc:27
 PTCrossWeight.cc:28
 PTCrossWeight.cc:29
 PTCrossWeight.cc:30
 PTCrossWeight.cc:31
 PTCrossWeight.cc:32
 PTCrossWeight.cc:33
 PTCrossWeight.cc:34
 PTCrossWeight.cc:35
 PTCrossWeight.cc:36
 PTCrossWeight.cc:37
 PTCrossWeight.cc:38
 PTCrossWeight.cc:39
 PTCrossWeight.cc:40
 PTCrossWeight.cc:41
 PTCrossWeight.cc:42
 PTCrossWeight.cc:43
 PTCrossWeight.cc:44
 PTCrossWeight.cc:45
 PTCrossWeight.cc:46
 PTCrossWeight.cc:47
 PTCrossWeight.cc:48
 PTCrossWeight.cc:49
 PTCrossWeight.cc:50
 PTCrossWeight.cc:51
 PTCrossWeight.cc:52
 PTCrossWeight.cc:53
 PTCrossWeight.cc:54
 PTCrossWeight.cc:55
 PTCrossWeight.cc:56
 PTCrossWeight.cc:57
 PTCrossWeight.cc:58
 PTCrossWeight.cc:59
 PTCrossWeight.cc:60
 PTCrossWeight.cc:61
 PTCrossWeight.cc:62
 PTCrossWeight.cc:63
 PTCrossWeight.cc:64
 PTCrossWeight.cc:65
 PTCrossWeight.cc:66
 PTCrossWeight.cc:67
 PTCrossWeight.cc:68
 PTCrossWeight.cc:69
 PTCrossWeight.cc:70
 PTCrossWeight.cc:71
 PTCrossWeight.cc:72
 PTCrossWeight.cc:73
 PTCrossWeight.cc:74
 PTCrossWeight.cc:75
 PTCrossWeight.cc:76
 PTCrossWeight.cc:77
 PTCrossWeight.cc:78
 PTCrossWeight.cc:79
 PTCrossWeight.cc:80
 PTCrossWeight.cc:81
 PTCrossWeight.cc:82
 PTCrossWeight.cc:83
 PTCrossWeight.cc:84
 PTCrossWeight.cc:85
 PTCrossWeight.cc:86
 PTCrossWeight.cc:87
 PTCrossWeight.cc:88
 PTCrossWeight.cc:89
 PTCrossWeight.cc:90
 PTCrossWeight.cc:91
 PTCrossWeight.cc:92
 PTCrossWeight.cc:93
 PTCrossWeight.cc:94
 PTCrossWeight.cc:95
 PTCrossWeight.cc:96
 PTCrossWeight.cc:97
 PTCrossWeight.cc:98
 PTCrossWeight.cc:99
 PTCrossWeight.cc:100
 PTCrossWeight.cc:101
 PTCrossWeight.cc:102
 PTCrossWeight.cc:103
 PTCrossWeight.cc:104
 PTCrossWeight.cc:105
 PTCrossWeight.cc:106
 PTCrossWeight.cc:107
 PTCrossWeight.cc:108