////////////////////////////////////////////////////////
//  Pluto embedded particles interface
//
//  With this class one may add additional
//  Particles in the PReaction chain
//
//
//  The embedded particles will be added
//  to the PReaction. If PVertexFile was used used before, the
//  eventloop will be stopped latest at the last entry of the ntuple
//  no matter if a larger number was given to the loop (see PVertexFile).
//  The event sequence number of the real data vertex ntuple (which identifies
//  which event has exacly has been used) is stored in ASCII output.
//  This variables will be later transported to HGeantKine and can be
//  retrieved via HGeantKine->getUserVal().
//  The synchronization in event embeding makes use out
//  of this data.
//                    Author:  Ingo Froehlich
//                    Written: 11/07/2007
//                    Revised: 11/28/2007 (Jochen Markert)
//
////////////////////////////////////////////////////////

#include "PEmbeddedParticles.h"
#include "PChannel.h"


PEmbeddedParticles::PEmbeddedParticles() {
    local_pos = 0;
    for (Int_t i=0; i<MAX_EMBEDDEDPARTICLES; i++) {
	local_pMin        [i] = 0;
	local_pMax        [i] = 0;
	local_openingAngle[i] = 0;
	local_theta       [i] = 0;
	local_phi         [i] = 0;
	local_mMax        [i] = 0;
	local_mMin        [i] = 0;

	local_thetaMin    [i] = 0;
	local_thetaMax    [i] = 0;
	local_phiMin      [i] = 0;
	local_phiMax      [i] = 0;

	local_version     [i] = 0;
	downscaling       [i] = 0;
	last_downscaling  [i] = 0;
	local             [i] = NULL;
    }

    vertex_x = makeStaticData()->GetBatchValue("_event_vertex_x");
    vertex_y = makeStaticData()->GetBatchValue("_event_vertex_y");
    vertex_z = makeStaticData()->GetBatchValue("_event_vertex_z");

}

Bool_t  PEmbeddedParticles::SetSampling(Double_t pMin, Double_t pMax,
					Double_t openingAngle, Double_t theta,
					Double_t phi,
					Double_t mMin, Double_t mMax) {
    // The current particle will be re-sampled during the event loop
    // Momentum sampling is done between "pMin" and "pMax" [rad]
    // A region with "openingAngle" [rad] pointing to "theta,phi" will be covered
    // with white mass between "mMin" and "mMax" [GeV/c]. if "mMax" < 0
    // the already set mass of the particle will be used during re-sampling.

    if (local_pos == 0) {
	Warning("SetSampling", "no particle defined");
	return kFALSE;
    }
    local_pMin        [local_pos - 1] = pMin;
    local_pMax        [local_pos - 1] = pMax;
    local_mMin        [local_pos - 1] = mMin;
    local_mMax        [local_pos - 1] = mMax;
    local_openingAngle[local_pos - 1] = -cos(openingAngle);

    local_theta       [local_pos - 1] = theta;
    local_phi         [local_pos - 1] = phi;

    local_version     [local_pos - 1] = 0;

    return kTRUE;
};


Bool_t  PEmbeddedParticles::SetSamplingSector(Double_t pMin       , Double_t pMax,
					      Double_t thetaMin   , Double_t thetaMax,
					      Double_t phiMin     , Double_t phiMax,
					      Double_t phiStartVal, Int_t numParticle,
					      Double_t deltaPhi   , Int_t numSectors)
{
    // The current particle will be re-sampled "numParticle" times per sector
    // during the event loop. Momentum sampling is done between "pMin"  "pMax",
    // theta (polar) between "thetaMin" - "thetaMax" and phi between "phiMin" - "phiMax" (0-60 deg).
    // The phi coordinate can be rotated by phiStart. Angles are in degrees, mom in GeV/c.
    // If the mass of the Particle has been set already before adding it to the bulk this
    // mass will be used during re-sampling. Otherwise the particle masses from PStaticData
    // are used.
    if(local_pos == 0) {
	Warning("SetSamplingSector","no particle defined");
	return kFALSE;
    }
    startPhi  = phiStartVal;
    nParticle = numParticle;
    Int_t addedParticle = 0;


    if(nParticle > MAX_EMBEDDEDPARTICLES/numSectors){

	nParticle = MAX_EMBEDDEDPARTICLES/numSectors;
	Warning("SetSamplingSector()",
		"Number of particles /sector > MAX_EMBEDDEDPARTICLES/%i, will be trucated to numParticle = %i .",
		numSectors,MAX_EMBEDDEDPARTICLES/numSectors);
    }

    for(Int_t s = 0; s < numSectors; s ++)
    {
	for(Int_t n = 0; n < nParticle; n ++){
	    if(addedParticle > 0){
		// first particle has been already added
		// clone the other one by one
		AddParticle((PParticle*)local[local_pos - 1]->Clone(),
			    downscaling[local_pos - 1]);
		//...local_pos incremented in the line above!
	    }
	    Double_t phi_shift = (s * deltaPhi + startPhi) * TMath::DegToRad();

	    local_pMin    [local_pos - 1] = pMin;
	    local_pMax    [local_pos - 1] = pMax;
	    local_thetaMin[local_pos - 1] = thetaMin * TMath::DegToRad();
	    local_thetaMax[local_pos - 1] = thetaMax * TMath::DegToRad();
	    local_phiMin  [local_pos - 1] = phi_shift + phiMin * TMath::DegToRad();
	    local_phiMax  [local_pos - 1] = phi_shift + phiMax * TMath::DegToRad();
	    local_version [local_pos - 1] = 1;
	    if(local[local_pos - 1]->M() > 0){
		// the mass of the particle has been set
                // outside already
		local_mMax    [local_pos - 1] = -1;
	    }
	    addedParticle ++;
	}
    }

    return kTRUE;
}



