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