using namespace std;
#include <sstream>
#include <iostream>
#include <iomanip>
#include "PSimpleVMDFF.h"
ClassImp(PSimpleVMDFF)
PSimpleVMDFF::PSimpleVMDFF() {
};
PSimpleVMDFF::PSimpleVMDFF(const Char_t *id, const Char_t *de, Int_t key) :
    PChannelModel (id, de, key) {
    dilepton = dilepton2 = parent = NULL;
    vector_meson_mass2 = -1;
    batch = NULL;
};
PDistribution* PSimpleVMDFF::Clone(const char *) const {
    return new PSimpleVMDFF((const PSimpleVMDFF &)* this);
};
Bool_t PSimpleVMDFF::AddEquation(const char *command) {
    if (!batch) batch = new PBatch();
    vq   = makeStaticData()->GetBatchValue("_q"); 
    vq2  = makeStaticData()->GetBatchValue("_q2"); 
    vff2 = makeStaticData()->GetBatchValue("_ff2"); 
    return batch->AddCommand(command);
}
Bool_t PSimpleVMDFF::Init(void) {
    dilepton = GetParticle("dilepton");
    if (!dilepton) {
	dilepton = GetParticle("dimuon");
    }
    if (!dilepton) {
	ep = GetParticle("e+");
	em = GetParticle("e-");
    }
    if (!dilepton && !(ep && em) ) {
	Error("Init", "No dilepton found");
	return kFALSE;
    }
    
    dilepton2 = GetParticle("dilepton");
    if (!dilepton2) {
	dilepton2 = GetParticle("dimuon");
    }
    parent = GetParticle("parent");
    if (!parent)  {
	    Error("Init", "No parent found");
	    return kFALSE;
    }
    if (vector_meson_mass2<0 && !batch) {
	Error("Init", "Vector meson mass OR eqn. must be initialized");
	return kFALSE;
    } 
    return kTRUE;    
};
Double_t PSimpleVMDFF::GetWeight(void) {
    Double_t q = 0;
    if (dilepton)
	q = dilepton->M();
    else {
	TLorentzVector dil = (*(TLorentzVector*)ep)+(*(TLorentzVector*)em);
	q = dil.M();
    }
	
    Double_t pmass = parent->M();
    Double_t ff = GetWeight(q, pmass);
    if (dilepton2) {
	Double_t q = dilepton2->M();
	ff *= GetWeight(q,pmass);
    }
    return ff;
};
Double_t PSimpleVMDFF::GetWeight(Double_t *mass, Int_t *) {
    Double_t q2 = mass[0]*mass[0];
    if (batch) {	
	*vq  = mass[0];
	*vq2 = q2;
	batch->Execute();
	return *vff2;
    }
    Double_t ff = vector_meson_mass2/(vector_meson_mass2 - q2);
    return ff*ff;
};