added stuff
[u/mrichter/AliRoot.git] / EVGEN / AliGenCocktail.cxx
CommitLineData
4c039060 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
88cb7938 16/* $Id$ */
4c039060 17
675e9664 18// Container class for AliGenerator through recursion.
19// Container is itself an AliGenerator.
20// What is stored are not the pointers to the generators directly but to objects of type
21// AliGenCocktail entry.
22// The class provides also iterator functionality.
23// Author: andreas.morsch@cern.ch
24//
25
116cbefd 26#include <TList.h>
27#include <TObjArray.h>
28
fe4da5cc 29#include "AliGenCocktail.h"
374924b5 30#include "AliGenCocktailEntry.h"
09e2e187 31#include "AliCollisionGeometry.h"
fe4da5cc 32#include "AliRun.h"
5d12ce38 33#include "AliMC.h"
4f85aa78 34#include "AliGenCocktailEventHeader.h"
fe4da5cc 35
36ClassImp(AliGenCocktail)
37
38AliGenCocktail::AliGenCocktail()
1c56e311 39 :AliGenerator(),
40 fNGenerators(0),
41 fRandom(kFALSE),
42 fUsePerEventRate(kFALSE),
43 fProb(0),
44 fEntries(0),
45 flnk1(0),
46 flnk2(0),
47 fHeader(0)
fe4da5cc 48{
374924b5 49// Constructor
8b31bfac 50 fName = "Cocktail";
51 fTitle= "Particle Generator using cocktail of generators";
fe4da5cc 52}
53
198bb1c7 54AliGenCocktail::AliGenCocktail(const AliGenCocktail & cocktail):
1c56e311 55 AliGenerator(cocktail),
56 fNGenerators(0),
57 fRandom(kFALSE),
58 fUsePerEventRate(kFALSE),
59 fProb(0),
60 fEntries(0),
61 flnk1(0),
62 flnk2(0),
63 fHeader(0)
374924b5 64{
198bb1c7 65// Copy constructor
66 cocktail.Copy(*this);
374924b5 67}
fe4da5cc 68
69AliGenCocktail::~AliGenCocktail()
70{
374924b5 71// Destructor
fe4da5cc 72 delete fEntries;
373c1a55 73 fEntries = 0;
74 // delete fHeader; // It is removed in AliRunLoader
75 fHeader = 0;
fe4da5cc 76}
77
78void AliGenCocktail::
a764b7de 79AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
fe4da5cc 80{
81//
b1403e15 82// Add a generator to the list
83// First check that list exists
84 if (!fEntries) fEntries = new TList();
85
86//
fe4da5cc 87// Forward parameters to the new generator
212bcf04 88 if(TestBit(kPtRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange)))
89 Generator->SetPtRange(fPtMin,fPtMax);
90 if(TestBit(kMomentumRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange)))
91 Generator->SetMomentumRange(fPMin,fPMax);
92
93 if (!(Generator->TestBit(kYRange)))
94 Generator->SetYRange(fYMin,fYMax);
95 if (!(Generator->TestBit(kPhiRange)))
96 Generator->SetPhiRange(fPhiMin*180/TMath::Pi(),fPhiMax*180/TMath::Pi());
97 if (!(Generator->TestBit(kThetaRange)))
98 Generator->SetThetaRange(fThetaMin*180/TMath::Pi(),fThetaMax*180/TMath::Pi());
99 if (!(Generator->TestBit(kVertexRange)))
100 Generator->SetOrigin(fOrigin[0], fOrigin[1], fOrigin[2]);
c8f7f6f9 101
102 Generator->SetSigma(fOsigma[0], fOsigma[1], fOsigma[2]);
7a50a130 103 Generator->SetVertexSmear(fVertexSmear);
c8f7f6f9 104 Generator->SetVertexSource(kContainer);
105 Generator->SetTrackingFlag(fTrackIt);
4f85aa78 106 Generator->SetContainer(this);
107
c97002cc 108
fe4da5cc 109//
110// Add generator to list
c5c3e4b0 111 char theName[256];
112 sprintf(theName, "%s_%d",Name, fNGenerators);
113 Generator->SetName(theName);
114
374924b5 115 AliGenCocktailEntry *entry =
fe4da5cc 116 new AliGenCocktailEntry(Generator, Name, RateExp);
c5c3e4b0 117
374924b5 118 fEntries->Add(entry);
fe4da5cc 119 fNGenerators++;
373c1a55 120 flnk1 = 0;
121 flnk2 = 0;
122 fRandom = kFALSE;
123 fHeader = 0;
124}
fe4da5cc 125
126 void AliGenCocktail::Init()
374924b5 127{
128// Initialisation
129 TIter next(fEntries);
130 AliGenCocktailEntry *entry;
131 //
132 // Loop over generators and initialize
133 while((entry = (AliGenCocktailEntry*)next())) {
c97002cc 134 if (fStack) entry->Generator()->SetStack(fStack);
374924b5 135 entry->Generator()->Init();
136 }
1ebe6dc8 137
138 next.Reset();
139
140 if (fRandom) {
141 fProb.Set(fNGenerators);
142 next.Reset();
143 Float_t sum = 0.;
144 while((entry = (AliGenCocktailEntry*)next())) {
145 sum += entry->Rate();
146 }
147
148 next.Reset();
149 Int_t i = 0;
150 Float_t psum = 0.;
151 while((entry = (AliGenCocktailEntry*)next())) {
152 psum += entry->Rate() / sum;
153 fProb[i++] = psum;
154 }
155 }
156 next.Reset();
374924b5 157}
fe4da5cc 158
09e2e187 159 void AliGenCocktail::FinishRun()
160{
161// Initialisation
162 TIter next(fEntries);
163 AliGenCocktailEntry *entry;
164 //
165 // Loop over generators and initialize
166 while((entry = (AliGenCocktailEntry*)next())) {
167 entry->Generator()->FinishRun();
168 }
169}
170
fe4da5cc 171 void AliGenCocktail::Generate()
374924b5 172{
173//
174// Generate event
175 TIter next(fEntries);
a8408367 176 AliGenCocktailEntry *entry = 0;
177 AliGenCocktailEntry *preventry = 0;
178 AliGenerator* gen = 0;
4f85aa78 179 if (fHeader) delete fHeader;
c89fc3f1 180
181
4f85aa78 182 fHeader = new AliGenCocktailEventHeader("Cocktail Header");
09e2e187 183
5d12ce38 184 TObjArray *partArray = gAlice->GetMCApp()->Particles();
c8f7f6f9 185
186//
187// Generate the vertex position used by all generators
188//
189 if(fVertexSmear == kPerEvent) Vertex();
190
67db3ff2 191 TArrayF eventVertex;
192 eventVertex.Set(3);
193 for (Int_t j=0; j < 3; j++) eventVertex[j] = fVertex[j];
194
1ebe6dc8 195 if (!fRandom) {
196 //
197 // Loop over generators and generate events
198 Int_t igen=0;
1ebe6dc8 199 while((entry = (AliGenCocktailEntry*)next())) {
c89fc3f1 200 if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
201
1ebe6dc8 202 igen++;
203 if (igen ==1) {
204 entry->SetFirst(0);
205 } else {
206 entry->SetFirst((partArray->GetEntriesFast())+1);
207 }
a8408367 208//
209// Handle case in which current generator needs collision geometry from previous generator
210//
1ebe6dc8 211 gen = entry->Generator();
212 if (gen->NeedsCollisionGeometry())
a8408367 213 {
1ebe6dc8 214 if (preventry && preventry->Generator()->ProvidesCollisionGeometry())
215 {
216 gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
217 } else {
218 Fatal("Generate()", "No Collision Geometry Provided");
219 }
a8408367 220 }
1ebe6dc8 221 entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
222 entry->Generator()->Generate();
223 entry->SetLast(partArray->GetEntriesFast());
224 preventry = entry;
225 }
c89fc3f1 226 } else if (fRandom) {
1ebe6dc8 227 //
228 // Select a generator randomly
229 //
230 Int_t i;
231 Float_t p0 = gRandom->Rndm();
232
233 for (i = 0; i < fNGenerators; i++) {
234 if (p0 < fProb[i]) break;
a8408367 235 }
1ebe6dc8 236
237 entry = (AliGenCocktailEntry*) fEntries->At(i);
238 entry->SetFirst(0);
239 gen = entry->Generator();
c8f7f6f9 240 entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
374924b5 241 entry->Generator()->Generate();
242 entry->SetLast(partArray->GetEntriesFast());
c89fc3f1 243 }
1ebe6dc8 244
374924b5 245 next.Reset();
4f85aa78 246
67db3ff2 247// Event Vertex
4f85aa78 248 fHeader->SetPrimaryVertex(eventVertex);
249 gAlice->SetGenEventHeader(fHeader);
374924b5 250}
fe4da5cc 251
c44d1c08 252void AliGenCocktail::SetVertexSmear(VertexSmear_t smear)
253{
254// Set vertex smearing and propagate it to the generators
255
256 AliGenerator::SetVertexSmear(smear);
257 TIter next(fEntries);
258 while (AliGenCocktailEntry* entry = (AliGenCocktailEntry*)next()) {
259 entry->Generator()->SetVertexSmear(smear);
260 }
261}
262
fe4da5cc 263AliGenCocktailEntry * AliGenCocktail::FirstGenerator()
264{
374924b5 265// Iterator over generators: Initialisation
fe4da5cc 266 flnk1 = fEntries->FirstLink();
267 if (flnk1) {
268 return (AliGenCocktailEntry*) (flnk1->GetObject());
269 } else {
270 return 0;
271 }
272}
273
274AliGenCocktailEntry* AliGenCocktail::NextGenerator()
275{
374924b5 276// Iterator over generators: Increment
fe4da5cc 277 flnk1 = flnk1->Next();
278 if (flnk1) {
279 return (AliGenCocktailEntry*) (flnk1->GetObject());
280 } else {
281 return 0;
282 }
283}
284
285void AliGenCocktail::
286FirstGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
287{
374924b5 288// Iterator over generator pairs: Initialisation
fe4da5cc 289 flnk2 = flnk1 = fEntries->FirstLink();
290 if (flnk1) {
291 e2 = e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
292 } else {
293 e2= e1 = 0;
294 }
295}
296
297void AliGenCocktail::
298NextGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
299{
374924b5 300// Iterator over generators: Increment
fe4da5cc 301 flnk2 = flnk2->Next();
302 if (flnk2) {
303 e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
304 e2 = (AliGenCocktailEntry*) (flnk2->GetObject());
305 } else {
306 flnk2 = flnk1 = flnk1->Next();
307 if (flnk1) {
308 e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
309 e2 = (AliGenCocktailEntry*) (flnk2->GetObject());
310 } else {
311 e1=0;
312 e2=0;
313 }
314 }
315}
316
4f85aa78 317void AliGenCocktail::AddHeader(AliGenEventHeader* header)
318{
319// Add a header to the list
320 if (fHeader) fHeader->AddHeader(header);
321}
322
374924b5 323AliGenCocktail& AliGenCocktail::operator=(const AliGenCocktail& rhs)
324{
325// Assignment operator
198bb1c7 326 rhs.Copy(*this);
327 return (*this);
fe4da5cc 328}
329
dc1d768c 330void AliGenCocktail::Copy(TObject &) const
198bb1c7 331{
332 Fatal("Copy","Not implemented!\n");
333}
334
335
fe4da5cc 336