// Author: I. Froehlich
// Written: 23.5.2007
// Revised: 

#ifndef _PCHANNELMODEL_H_
#define _PCHANNELMODEL_H_

#include "TComplex.h"

#include "TF1.h"
#include "TF2.h"
#include "PDistribution.h"


class PChannelModel : public PDistribution {
  
 public:
    PChannelModel();
    PChannelModel(const Char_t *id, const Char_t *de, Int_t key=-1);
    PDistribution* Clone(const char*delme=NULL) const;

    using PDistribution::SampleMass;   

    virtual Bool_t SampleMass(Double_t *mass, Int_t *didx=NULL);

    Double_t SampleMass(Int_t didx) {
	Double_t mass;
	if (SampleMass(&mass, &didx)) return mass;
	return 0.;
    }
    Double_t SampleTotalMass(void) { 
	return SampleMass(-1); 
    } 


    //weight/amplitude calculations: To avoid endless loops,
    using PDistribution::GetWeight;   
    virtual Double_t GetWeight(Double_t *mass, Int_t *didx=NULL);
    
    Double_t GetWeight(Double_t mass, Int_t *didx=NULL) {
	//wrapper for models with single variable only
	return GetWeight(&mass, didx);
    };
    Double_t GetWeight(Double_t mass, Double_t mass2, Int_t *didx=NULL) {
	//wrapper for models with 2 variables only
	Double_t m[2];
	m[0] = mass;
	m[1] = mass2;
	return GetWeight(m, didx);
    };
    Double_t GetWeight(Double_t mass, Double_t mass2, Double_t mass3, Int_t *didx=NULL) {
	//wrapper for models with 3 variables
	Double_t m[3];
	m[0] = mass;
	m[1] = mass2;
	m[2] = mass3;
	return GetWeight(m, didx);
    };
    Double_t GetWeight(Double_t mass, Double_t mass2, Double_t mass3, Double_t mass4, 
		       Int_t *didx=NULL) {
	//wrapper for models with 4 variables
	Double_t m[4];
	m[0] = mass;
	m[1] = mass2;
	m[2] = mass3;
	m[3] = mass4;
	return GetWeight(m, didx);
    };
    Double_t GetWeight(Double_t mass, Double_t mass2, Double_t mass3, Double_t mass4, Double_t mass5, 
		       Int_t *didx=NULL) {
	//wrapper for models with 5 variables
	Double_t m[5];
	m[0] = mass;
	m[1] = mass2;
	m[2] = mass3;
	m[3] = mass4;
	m[4] = mass5;
	return GetWeight(m, didx);
    };

    virtual TComplex GetAmplitude(Double_t *mass, Int_t *didx=NULL);

    virtual Bool_t GetWidth(Double_t mass, Double_t *width, Int_t didx=-1);

    Double_t GetWidth(Double_t mass) {
	Double_t lWidth;
	GetWidth(mass, &lWidth, -1);
	return lWidth;
    };


    Double_t GetBR(Double_t mass) {
	Double_t br;
	Bool_t res = GetBR(mass, &br);
	if (res) return br;
	return 0.;
    }   


    virtual Bool_t GetBR(Double_t mass, Double_t *br, Double_t totalwidth=-1.);

    virtual int GetDepth(int i=0);
    // Returns the total number of embedded recursive decays 
    // (TDepth if flag=0) or the number of embedded recursive
    // hadronic decays (HDepth if flag=1).
    // The default value 0 means the index has not been accessed yet.
    // Zero depth corresponds to index -1.

    virtual Double_t Eval(Double_t x, Double_t y = 0, Double_t z = 0, Double_t t = 0) const;
    virtual Double_t EvalPar(const Double_t *x, const Double_t *params);
    //TF1 wrapper

    void SetDraw(Int_t opt) {
	SetParameter(0, (Double_t)opt);
	draw_option = opt;
    };
    void SetDidx(Int_t opt) {
	SetParameter(1, (Double_t)opt);
	didx_option = opt;
    };

    void SetDynamicRange(Double_t my_mmin,Double_t my_mmax) {
	mmin  = my_mmin;
	mmax  = my_mmax;
	fXmin = mmin;
	fXmax = mmax;
	fIntegral.clear();
    };

    Double_t GetMin(void)            {return mmin;};
    Double_t GetMax(void)            {return mmax;};
    void     SetMin(Double_t my_mmin){mmin = my_mmin;};
    void     SetMax(Double_t my_mmax){mmax = my_mmax;};

    void ClearIntegral(void) {
	if (!fIntegral.empty()) {
	    fIntegral.clear();
	    fAlpha.clear();
	    fBeta.clear();
	    fGamma.clear();
	}
    };

    Int_t GetDef() {return model_def_key;};
    void SetWidthInit (Int_t w) {width_init=w;};

 protected:
  
    Double_t width;     //Mass-dependent width container
    PMesh *mesh;        //Linear mesh container for fast lookup
    Int_t width_init;   //flag for the 1st initialization in getWidth
    Int_t is_channel, is_pid;
    Double_t mmin, mmax; //Dynamic range of the model
    Int_t maxmesh;
    Int_t draw_option;
    int mc_max, mc;      //Monte-Carlo intergration params
    Int_t didx_param, scfactor_param;
    Double_t *unstable_width;

    Int_t loop_flag; //To avoid endless loops of GetWidth/GetAmplitude calls

    PChannelModel *GetSecondaryModel(const char *);

    ClassDef(PChannelModel, 0)  // Base class for coupled-channel distributions
};

#endif


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