// Author: M.A. Kagarlis
// Written: 21.01.99
// Revised: 05.12.05  by R. Holzmann
// Revised: 29.08.06  by R. Holzmann
// PChannel Class Header

#ifndef _PCHANNEL_H_
#define _PCHANNEL_H_

#include "TArrayI.h"
#include "TF1.h"
#include "TF2.h"
//#include "PSaid.h"
#include "PParticle.h"
#include "PDistribution.h"
#include "PStdData.h"
#include "PBulkInterface.h"
#include "PProjector.h"
//#include "PPlutoBulkDecay.h"

#define MAX_DISTRIBUTIONS 10
#define MAX_NUM_NOT_FINALIZED 5
#define MAX_BULKDECAY 5

#define STATUS_OK          0
#define STATUS_EMPTY_EVENT 80
#define STATUS_PSEUDO_EMPTY_EVENT 81
#define STATUS_REDO_CHAIN  82
#define STATUS_NOT_DECAYED 99



class PChannel : public TObject {
  
 public:

    PChannel(PParticle **, int nt=2, int mf=0, int af=0, int bf=0);
    // Channel constructor by particle array

    PChannel(int idx, PParticle **particles=NULL, int mf=0, int af=0, int bf=0);
    // Channel constructor by decay-mode index

    PChannel(int idx, PParticle &parent, int mf=0, int af=0, int bf=0);
    // Channel constructor by decay-mode index and parent reference

    ~PChannel() {
	// Channel destructor
	if (ipid.GetArray()) 
	    ipid.~TArrayI();
	// necessary, because of constr 2+3, for constr 1 also better
	if (ptcls && ptcls_extern>=0) {
	    for(int i=ptcls_extern; i<=n; i++)
		if (ptcls[i]) delete ptcls[i];
	    if (ptcls) delete [] ptcls;
	}
    }

    Bool_t Init();
    // sets on the first call all parent, siblings etc IDs. This
    // has to be done only once per PReaction

    Bool_t Reset();
    // cleans the channel, so an init will be done in the next decay()

    Bool_t SetDaughters();

    void SetSourceId(Int_t mysourceid) {
	sourceid = mysourceid;
    }

    int Decay();
    // N-body phase-space decay function based on the CERNLIB routine 
    // GENBOD. Resonance and dilepton masses (for Dalitz decays)
    // are sampled in PData. Scattering angles are sampled here, for
    // selected channels (see idChannel).

    int decay() {
	//kept for comp.
	return Decay();
    }; 

    PParticle **GetParticles() {
	// address of array of pointers to the channel particles
	return ptcls;
    }

    int *GetPids() {
	// address of array of channel-particle ids
	return ipid.GetArray();
    }   

    int GetNumPar() {  
	// number of decay products
	return n; 
    }
    
    //******for distribution handling:
    int SetDistribution(PDistribution *distribution);
    // return -1 if distribution does not match the channel
    int CheckSiblings(PParticle *p, PDistribution *dist, int flag); //check for siblings

    double GetBT() {
	PParticle *beam   = ptcls[0]->GetScattering(0);
	PParticle *target = ptcls[0]->GetScattering(1);
	
	if (beam) {
	    return (beam->KE() > target->KE()? beam->KE(): target->KE());
	}
	else return ptcls[0]->Rapidity();
    }

    //******for quasi-elastic handling:
    PChannel *GetQuasi(void) {
	return quasi_pchannel;
    };
    void ClearQuasi(void) {
	quasi_pchannel = NULL;
    };

    int GetParentSize() { 
	// 1 or 2 for elementary or quasi particles respectively
	return 1 + (ipid[0]>=1000); 
    }   

    static void SetGlobalWeight(double w) { 
	globalWeight = w; 
    } 
    static double GetGlobalWeight() { 
	return globalWeight; 
    }

    int GetDMIndex() { 
	//returns the decay mode index of this channel
	return DMIndex; 
    }

    void GetMessage();
    // status message

    void Print(const Option_t *delme="") const;
    void PrintReaction(Int_t check_key = 1) const;
    void PrintNew();
    
    void PrintReport() const; //Print a final report after sampling
    void CheckDecayKey() const;

