effc++ corrections (alberto)
[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()
39 :AliGenerator()
40{
374924b5 41// Constructor
8b31bfac 42 fName = "Cocktail";
43 fTitle= "Particle Generator using cocktail of generators";
c89fc3f1 44 flnk1 = 0;
45 flnk2 = 0;
46 fNGenerators = 0;
47 fEntries = 0;
48 fHeader = 0;
49 fRandom = kFALSE;
50 fUsePerEventRate = kFALSE;
fe4da5cc 51}
52
198bb1c7 53AliGenCocktail::AliGenCocktail(const AliGenCocktail & cocktail):
54 AliGenerator(cocktail)
374924b5 55{
198bb1c7 56// Copy constructor
57 cocktail.Copy(*this);
374924b5 58}
fe4da5cc 59
60AliGenCocktail::~AliGenCocktail()
61{
374924b5 62// Destructor
fe4da5cc 63 delete fEntries;
373c1a55 64 fEntries = 0;
65 // delete fHeader; // It is removed in AliRunLoader
66 fHeader = 0;
fe4da5cc 67}
68
69void AliGenCocktail::
a764b7de 70AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
fe4da5cc 71{
b1403e15 72//
73// Add a generator to the list
74// First check that list exists
75 if (!fEntries) fEntries = new TList();
76
fe4da5cc 77//
78// Forward parameters to the new generator
212bcf04 79 if(TestBit(kPtRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange)))
80 Generator->SetPtRange(fPtMin,fPtMax);
81 if(TestBit(kMomentumRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange)))
82 Generator->SetMomentumRange(fPMin,fPMax);
83
84 if (!(Generator->TestBit(kYRange)))
85 Generator->SetYRange(fYMin,fYMax);
86 if (!(Generator->TestBit(kPhiRange)))
87 Generator->SetPhiRange(fPhiMin*180/TMath::Pi(),fPhiMax*180/TMath::Pi());
88 if (!(Generator->TestBit(kThetaRange)))
89 Generator->SetThetaRange(fThetaMin*180/TMath::Pi(),fThetaMax*180/TMath::Pi());
90 if (!(Generator->TestBit(kVertexRange)))
91 Generator->SetOrigin(fOrigin[0], fOrigin[1], fOrigin[2]);
c8f7f6f9 92
93 Generator->SetSigma(fOsigma[0], fOsigma[1], fOsigma[2]);
7a50a130 94 Generator->SetVertexSmear(fVertexSmear);
c8f7f6f9 95 Generator->SetVertexSource(kContainer);
96 Generator->SetTrackingFlag(fTrackIt);
4f85aa78 97 Generator->SetContainer(this);
98
c97002cc 99
fe4da5cc 100//
101// Add generator to list
c5c3e4b0 102 char theName[256];
103 sprintf(theName, "%s_%d",Name, fNGenerators);
104 Generator->SetName(theName);
105
374924b5 106 AliGenCocktailEntry *entry =
fe4da5cc 107 new AliGenCocktailEntry(Generator, Name, RateExp);
c5c3e4b0 108
374924b5 109 fEntries->Add(entry);
fe4da5cc 110 fNGenerators++;
373c1a55 111 flnk1 = 0;
112 flnk2 = 0;
113 fRandom = kFALSE;
114 fHeader = 0;
115}
fe4da5cc 116
117 void AliGenCocktail::Init()
374924b5 118{
119// Initialisation
120 TIter next(fEntries);
121 AliGenCocktailEntry *entry;
122 //
123 // Loop over generators and initialize
124 while((entry = (AliGenCocktailEntry*)next())) {
c97002cc 125 if (fStack) entry->Generator()->SetStack(fStack);
374924b5 126 entry->Generator()->Init();
127 }
1ebe6dc8 128
129 next.Reset();
130
131 if (fRandom) {
132 fProb.Set(fNGenerators);
133 next.Reset();
134 Float_t sum = 0.;
135 while((entry = (AliGenCocktailEntry*)next())) {
136 sum += entry->Rate();
137 }
138
139 next.Reset();
140 Int_t i = 0;
141 Float_t psum = 0.;
142 while((entry = (AliGenCocktailEntry*)next())) {
143 psum += entry->Rate() / sum;
144 fProb[i++] = psum;
145 }
146 }
147 next.Reset();
374924b5 148}
fe4da5cc 149
09e2e187 150 void AliGenCocktail::FinishRun()
151{
152// Initialisation
153 TIter next(fEntries);
154 AliGenCocktailEntry *entry;
155 //
156 // Loop over generators and initialize
157 while((entry = (AliGenCocktailEntry*)next())) {
158 entry->Generator()->FinishRun();
159 }
160}
161
fe4da5cc 162 void AliGenCocktail::Generate()
374924b5 163{
164//
165// Generate event
166 TIter next(fEntries);
a8408367 167 AliGenCocktailEntry *entry = 0;
168 AliGenCocktailEntry *preventry = 0;
169 AliGenerator* gen = 0;
4f85aa78 170 if (fHeader) delete fHeader;
c89fc3f1 171
172
4f85aa78 173 fHeader = new AliGenCocktailEventHeader("Cocktail Header");
09e2e187 174
5d12ce38 175 TObjArray *partArray = gAlice->GetMCApp()->Particles();
c8f7f6f9 176
177//
178// Generate the vertex position used by all generators
179//
180 if(fVertexSmear == kPerEvent) Vertex();
181
67db3ff2 182 TArrayF eventVertex;
183 eventVertex.Set(3);
184 for (Int_t j=0; j < 3; j++) eventVertex[j] = fVertex[j];
185
1ebe6dc8 186 if (!fRandom) {
187 //
188 // Loop over generators and generate events
189 Int_t igen=0;
1ebe6dc8 190 while((entry = (AliGenCocktailEntry*)next())) {
c89fc3f1 191
192 if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
193
1ebe6dc8 194 igen++;
195 if (igen ==1) {
196 entry->SetFirst(0);
197 } else {
198 entry->SetFirst((partArray->GetEntriesFast())+1);
199 }
a8408367 200//
201// Handle case in which current generator needs collision geometry from previous generator
202//
1ebe6dc8 203 gen = entry->Generator();
204 if (gen->NeedsCollisionGeometry())
a8408367 205 {
1ebe6dc8 206 if (preventry && preventry->Generator()->ProvidesCollisionGeometry())
207 {
208 gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
209 } else {
210 Fatal("Generate()", "No Collision Geometry Provided");
211 }
a8408367 212 }
1ebe6dc8 213 entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
214 entry->Generator()->Generate();
215 entry->SetLast(partArray->GetEntriesFast());
216 preventry = entry;
217 }
c89fc3f1 218 } else if (fRandom) {
1ebe6dc8 219 //
220 // Select a generator randomly
221 //
222 Int_t i;
223 Float_t p0 = gRandom->Rndm();
224
225 for (i = 0; i < fNGenerators; i++) {
226 if (p0 < fProb[i]) break;
a8408367 227 }
1ebe6dc8 228
229 entry = (AliGenCocktailEntry*) fEntries->At(i);
230 entry->SetFirst(0);
231 gen = entry->Generator();
c8f7f6f9 232 entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
374924b5 233 entry->Generator()->Generate();
234 entry->SetLast(partArray->GetEntriesFast());
c89fc3f1 235 }
1ebe6dc8 236
374924b5 237 next.Reset();
4f85aa78 238
67db3ff2 239// Event Vertex
4f85aa78 240 fHeader->SetPrimaryVertex(eventVertex);
241 gAlice->SetGenEventHeader(fHeader);
374924b5 242}
fe4da5cc 243
c44d1c08 244void AliGenCocktail::SetVertexSmear(VertexSmear_t smear)
245{
246// Set vertex smearing and propagate it to the generators
247
248 AliGenerator::SetVertexSmear(smear);
249 TIter next(fEntries);
250 while (AliGenCocktailEntry* entry = (AliGenCocktailEntry*)next()) {
251 entry->Generator()->SetVertexSmear(smear);
252 }
253}
254
fe4da5cc 255AliGenCocktailEntry * AliGenCocktail::FirstGenerator()
256{
374924b5 257// Iterator over generators: Initialisation
fe4da5cc 258 flnk1 = fEntries->FirstLink();
259 if (flnk1) {
260 return (AliGenCocktailEntry*) (flnk1->GetObject());
261 } else {
262 return 0;
263 }
264}
265
266AliGenCocktailEntry* AliGenCocktail::NextGenerator()
267{
374924b5 268// Iterator over generators: Increment
fe4da5cc 269 flnk1 = flnk1->Next();
270 if (flnk1) {
271 return (AliGenCocktailEntry*) (flnk1->GetObject());
272 } else {
273 return 0;
274 }
275}
276
277void AliGenCocktail::
278FirstGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
279{
374924b5 280// Iterator over generator pairs: Initialisation
fe4da5cc 281 flnk2 = flnk1 = fEntries->FirstLink();
282 if (flnk1) {
283 e2 = e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
284 } else {
285 e2= e1 = 0;
286 }
287}
288
289void AliGenCocktail::
290NextGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
291{
374924b5 292// Iterator over generators: Increment
fe4da5cc 293 flnk2 = flnk2->Next();
294 if (flnk2) {
295 e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
296 e2 = (AliGenCocktailEntry*) (flnk2->GetObject());
297 } else {
298 flnk2 = flnk1 = flnk1->Next();
299 if (flnk1) {
300 e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
301 e2 = (AliGenCocktailEntry*) (flnk2->GetObject());
302 } else {
303 e1=0;
304 e2=0;
305 }
306 }
307}
308
4f85aa78 309void AliGenCocktail::AddHeader(AliGenEventHeader* header)
310{
311// Add a header to the list
312 if (fHeader) fHeader->AddHeader(header);
313}
314
374924b5 315AliGenCocktail& AliGenCocktail::operator=(const AliGenCocktail& rhs)
316{
317// Assignment operator
198bb1c7 318 rhs.Copy(*this);
319 return (*this);
fe4da5cc 320}
321
dc1d768c 322void AliGenCocktail::Copy(TObject &) const
198bb1c7 323{
324 Fatal("Copy","Not implemented!\n");
325}
326
327
fe4da5cc 328