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