Bool_t PEmbeddedParticles::AddParticle(PParticle * particle, int downsc) {
    // Adds the "particle" to the stack to be embedded during the event loop
    // if "downsc" is larger then 1, only each downsc^th event will be filled

    if (local_pos == MAX_EMBEDDEDPARTICLES ) {
	cout << "MAX_EMBEDDEDPARTICLES reached" << endl;
	return kFALSE;
    }
    downscaling     [local_pos]    = downsc;
    last_downscaling[local_pos]    = 1;
    local           [local_pos ++] = particle;
    return kTRUE;
}

Bool_t PEmbeddedParticles::Modify(PParticle **mstack, int *, int *num, int stacksize) {
    // Read the particles from the defined stack and copy this to the official
    // particle stream

    int cur = *num;
    Double_t  p,cos_theta,sin_theta,theta,phi,m;

    for (int i=0; i<local_pos; i++) {
	if (last_downscaling[i] == downscaling[i])
	{
	    last_downscaling[i] = 1;

	    *(mstack[cur]) = *(local[i]);

	    //set embedded particle to global vertex
	    mstack[cur]->SetVertex(*vertex_x,
				   *vertex_y,
				   *vertex_z,0.);

	    mstack[cur]->SetSourceId(EMBEDDED_SOURCE_ID);
	    mstack[cur]->SetProperTime();

	    if (local_pMax[i] > 0) {
		//do the resample

		if (local_version[i] == 0) {
		    //Cone sampling
		    p         = local_pMin[i] + (PUtils::sampleFlat() * (local_pMax[i] - local_pMin[i]));
		    theta     = acos(1. - ((local_openingAngle[i] + 1) * PUtils::sampleFlat()));
		    phi       = 2. * TMath::Pi() * PUtils::sampleFlat();
		    sin_theta = sin(theta);

		    mstack[cur]->SetPx(p * cos(phi) * sin_theta);
		    mstack[cur]->SetPy(p * sin(phi) * sin_theta);
		    mstack[cur]->SetPz(p * cos(theta));

		    mstack[cur]->RotateY(local_theta[i]);
		    mstack[cur]->RotateZ(local_phi  [i]);

		    if (local_mMax[i] < 0.) {
			mstack[cur]->SetM(local[i]->M());
		    } else {
			m = local_mMin[i] + (PUtils::sampleFlat() * (local_mMax[i] - local_mMin[i])) ;
			mstack[cur]->SetM(m);
		    }
		}
		else if (local_version[i] == 1) {
		    //Sector sampling
		    cos_theta = cos(local_thetaMin[i]) + (PUtils::sampleFlat() * (cos(local_thetaMax[i]) - cos(local_thetaMin[i])));

		    p         = local_pMin  [i] + (PUtils::sampleFlat() * (local_pMax  [i] - local_pMin  [i]));
		    phi       = local_phiMin[i] + (PUtils::sampleFlat() * (local_phiMax[i] - local_phiMin[i]));
		    theta     = acos(cos_theta);
		    sin_theta = sin(theta);

		    mstack[cur]->SetPx(p * cos(phi) * sin_theta);
		    mstack[cur]->SetPy(p * sin(phi) * sin_theta);
		    mstack[cur]->SetPz(p * cos_theta);

		    if (local_mMax[i] < 0.) {
			mstack[cur]->SetM(local[i]->M());
		    } else {
			mstack[cur]->SetM(makeStaticData()->GetParticleMass(mstack[cur]->ID()));
		    }
		    //Vertex set by PParticle ctor

		}
	    }
	    cur ++;

	    if (cur == stacksize) {
		return kFALSE;
	    }

	} else last_downscaling[i] ++; //wait for next time...
    }

    *num = cur;

    return kTRUE;
}

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