// Author: I. Froehlich
// Written: 3.7.2006
// Revised: 
// Base class for common distributions

#ifndef _PDISTRIBUTION_H_
#define _PDISTRIBUTION_H_

#define MAX_PARTICLE_LIST              10
#define PARTICLE_LIST_DAUGHTER         1
#define PARTICLE_LIST_PARENT           2
#define PARTICLE_LIST_GRANDPARENT      4
#define PARTICLE_LIST_GRANDGRANDPARENT 8
#define PARTICLE_LIST_GRANDDAUGHTER    16
#define PARTICLE_LIST_SIBLING          128

#define DISTRIBUTION_QUASI_PID     1000
#define DISTRIBUTION_SOMETHING_PID 2000
#define DISTRIBUTION_NUCLEON_PID   3000

//Flags for sampling, generator, etc.
#define VERSION_SAMPLING          1
#define VERSION_WEIGHTING         2
#define VERSION_INVERT_WEIGHTING  4
#define VERSION_MASS_SAMPLING     128
#define VERSION_IS_PRIMARY        256
#define VERSION_GENERATOR_MC      512
#define VERSION_NO_PHASE_SPACE    1024
#define VERSION_FORCE_NO_PARTIAL_WIDTH    2048

#include "TF1.h"
#include "TF2.h"
#include "PParticle.h"
#include "PData.h"
#include "PUtils.h"


class PDistribution : public TF1 {
  
 public:

    PDistribution();
    PDistribution(const Char_t *id, const Char_t *de);
    virtual ~PDistribution();
  
    virtual PDistribution* Clone(const char *delme=NULL) const;

    Int_t Add(const Char_t *opt);
    Int_t Add(const Char_t *name, int flag, 
	      const Char_t *pflag);  //Adds a particle placehoulder to the list
  
    Int_t Add(const Char_t *name, const Char_t *flag1, 
	      const Char_t *flag2 = NULL, const Char_t *flag3 = NULL);
    //Helper function
  
    Int_t Set(const Char_t *opt);
    Int_t Set(const Char_t *name, const Char_t *pflag); //Like Add(), but rather replace it

    void Reset(void);
  
    Int_t SetParticle(PParticle *part, int pid, int flag); 
    //Replaces the pointer for the given particle
    //This function should be called from PChannel

    void ResetRelatives(Int_t flag = 0);
    const char *GetGroupID(void) { //Some wish where I want to go to...
	return group;
    };
    void SetGroupID(const char *gr) {
	group=gr;
    }

    PParticle *GetParticle(const Char_t *pflag=NULL); //get the particly with the private flag 
  
    Int_t GetStatus(void); //returns 0 if all particles are set
    Int_t CheckDaughters(void) ; //returns 0 if all daughters are set
    void  NoDaughters(void) {no_daughters=1;};
    void  ResetStatus(void);  //Clean setted particles
  
    virtual void Print(const Option_t *delme=NULL) const ;  //Debug info
    void BasePrint(void) const ;  //Debug info
    const Char_t *OptString(void) {return opt_string;};
    virtual void SubPrint(Int_t opt) const ;  //Print sub-models
    const Char_t *Path(void) const; //print path for models

    const Char_t *GetIdentifier(void)  {return identifier;};
    const Char_t *GetDescription(void) {return description;};

    void  SetEnable(Int_t en)    {enable=en;};
    Int_t GetEnable(void)        {return enable;};
    void  SetActivated(Int_t en) {is_activated=en;};
    Int_t GetActivated(void)     {return is_activated;};
    
    void  SetVersionFlag(Int_t f)   {version_flag |= f;};
    void  ClearVersionFlag(Int_t f) {version_flag &= (~f);};
    UInt_t GetVersionFlag(UInt_t f = 0xffffffff)  const {
	return version_flag & f;
    };
    

    /* the following tool functions set different versions */
    void DisableSampling(void) {
	ClearVersionFlag(VERSION_SAMPLING);
    }; //No Sampling in the GenBod
    
    void EnableGenerator(void) {
	ClearVersionFlag(VERSION_WEIGHTING);
	SetVersionFlag(VERSION_INVERT_WEIGHTING);
    }; //Use distribution as a generator

    void EnableWeighting(void) {
	ClearVersionFlag(VERSION_SAMPLING);
	SetVersionFlag(VERSION_WEIGHTING);
    }; //Use distribution as a generator
    
    virtual Bool_t FreezeOut(void);

    virtual Bool_t Init(void);

    virtual Bool_t Prepare(void);
 
    virtual Bool_t SampleMass(void);

    virtual Bool_t SampleMomentum(void);
  
    virtual Bool_t SampleAngle(void);

    virtual Bool_t IsNotRejected(void);

    virtual Bool_t CheckAbort(void);

