/////////////////////////////////////////////////////////////////////
//
// Generic distribution for 3body decay
// used so far for eta -> pi+pi-pi0
//
//                                  Author:  I. Froehlich
/////////////////////////////////////////////////////////////////////


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

#include "PDalitzDistribution.h"
#include "PDynamicData.h"

ClassImp(PDalitzDistribution)

PDalitzDistribution::PDalitzDistribution() {
    Fatal("PDalitzDistribution", "Wrong ctor called");
};

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

    primary = NULL;
    parent  = NULL;
    projector   = NULL;
    max = 0.0;
};

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

void PDalitzDistribution::MakeVars() {
    if (!projector) projector = new PProjector();

    vprimary   = makeDynamicData()->GetBatchParticle("_primary"); 
    vs1        = makeDynamicData()->GetBatchParticle("_s1"); 
    vs2        = makeDynamicData()->GetBatchParticle("_s2"); 
 
    vf         = makeStaticData()->GetBatchValue("_f"); 
};

Bool_t PDalitzDistribution::AddEquation(const char *command) {
    MakeVars();
    return projector->AddCommand(command);
};

Bool_t PDalitzDistribution::AddHistogram(TH2 *histo, const char *command) {
    MakeVars();
    return projector->AddHistogram(histo, command, 0);
};

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

    //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;
    }

    int side = 0;

    if ((side_particle[0] = GetParticle("s1"))) side++;
    if ((side_particle[1] = GetParticle("s2"))) side++;

    if (!side_particle[0] && (side_particle[0] = GetParticle("daughter"))) side++;
    if (!side_particle[1] && (side_particle[1] = GetParticle("daughter"))) side++;

    if (side != 2) {
	Warning("Init","Less or more than 2 additional found");
	return kFALSE;
    }

    return kTRUE;    
};

Bool_t PDalitzDistribution::Prepare(void) {
    return kTRUE;
};

Bool_t PDalitzDistribution::Finalize(void) {
    return kTRUE;
};

Bool_t PDalitzDistribution::CheckAbort(void) {
    return kFALSE;
};

Bool_t PDalitzDistribution::IsNotRejected(void) {
    
    // eta -> pi+ pi- pi0
    // see e.g. Ref. 8 

    double factor = 1.;

    if (projector) {	
	*vprimary = primary;
	*vs1 = *side_particle[0];
	*vs2 = *side_particle[1];

	//projector->Execute();
	Int_t num = 0;
	projector->Modify(NULL, NULL, &num, 0);
	factor = *vf;
    } else {
	double eta_Q = parent->M() // eta
	    - primary->M()
	    - side_particle[0]->M()
	    - side_particle[1]->M();
		
	double pi0_T = primary->E() - primary->M();
	
	double dalitz_y = (3*pi0_T / eta_Q) -1;
	factor = 1 + slope1*dalitz_y + slope2*dalitz_y*dalitz_y;
    }
    
    if (factor > max) Warning("IsNotRejected", "Dalitz factor > max");
 
    if ((factor/max) > PUtils::sampleFlat()) return kTRUE; // sample now angular distribution
    
    return kFALSE;
};


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