/////////////////////////////////////////////////////////////////////
//
// Mass sampling of the remaining mass "X" in inclusive reactions
// like Q->a+b+X
// The model is based on a TF1 function which should be valid
// between [0,1], which corresponds to the free energy
// (so 1 is the maximum always)
// Sampling is done for the "primary" particle
//
//                                  Author:  I. Froehlich
/////////////////////////////////////////////////////////////////////


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

#include "PInclusiveModel.h"
#include "PDynamicData.h"


ClassImp(PInclusiveModel)

PInclusiveModel::PInclusiveModel()  {
    Setup();
};

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

    Setup();
};

void PInclusiveModel::Setup(void) {
    sample = NULL;
    sample_d = 0.;
    parent = NULL;
    daughter_pos=0;
    for (int i=0; i<INCLUSIVE_MAX_DAUGHTERS; i++) {
	daughters[i]       = NULL;
	daughter_models[i] = NULL;
    }
}

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


Bool_t PInclusiveModel::Init(void) {

    daughter_pos = 0; //clear stuff because the Attach function makes a clone

    //is the needed function set?
//     if (sample == NULL ) {
// 	Warning("Init","Mass sampling distribution not found");
// 	return kFALSE;
//     }

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

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

    //Now get all daughters
    for (int i=0; i<INCLUSIVE_MAX_DAUGHTERS; i++) {
	daughters[i] = GetParticle("daughter");
	if (daughters[i]) {	    
	    daughter_pos++;
	}
    }
//    cout <<"daughter_pos"<< daughter_pos << endl;

    return kTRUE;    
};

int PInclusiveModel::GetDepth(int) {
    //check if we have models
    //This also initializes the sub-models
    //returns 0 in case of stable daughters BUGBUG->check all others

    Int_t a1 = -1; //Default is stable decay products
    
    for (int i=0; i<daughter_pos; i++) {
	daughter_models[i] = makeDynamicData()->GetParticleModel(daughters[i]->ID());
	if (daughter_models[i]) {
	    int b1 = daughter_models[i]->GetDepth(i);
	    a1     = TMath::Max(a1,b1);
	}
    }

    return a1; 
}

Bool_t PInclusiveModel::SampleMass(void) {
    //In the sampling function, we first call the sub-models to
    
    Double_t mass, total_mass = 0;
    for (int i=0; i<daughter_pos; i++) {
	if (daughter_models[i]) {
	    daughter_models[i]->SampleMass(&mass);
	    daughters[i]->SetM(mass);
	}
	total_mass += daughters[i]->M();
    }

    Double_t q = parent->M();
    if ((q-total_mass) < 0) {
	Warning("SampleMass", "Not enough energy");
	return kFALSE;
    }

    Double_t frac = sample_d;
    weight = 1.;

    if (sample) {
	
	frac=sample->GetRandom();
	weight = sample->Eval(frac);
    } 

    primary->SetM(frac*(q-total_mass));
    //cout << total_mass << ":" << q-total_mass  << ":" << frac*(q-total_mass) << endl;
    dynamic_range = q-total_mass;
    
    return kTRUE;
}

Double_t PInclusiveModel::GetWeight(void) {
    return weight;
    //BUGBUG: It can happen that sampleMass was not called
}


ClassImp(PInclusiveModel)

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