    virtual Bool_t Finalize(void);

    virtual Bool_t EndOfChain(void);
 
    virtual Double_t GetWeight(void);

    virtual Bool_t WriteDebugInfo(PParticle *par);

    Int_t GetKey(void) {
	return primary_key;
    };
    void SetDecayIdx(Int_t opt) {didx_option=opt;};

    virtual int GetDepth(int i=0);

    int LinkDBdone(void)   {return linkdbdone;};
    void LinkDBdone(int i) {linkdbdone=i;};

    void IncrementWeightSum(Double_t w, Double_t sc=1.) {
//	if (w_num && (sc>(w_num  *1.1))) return;

	w_sum += w*sc;
	w_num += sc;
    };

    Double_t GetExpectedWeightMean() {
	//For PChannelModels: ExpectedWeightMean is set to the
	//static branching ratio

	return exp_w_mean;
    };

    void SetExpectedWeightMean(Double_t w) {
	exp_w_mean = w;
    };

    Double_t CalcWeightMean() {
	return w_sum/w_num;
    }

    void SetWeightMax(Double_t w) {
	w_max = w;
    }

    Double_t GetWeightMax(void) {
	return w_max;
    }

    Double_t GetDynamicRange() {
	//Used to get the Delta_x for the Monte-Carlo Integral
	return dynamic_range;
    };

    Bool_t Exec(const char * command); 
    virtual Bool_t ExecCommand(const char *command, Double_t value); 

    Int_t  debug_flag;  

    void PreParticles(void) {preliminary_particles = 1;};

    void SetDrawScaling(Double_t my_draw_scaling) {draw_scaling = my_draw_scaling;};

 protected:
  
    const Char_t *identifier, *description;
    Int_t enable, is_activated;
    char *parse[4];  //Option strings
    Int_t parse_s; //max 4 options

    PParticle    *particle[MAX_PARTICLE_LIST];
    const Char_t *names[MAX_PARTICLE_LIST];
    const Char_t *private_flag[MAX_PARTICLE_LIST];
    const Char_t *private_flag_int[MAX_PARTICLE_LIST];
    const Char_t *opt_string;  
    Int_t particle_flag[MAX_PARTICLE_LIST], pid[MAX_PARTICLE_LIST];
    Int_t position, current_flag;

    Int_t primary_key;   //Model coupled to data base key
    Int_t model_def_key; //Model type definition key
    Int_t sec_key;       //Key if secondary model (tmp)

    Int_t didx_option;  //Model has target didx

    Int_t preliminary_particles; //Used for auto-Adds in PChannelModel

    Int_t GetFlag(const Char_t *flag);
    const Char_t *group;     //Group ID
    Int_t linkdbdone;
    Int_t no_daughters;
    UInt_t version_flag, relative_warning;

    Double_t w_sum, exp_w_mean, w_num, dynamic_range; //For weighting method
    Double_t w_max;
    Double_t draw_scaling;

    ClassDef(PDistribution,0) //Base class for all distributions

};