    char const *GetName(void) const ;

//  double ds_dt(double);
    // ...> moved to PParticle (IF)
    // ds/dt(cos_th_cm) in the cm for N+N->N+Delta. With the mass of Delta
    // sampled independently in PData, the simulated events are distributed
    // as ds/dOmega (normalized). Ref: NPA459 (1986) 503

    void SetPrintTentative(bool t){ 
	// Print the tentative messages on the 1st call
	print_tentative = t;
    };

    void DisableHelicityAngle() {
	cout << "DisableHelicityAngle() is obsolete -> use DistributionManager" << endl;
    };

    //this is for envelope distributions:
    int GetNumNotFinalized() {
	return num_not_finalized;
    };
    PDistribution *GetDistributionNotFinalized(int i) {
	return distribution_not_finalized[i];
    };
    
    Bool_t AddBulk(PBulkInterface *mybulk);
    Bool_t AddPrologueBulk(PBulkInterface *mybulk); //Bulk IO before any decay

    Bool_t Do(const char *command) {
	return GetCurrentProjector()->AddCommand(command);
    }
    Bool_t Do(TH1F *f, const char *command, Int_t flag=1) {
	return GetCurrentProjector()->AddHistogram(f,command,flag);
    }
    Bool_t Do(TH2F *f, const char *command, Int_t flag=1) {
	return GetCurrentProjector()->AddHistogram(f,command,flag);
    }
    Bool_t Do(TH3F *f, const char *command, Int_t flag=1) {
	return GetCurrentProjector()->AddHistogram(f,command,flag);
    }

 private:

    int n, status, DMIndex;
    // number of products, error status
    // decay-mode index matching PData modes

    int thSrc, tcSrc, dlSrc, sourceid;

    Double_t weight_sum;   //Sum of all weights for debugging

    int decay_key;                       //! Key to data base entry
    TString decay_string; //! For parsing the decay string
    TString decay_string2; 

    // For generic distributions
    PDistribution *dist[MAX_DISTRIBUTIONS];
    Double_t dist_weight[MAX_DISTRIBUTIONS];
    Double_t dist_weight_sum[MAX_DISTRIBUTIONS];
    Double_t dist_counter[MAX_DISTRIBUTIONS];

    Int_t distribution_position;
    bool init_done, thermal_disable_didx;
    bool print_tentative;
    Long_t fEnablePattern;

    int bid,tid;
    PParticle *spectator, *participant, *quasi_composite;
    PChannel *quasi_pchannel;
    // general quasifree particle production on the deuteron  (V. Hejny 13/10/00)

    TArrayI ipid;
    // particle id array

    static double globalWeight;
    // global weight for the event (see PDecayManager, BUGBUG: never used)

    double *event_impact_param , *event_plane;  //from batch system
    double *weight_version;  //from batch system
    double *events;   //from batch system

    PParticle ** ptcls, *parent, *orig_parent, *grandparent, *grandgrandparent;
    // array of channel particles

    int ptcls_extern;
    // flag whether ptcls are coming from external source (do not delete those!)

    double ecm, w, emin; // invariant mass, weight of decay, energy threshold
    double e_cm;  // used to save parameters between calls of ds_dt()


    void IsReaction();
    // if a reaction channel identify beam, target, and decay mode

    void IdChannel();
    // identify reaction channel of known angular distribution
    // these are: p+p->p+p (elastic), N+N->N+Delta, pi+N->N+w, pi+ + P -> pi+ + P + w

    void ThermalSampling();
    // parent is midrapidity thermal source

    Int_t ReadFileInput();
    // parent is file input interface

    void MakeDilepton();
    // parent is a sampled dilepton


    int Genbod(int);
    // N-body phase-space decay of a particle in its rest frame.
    // This follows the CERNLIB routine GENBOD, in C++

    //this is for envelope distributions:
    int num_not_finalized;
    PDistribution *distribution_not_finalized[MAX_NUM_NOT_FINALIZED];

    int bulkdecay_pos, pro_bulkdecay_pos;
    
    PBulkInterface *bulk[MAX_BULKDECAY];
    PBulkInterface *pro_bulk[MAX_BULKDECAY];
    PProjector *current_projector;

