]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVGEN/AliGenCocktail.cxx
Removing obsolete constants.
[u/mrichter/AliRoot.git] / EVGEN / AliGenCocktail.cxx
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
16 /* $Id$ */
17
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
26 #include <TList.h>
27 #include <TObjArray.h>
28
29 #include "AliGenCocktail.h"
30 #include "AliGenCocktailEntry.h"
31 #include "AliCollisionGeometry.h"
32 #include "AliRun.h"
33 #include "AliMC.h"
34 #include "AliGenCocktailEventHeader.h"
35
36 ClassImp(AliGenCocktail)
37
38 AliGenCocktail::AliGenCocktail()
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)
48 {
49 // Constructor
50     fName = "Cocktail";
51     fTitle= "Particle Generator using cocktail of generators";
52 }
53
54 AliGenCocktail::AliGenCocktail(const AliGenCocktail & cocktail):
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)
64 {
65 // Copy constructor
66     cocktail.Copy(*this);
67 }
68
69 AliGenCocktail::~AliGenCocktail()
70 {
71 // Destructor
72     delete fEntries;
73     fEntries = 0;
74     //    delete fHeader; // It is removed in AliRunLoader
75     fHeader = 0;
76 }
77
78 void AliGenCocktail::
79 AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
80 {
81 //
82 // Add a generator to the list 
83 // First check that list exists
84     if (!fEntries) fEntries = new TList();
85
86 //
87 //  Forward parameters to the new generator
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]);
101
102     Generator->SetSigma(fOsigma[0], fOsigma[1], fOsigma[2]);
103     Generator->SetVertexSmear(fVertexSmear);
104     Generator->SetVertexSource(kContainer);
105     Generator->SetTrackingFlag(fTrackIt);
106     Generator->SetContainer(this);
107     
108         
109 //
110 //  Add generator to list   
111     char theName[256];
112     sprintf(theName, "%s_%d",Name, fNGenerators);
113     Generator->SetName(theName);
114
115     AliGenCocktailEntry *entry = 
116         new AliGenCocktailEntry(Generator, Name, RateExp);
117     
118      fEntries->Add(entry);
119      fNGenerators++;
120      flnk1 = 0;
121      flnk2 = 0;
122      fRandom  = kFALSE;
123      fHeader  = 0;
124 }
125
126   void AliGenCocktail::Init()
127 {
128 // Initialisation
129     TIter next(fEntries);
130     AliGenCocktailEntry *entry;
131     //
132     // Loop over generators and initialize
133     while((entry = (AliGenCocktailEntry*)next())) {
134         if (fStack)  entry->Generator()->SetStack(fStack);
135         entry->Generator()->Init();
136     }  
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();
157 }
158
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
171  void AliGenCocktail::Generate()
172 {
173 //
174 // Generate event 
175     TIter next(fEntries);
176     AliGenCocktailEntry *entry = 0;
177     AliGenCocktailEntry *preventry = 0;
178     AliGenerator* gen = 0;
179     if (fHeader) delete fHeader;
180
181     
182     fHeader = new AliGenCocktailEventHeader("Cocktail Header");
183
184     TObjArray *partArray = gAlice->GetMCApp()->Particles();
185
186 //
187 //  Generate the vertex position used by all generators
188 //    
189     if(fVertexSmear == kPerEvent) Vertex();
190
191     TArrayF eventVertex;
192     eventVertex.Set(3);
193     for (Int_t j=0; j < 3; j++) eventVertex[j] = fVertex[j];
194
195     if (!fRandom) {
196         //
197         // Loop over generators and generate events
198         Int_t igen=0;
199         while((entry = (AliGenCocktailEntry*)next())) {
200             if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
201             
202             igen++;
203             if (igen ==1) {
204                 entry->SetFirst(0);
205             } else {
206                 entry->SetFirst((partArray->GetEntriesFast())+1);
207             }
208 //
209 //      Handle case in which current generator needs collision geometry from previous generator
210 //
211             gen = entry->Generator();
212             if (gen->NeedsCollisionGeometry())
213             {
214                 if (preventry && preventry->Generator()->ProvidesCollisionGeometry())
215                 {
216                     gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
217                 } else {
218                     Fatal("Generate()", "No Collision Geometry Provided");
219                 }
220             }
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         }  
226     } else if (fRandom) {
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;
235         }
236
237         entry = (AliGenCocktailEntry*) fEntries->At(i);
238         entry->SetFirst(0);
239         gen = entry->Generator();
240         entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
241         entry->Generator()->Generate();
242         entry->SetLast(partArray->GetEntriesFast());
243     } 
244     
245     next.Reset();
246
247 // Event Vertex
248     fHeader->SetPrimaryVertex(eventVertex);
249     gAlice->SetGenEventHeader(fHeader); 
250 }
251
252 void 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
263 AliGenCocktailEntry *  AliGenCocktail::FirstGenerator()
264 {
265 // Iterator over generators: Initialisation
266     flnk1 = fEntries->FirstLink();
267     if (flnk1) {
268         return (AliGenCocktailEntry*) (flnk1->GetObject());
269     } else {
270         return 0;
271     }
272 }
273
274 AliGenCocktailEntry*  AliGenCocktail::NextGenerator()
275 {
276 // Iterator over generators: Increment
277     flnk1 = flnk1->Next();
278     if (flnk1) {
279         return (AliGenCocktailEntry*) (flnk1->GetObject());
280     } else {
281         return 0;
282     }
283 }
284
285 void AliGenCocktail::
286 FirstGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
287 {
288 // Iterator over generator pairs: Initialisation
289     flnk2 = flnk1 = fEntries->FirstLink();
290     if (flnk1) {
291         e2 = e1 = (AliGenCocktailEntry*) (flnk1->GetObject());
292     } else {
293         e2= e1 = 0;
294     }
295 }
296
297 void AliGenCocktail::
298 NextGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
299 {
300 // Iterator over generators: Increment
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
317 void AliGenCocktail::AddHeader(AliGenEventHeader* header)
318 {
319 // Add a header to the list 
320     if (fHeader) fHeader->AddHeader(header);
321 }
322                               
323 AliGenCocktail& AliGenCocktail::operator=(const  AliGenCocktail& rhs)
324 {
325 // Assignment operator
326     rhs.Copy(*this); 
327     return (*this);
328 }
329
330 void AliGenCocktail::Copy(TObject &) const
331 {
332     Fatal("Copy","Not implemented!\n");
333 }
334
335
336