#endif


	
 PDistribution.h:1
 PDistribution.h:2
 PDistribution.h:3
 PDistribution.h:4
 PDistribution.h:5
 PDistribution.h:6
 PDistribution.h:7
 PDistribution.h:8
 PDistribution.h:9
 PDistribution.h:10
 PDistribution.h:11
 PDistribution.h:12
 PDistribution.h:13
 PDistribution.h:14
 PDistribution.h:15
 PDistribution.h:16
 PDistribution.h:17
 PDistribution.h:18
 PDistribution.h:19
 PDistribution.h:20
 PDistribution.h:21
 PDistribution.h:22
 PDistribution.h:23
 PDistribution.h:24
 PDistribution.h:25
 PDistribution.h:26
 PDistribution.h:27
 PDistribution.h:28
 PDistribution.h:29
 PDistribution.h:30
 PDistribution.h:31
 PDistribution.h:32
 PDistribution.h:33
 PDistribution.h:34
 PDistribution.h:35
 PDistribution.h:36
 PDistribution.h:37
 PDistribution.h:38
 PDistribution.h:39
 PDistribution.h:40
 PDistribution.h:41
 PDistribution.h:42
 PDistribution.h:43
 PDistribution.h:44
 PDistribution.h:45
 PDistribution.h:46
 PDistribution.h:47
 PDistribution.h:48
 PDistribution.h:49
 PDistribution.h:50
 PDistribution.h:51
 PDistribution.h:52
 PDistribution.h:53
 PDistribution.h:54
 PDistribution.h:55
 PDistribution.h:56
 PDistribution.h:57
 PDistribution.h:58
 PDistribution.h:59
 PDistribution.h:60
 PDistribution.h:61
 PDistribution.h:62
 PDistribution.h:63
 PDistribution.h:64
 PDistribution.h:65
 PDistribution.h:66
 PDistribution.h:67
 PDistribution.h:68
 PDistribution.h:69
 PDistribution.h:70
 PDistribution.h:71
 PDistribution.h:72
 PDistribution.h:73
 PDistribution.h:74
 PDistribution.h:75
 PDistribution.h:76
 PDistribution.h:77
 PDistribution.h:78
 PDistribution.h:79
 PDistribution.h:80
 PDistribution.h:81
 PDistribution.h:82
 PDistribution.h:83
 PDistribution.h:84
 PDistribution.h:85
 PDistribution.h:86
 PDistribution.h:87
 PDistribution.h:88
 PDistribution.h:89
 PDistribution.h:90
 PDistribution.h:91
 PDistribution.h:92
 PDistribution.h:93
 PDistribution.h:94
 PDistribution.h:95
 PDistribution.h:96
 PDistribution.h:97
 PDistribution.h:98
 PDistribution.h:99
 PDistribution.h:100
 PDistribution.h:101
 PDistribution.h:102
 PDistribution.h:103
 PDistribution.h:104
 PDistribution.h:105
 PDistribution.h:106
 PDistribution.h:107
 PDistribution.h:108
 PDistribution.h:109
 PDistribution.h:110
 PDistribution.h:111
 PDistribution.h:112
 PDistribution.h:113
 PDistribution.h:114
 PDistribution.h:115
 PDistribution.h:116
 PDistribution.h:117
 PDistribution.h:118
 PDistribution.h:119
 PDistribution.h:120
 PDistribution.h:121
 PDistribution.h:122
 PDistribution.h:123
 PDistribution.h:124
 PDistribution.h:125
 PDistribution.h:126
 PDistribution.h:127
 PDistribution.h:128
 PDistribution.h:129
 PDistribution.h:130
 PDistribution.h:131
 PDistribution.h:132
 PDistribution.h:133
 PDistribution.h:134
 PDistribution.h:135
 PDistribution.h:136
 PDistribution.h:137
 PDistribution.h:138
 PDistribution.h:139
 PDistribution.h:140
 PDistribution.h:141
 PDistribution.h:142
 PDistribution.h:143
 PDistribution.h:144
 PDistribution.h:145
 PDistribution.h:146
 PDistribution.h:147
 PDistribution.h:148
 PDistribution.h:149
 PDistribution.h:150
 PDistribution.h:151
 PDistribution.h:152
 PDistribution.h:153
 PDistribution.h:154
 PDistribution.h:155
 PDistribution.h:156
 PDistribution.h:157
 PDistribution.h:158
 PDistribution.h:159
 PDistribution.h:160
 PDistribution.h:161
 PDistribution.h:162
 PDistribution.h:163
 PDistribution.h:164
 PDistribution.h:165
 PDistribution.h:166
 PDistribution.h:167
 PDistribution.h:168
 PDistribution.h:169
 PDistribution.h:170
 PDistribution.h:171
 PDistribution.h:172
 PDistribution.h:173
 PDistribution.h:174
 PDistribution.h:175
 PDistribution.h:176
 PDistribution.h:177
 PDistribution.h:178
 PDistribution.h:179
 PDistribution.h:180
 PDistribution.h:181
 PDistribution.h:182
 PDistribution.h:183
 PDistribution.h:184
 PDistribution.h:185
 PDistribution.h:186
 PDistribution.h:187
 PDistribution.h:188
 PDistribution.h:189
 PDistribution.h:190
 PDistribution.h:191
 PDistribution.h:192
 PDistribution.h:193
 PDistribution.h:194
 PDistribution.h:195
 PDistribution.h:196
 PDistribution.h:197
 PDistribution.h:198
 PDistribution.h:199
 PDistribution.h:200
 PDistribution.h:201
 PDistribution.h:202
 PDistribution.h:203
 PDistribution.h:204
 PDistribution.h:205
 PDistribution.h:206
 PDistribution.h:207
 PDistribution.h:208
 PDistribution.h:209
 PDistribution.h:210
 PDistribution.h:211
 PDistribution.h:212
 PDistribution.h:213
 PDistribution.h:214
 PDistribution.h:215
 PDistribution.h:216
 PDistribution.h:217
 PDistribution.h:218
 PDistribution.h:219
 PDistribution.h:220
 PDistribution.h:221
 PDistribution.h:222
 PDistribution.h:223
 PDistribution.h:224
 PDistribution.h:225
 PDistribution.h:226
 PDistribution.h:227
 PDistribution.h:228
 PDistribution.h:229
 PDistribution.h:230
 PDistribution.h:231
 PDistribution.h:232
 PDistribution.h:233
 PDistribution.h:234