    PProjector *GetCurrentProjector(void) {
	if (!bulkdecay_pos) {
	    current_projector = new PProjector();
	    AddBulk(current_projector);
	} else {
	    if ((strcmp("PProjector",bulk[bulkdecay_pos-1] -> GetName())==0)) {
		if ((bulk[bulkdecay_pos-1] -> GetPriority() > FILTER_PRIORITY) ||
		    ((PProjector *)bulk[bulkdecay_pos-1])->IsFileOpen()) {
		    current_projector = (PProjector *)bulk[bulkdecay_pos-1];
		} else {
		    current_projector = new PProjector();
		    AddBulk(current_projector);
		}
	    } else {		
		current_projector = new PProjector();
		AddBulk(current_projector);
	    }
	}
	return current_projector;
    }
    
    ClassDef(PChannel,0) //Pluto Channel Class

};

#endif // _PCHANNEL_H_
 PChannel.h:1
 PChannel.h:2
 PChannel.h:3
 PChannel.h:4
 PChannel.h:5
 PChannel.h:6
 PChannel.h:7
 PChannel.h:8
 PChannel.h:9
 PChannel.h:10
 PChannel.h:11
 PChannel.h:12
 PChannel.h:13
 PChannel.h:14
 PChannel.h:15
 PChannel.h:16
 PChannel.h:17
 PChannel.h:18
 PChannel.h:19
 PChannel.h:20
 PChannel.h:21
 PChannel.h:22
 PChannel.h:23
 PChannel.h:24
 PChannel.h:25
 PChannel.h:26
 PChannel.h:27
 PChannel.h:28
 PChannel.h:29
 PChannel.h:30
 PChannel.h:31
 PChannel.h:32
 PChannel.h:33
 PChannel.h:34
 PChannel.h:35
 PChannel.h:36
 PChannel.h:37
 PChannel.h:38
 PChannel.h:39
 PChannel.h:40
 PChannel.h:41
 PChannel.h:42
 PChannel.h:43
 PChannel.h:44
 PChannel.h:45
 PChannel.h:46
 PChannel.h:47
 PChannel.h:48
 PChannel.h:49
 PChannel.h:50
 PChannel.h:51
 PChannel.h:52
 PChannel.h:53
 PChannel.h:54
 PChannel.h:55
 PChannel.h:56
 PChannel.h:57
 PChannel.h:58
 PChannel.h:59
 PChannel.h:60
 PChannel.h:61
 PChannel.h:62
 PChannel.h:63
 PChannel.h:64
 PChannel.h:65
 PChannel.h:66
 PChannel.h:67
 PChannel.h:68
 PChannel.h:69
 PChannel.h:70
 PChannel.h:71
 PChannel.h:72
 PChannel.h:73
 PChannel.h:74
 PChannel.h:75
 PChannel.h:76
 PChannel.h:77
 PChannel.h:78
 PChannel.h:79
 PChannel.h:80
 PChannel.h:81
 PChannel.h:82
 PChannel.h:83
 PChannel.h:84
 PChannel.h:85
 PChannel.h:86
 PChannel.h:87
 PChannel.h:88
 PChannel.h:89
 PChannel.h:90
 PChannel.h:91
 PChannel.h:92
 PChannel.h:93
 PChannel.h:94
 PChannel.h:95
 PChannel.h:96
 PChannel.h:97
 PChannel.h:98
 PChannel.h:99
 PChannel.h:100
 PChannel.h:101
 PChannel.h:102
 PChannel.h:103
 PChannel.h:104
 PChannel.h:105
 PChannel.h:106
 PChannel.h:107
 PChannel.h:108
 PChannel.h:109
 PChannel.h:110
 PChannel.h:111
 PChannel.h:112
 PChannel.h:113
 PChannel.h:114
 PChannel.h:115
 PChannel.h:116
 PChannel.h:117
 PChannel.h:118
 PChannel.h:119
 PChannel.h:120
 PChannel.h:121
 PChannel.h:122
 PChannel.h:123
 PChannel.h:124
 PChannel.h:125
 PChannel.h:126
 PChannel.h:127
 PChannel.h:128
 PChannel.h:129
 PChannel.h:130
 PChannel.h:131
 PChannel.h:132
 PChannel.h:133
 PChannel.h:134
 PChannel.h:135
 PChannel.h:136
 PChannel.h:137
 PChannel.h:138
 PChannel.h:139
 PChannel.h:140
 PChannel.h:141
 PChannel.h:142
 PChannel.h:143
 PChannel.h:144
 PChannel.h:145
 PChannel.h:146
 PChannel.h:147
 PChannel.h:148
 PChannel.h:149
 PChannel.h:150
 PChannel.h:151
 PChannel.h:152
 PChannel.h:153
 PChannel.h:154
 PChannel.h:155
 PChannel.h:156
 PChannel.h:157
 PChannel.h:158
 PChannel.h:159
 PChannel.h:160
 PChannel.h:161
 PChannel.h:162
 PChannel.h:163
 PChannel.h:164
 PChannel.h:165
 PChannel.h:166
 PChannel.h:167
 PChannel.h:168
 PChannel.h:169
 PChannel.h:170
 PChannel.h:171
 PChannel.h:172
 PChannel.h:173
 PChannel.h:174
 PChannel.h:175
 PChannel.h:176
 PChannel.h:177
 PChannel.h:178
 PChannel.h:179
 PChannel.h:180
 PChannel.h:181
 PChannel.h:182
 PChannel.h:183
 PChannel.h:184
 PChannel.h:185
 PChannel.h:186
 PChannel.h:187
 PChannel.h:188
 PChannel.h:189
 PChannel.h:190
 PChannel.h:191
 PChannel.h:192
 PChannel.h:193
 PChannel.h:194
 PChannel.h:195
 PChannel.h:196
 PChannel.h:197
 PChannel.h:198
 PChannel.h:199
 PChannel.h:200
 PChannel.h:201
 PChannel.h:202
 PChannel.h:203
 PChannel.h:204
 PChannel.h:205
 PChannel.h:206
 PChannel.h:207
 PChannel.h:208
 PChannel.h:209
 PChannel.h:210
 PChannel.h:211
 PChannel.h:212
 PChannel.h:213
 PChannel.h:214
 PChannel.h:215
 PChannel.h:216
 PChannel.h:217
 PChannel.h:218
 PChannel.h:219
 PChannel.h:220
 PChannel.h:221
 PChannel.h:222
 PChannel.h:223
 PChannel.h:224
 PChannel.h:225
 PChannel.h:226
 PChannel.h:227
 PChannel.h:228
 PChannel.h:229
 PChannel.h:230
 PChannel.h:231
 PChannel.h:232
 PChannel.h:233
 PChannel.h:234
 PChannel.h:235
 PChannel.h:236
 PChannel.h:237
 PChannel.h:238
 PChannel.h:239
 PChannel.h:240
 PChannel.h:241
 PChannel.h:242
 PChannel.h:243
 PChannel.h:244
 PChannel.h:245
 PChannel.h:246
 PChannel.h:247
 PChannel.h:248
 PChannel.h:249
 PChannel.h:250
 PChannel.h:251
 PChannel.h:252
 PChannel.h:253
 PChannel.h:254
 PChannel.h:255
 PChannel.h:256
 PChannel.h:257
 PChannel.h:258
 PChannel.h:259
 PChannel.h:260
 PChannel.h:261
 PChannel.h:262
 PChannel.h:263
 PChannel.h:264
 PChannel.h:265
 PChannel.h:266
 PChannel.h:267
 PChannel.h:268
 PChannel.h:269
 PChannel.h:270
 PChannel.h:271
 PChannel.h:272
 PChannel.h:273
 PChannel.h:274
 PChannel.h:275
 PChannel.h:276
 PChannel.h:277
 PChannel.h:278
 PChannel.h:279
 PChannel.h:280
 PChannel.h:281
 PChannel.h:282
 PChannel.h:283
 PChannel.h:284
 PChannel.h:285
 PChannel.h:286
 PChannel.h:287
 PChannel.h:288
 PChannel.h:289
 PChannel.h:290
 PChannel.h:291
 PChannel.h:292
 PChannel.h:293
 PChannel.h:294