////////////////////////////////////////////////////////
//  Pluto bulk decay base 
//
//  This class let all particles with tau<tauMax 
//  decay in the pluto way
//
//                    Author:  Ingo Froehlich
//                    Written: 10/07/2007
//                    Revised:
//
////////////////////////////////////////////////////////

#include "PPlutoBulkDecay.h"
#include "PDynamicData.h"
#include "PChannel.h"
#include "PDistributionManager.h"



PPlutoBulkDecay::PPlutoBulkDecay() {
    makeStdData();
    didx = makeDataBase()->GetParamInt("didx");
    stackchannel = makeDataBase()->GetParamTObj("stackchannel");

    fPriority = DECAY_PRIORITY;
}

bool PPlutoBulkDecay::Modify(PParticle ** stack, int *decay_done, int * num, int stacksize) {
    //decay all particle
    //input: particle array with *num members
    //new particles should be instantiated and *num increased

    const int maxnp = 7; //max 7 particle decay -> Matches to static-data (IF)
    Int_t st_i1, st_i3, k; //st_i2
    Int_t size_ini = *num;  //Input size


    Int_t child_ids[maxnp+1];
    PParticle *cur_p, *work[maxnp+2];

    //I follow the LIFO principle, starting with the last particle and going to 0
    //This is needed because I first HAVE to end a decay chain, before re-using the PChannel
    //BUGBUG: This might not work with a self-decay like Delta->Delta+gamma
    st_i1 = size_ini-1;
    
    //st_i2 = st_i3 = size_ini;
    st_i3 = size_ini;

    while (st_i1 > -1) {
	cur_p = stack[st_i1];  //set current pointer
	//if (cur_p->ID() == 17)
	//  cout << cur_p->IsActive()  << ":"<<  makeDynamicData()->GetParticleLife(cur_p->ID() )<< ":"<< 
	//	decay_done[st_i1] << endl;

	if (cur_p->IsActive() && 
	    makeDynamicData()->GetParticleLife(cur_p->ID()) 
	    < tauMax && !decay_done[st_i1]) {

 	    //cout << "Current stack particle: " << st_i1 << endl;
 	    //cur_p->Print();

	    //	    cout << "cur_p" << cur_p->ID()<< ":"<< cur_p->M()<< endl;

	    Int_t channel_idx = 
		makeDynamicData()->PickDecayChannel(cur_p->ID(), cur_p->M(), child_ids);	    

	    //cout << channel_idx << endl;

	    if (channel_idx>-1) { 

		Int_t np = child_ids[0];   // number of decay products
		if (np > 0) { // np==0 can happen if M()<threshold
		    //First we check if we have already the PChannel on stock
		    TObject * ch;
		    PChannel* d_ch;
		    
		    if (!makeDataBase()->GetParamTObj (didx, channel_idx , stackchannel, &ch)) {
			Info("Modify", "(%s) Making new channel for didx %i", PRINT_AUTO_ALLOC, channel_idx);

			if (np > maxnp) {
			    printf("PPlutoBulkDecay::Modify(): np=%d > maxnp=%d\n", np, maxnp);
			    np = maxnp;
			}

			for (k=1; k<=np; k++) {
			    work[k] = new PParticle(child_ids[k]);
			}
			work[0] = new PParticle(cur_p->ID());

			Int_t sourceIdSave = cur_p->GetSourceId();  // Need to save because
			Int_t parentIdSave = cur_p->GetParentId();  // PChannel constructor
			PParticle **local = new PParticle*[np+1];
			for(int i=0; i<=np; i++) local[i] = work[i]; //make local copy as work array is overwritten

			d_ch = new PChannel(local, np, 1, 1, 0); // overwrites values.

			makeDistributionManager()->Attach(d_ch);
			d_ch->SetPrintTentative(0);
			d_ch->Init();

			work[0]->SetSourceId(sourceIdSave);           // Reset them here.
			work[0]->SetParentId(parentIdSave);

			makeDataBase()->SetParamTObj(makeDataBase()->GetEntryInt("didx", channel_idx) , 
						     "stackchannel", d_ch);	
			
		    }

		    if (makeDataBase()->GetParamTObj(didx, channel_idx, stackchannel, &ch)) {
			d_ch = (PChannel*)ch;
			d_ch->Reset(); //make channel clean for next use

			(d_ch->GetParticles()[0]) = cur_p;

			work[0] = cur_p;

			//Get out the daughters and modify them
			for (k=1; k<=np; k++) {
			    work[k] = (d_ch->GetParticles()[k]);  //re-use daughter particles
			    work[k]->SetActive();
			    work[k]->SetIndex(st_i3+1+k);
			    work[k]->SetParent(work[0]);
			    decay_done[st_i3+k-1]=0;
			}
			for (k=1; k<=np; k++) {
			    //set the siblings again
			    if (k!=np) {
				work[k]->SetSibling(work[k+1]);
			    } else {
				work[k]->SetSibling(work[1]);
			    }
			}
			
			//--> otherwise we are not doing sampling with the total width
		    } else {
			Fatal("Modify", "Unable to get PChannel");
			return kFALSE;
		    }

#if 0
		    //before doing the decay, maybe dump the stack
		    cout << "dumping stack" << endl;
		    for (int t=0;t<st_i3+np;t++) {
			cout << t << ":" << stack[t] << ":"<< stack[t]->GetSibling() << endl;
			stack[t]->Print();
		    }
#endif
		    //end debug
		    

		    (d_ch->GetParticles()[0])->SetParent(work[0]->GetParent());
		    //now Init in background
		    d_ch->SetPrintTentative(0);
 		    d_ch->Init();

		    d_ch->PrintNew();
		    d_ch->SetPrintTentative(1);

		    Int_t error_count = 0;
		    if (error_count < 100) {
			if (d_ch->decay()) {
			    error_count++;
			}
		    } else {
			Warning("Modify", "Decay not possible, giving up");
		    }

		    work[0]->SetDecayModeIndex(channel_idx, 1); //set the index, but destroy it later
		    //-->has to happen after decay()

		    decay_done[st_i1] = 1;  
		    //never let this particle decay again in this event

		    //after the decay we put all local particles to stack
		    for(int k=1; k<=np; k++) {
			*stack[st_i3+k-1] = *work[k];
		    }

		    for(int k=1; k<=np; k++) {
			stack[st_i3+k-1]->SetParent(cur_p);
			stack[st_i3+k-1]->SetActive();

			//set the siblings again
			if (k!=np) {
			    stack[st_i3+k-1]->SetSibling(stack[st_i3+k]);
			}
			else {
			    stack[st_i3+k-1]->SetSibling(stack[st_i3]);
			}
		    }
		    st_i3 += np;
		    if (recursiveMode)
			st_i1 = st_i3;
		    else
			st_i1 = size_ini-1;

		} //if (np > 0)
	    } //idx>-1

	    if(st_i3>(stacksize-maxnp))   {  // check for stack overflow, taking into account next particles
		printf("PPlutoBulkDecay::decayAll: st_i3=%d > stacksize%i\n", st_i3, stacksize);
	    }
	
	} // if (cur_p->isActive()...

	//set the focus to the last particle, and let us drop to the first one
	st_i1--;
    }

    *num = st_i3;  //Output size
	    
    return kTRUE;
}

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