Misplaced initialisation of ntimes corrected.
[u/mrichter/AliRoot.git] / EVGEN / AliGenCocktail.cxx
index f480c94..7524d94 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.7  2000/10/02 15:15:41  morsch
-Use default streamer for AliGenCocktail
+/* $Id$ */
 
-Revision 1.6  2000/09/07 17:04:31  morsch
-In Streamer: TIter() after R__b << fEntries (Dmitri Yurevitch Peressounko)
-
-Revision 1.5  2000/06/09 20:28:51  morsch
-All coding rule violations except RS3 corrected (AM)
-
-Revision 1.4  1999/09/29 09:24:12  fca
-Introduction of the Copyright and cvs Log
+// Container class for AliGenerator through recursion.
+// Container is itself an AliGenerator.
+// What is stored are not the pointers to the generators directly but to objects of type
+// AliGenCocktail entry.   
+// The class provides also iterator functionality.  
+// Author: andreas.morsch@cern.ch 
+//
 
-*/
+#include <TList.h>
+#include <TObjArray.h>
+#include <TFormula.h>
 
 #include "AliGenCocktail.h"
 #include "AliGenCocktailEntry.h"
+#include "AliCollisionGeometry.h"
 #include "AliRun.h"
+#include "AliLog.h"
+#include "AliMC.h"
+#include "AliGenCocktailEventHeader.h"
 
 ClassImp(AliGenCocktail)
 
 AliGenCocktail::AliGenCocktail()
-                 :AliGenerator()
+    :AliGenerator(), 
+     fNGenerators(0),
+     fTotalRate(0.),
+     fRandom(kFALSE),
+     fUsePerEventRate(kFALSE),
+     fProb(0),
+     fEntries(0),
+     flnk1(0),
+     flnk2(0), 
+     fHeader(0)
 {
 // Constructor
-    fEntries = new TList;
-    fNGenerators=0;
-}
-
-AliGenCocktail::AliGenCocktail(const AliGenCocktail & cocktail)
-{
-// copy constructor
+    fName = "Cocktail";
+    fTitle= "Particle Generator using cocktail of generators";
 }
 
 AliGenCocktail::~AliGenCocktail()
 {
 // Destructor
     delete fEntries;
+    fEntries = 0;
+    //    delete fHeader; // It is removed in AliRunLoader
+    fHeader = 0;
 }
 
 void AliGenCocktail::
-AddGenerator(AliGenerator *Generator, char* Name, Float_t RateExp)
+AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp, TFormula* formula)
 {
 //
+// Add a generator to the list 
+// First check that list exists
+    if (!fEntries) fEntries = new TList();
+    fTotalRate += RateExp;
+//
 //  Forward parameters to the new generator
-    if(TestBit(kPtRange)) 
-    Generator->SetPtRange(fPtMin,fPtMax);
-    if(TestBit(kMomentumRange))
-    Generator->SetMomentumRange(fPMin,fPMax);
-
-    Generator->SetYRange(fYMin,fYMax);
-    Generator->
-       SetPhiRange(fPhiMin*180/TMath::Pi(),fPhiMax*180/TMath::Pi());
-    Generator->
-       SetThetaRange(fThetaMin*180/TMath::Pi(),fThetaMax*180/TMath::Pi());
-    Generator->
-       SetOrigin(fOrigin[0], fOrigin[1], fOrigin[2]);
-    Generator->
-       SetSigma(fOsigma[0], fOsigma[1], fOsigma[2]);
-    Generator->SetVertexSmear(fVertexSmear);
-    Generator->SetTrackingFlag(fTrackIt);    
+    if(TestBit(kPtRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange))) 
+       Generator->SetPtRange(fPtMin,fPtMax);
+    if(TestBit(kMomentumRange) && !(Generator->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange)))
+       Generator->SetMomentumRange(fPMin,fPMax);
+    
+    if (!(Generator->TestBit(kYRange)))    
+       Generator->SetYRange(fYMin,fYMax);
+    if (!(Generator->TestBit(kPhiRange)))   
+       Generator->SetPhiRange(fPhiMin*180/TMath::Pi(),fPhiMax*180/TMath::Pi());
+    if (!(Generator->TestBit(kThetaRange))) 
+       Generator->SetThetaRange(fThetaMin*180/TMath::Pi(),fThetaMax*180/TMath::Pi());
+    if (!(Generator->TestBit(kVertexRange))) {
+       Generator->SetOrigin(fOrigin[0], fOrigin[1], fOrigin[2]);
+       Generator->SetSigma(fOsigma[0], fOsigma[1], fOsigma[2]);
+       Generator->SetVertexSmear(fVertexSmear);
+       Generator->SetVertexSource(kContainer);
+    }
+    Generator->SetTrackingFlag(fTrackIt);
+    Generator->SetContainer(this);
+
+        
 //
 //  Add generator to list   
-    
+    char theName[256];
+    snprintf(theName, 256, "%s_%d",Name, fNGenerators);
+    Generator->SetName(theName);
+
     AliGenCocktailEntry *entry = 
        new AliGenCocktailEntry(Generator, Name, RateExp);
+    if (formula) entry->SetFormula(formula);    
      fEntries->Add(entry);
      fNGenerators++;
- }
+     flnk1 = 0;
+     flnk2 = 0;
+     fRandom  = kFALSE;
+     fHeader  = 0;
+}
 
   void AliGenCocktail::Init()
 {
@@ -92,8 +119,41 @@ AddGenerator(AliGenerator *Generator, char* Name, Float_t RateExp)
     //
     // Loop over generators and initialize
     while((entry = (AliGenCocktailEntry*)next())) {
+       if (fStack)  entry->Generator()->SetStack(fStack);
        entry->Generator()->Init();
     }  
+
+    next.Reset();
+
+    if (fRandom) {
+       fProb.Set(fNGenerators);
+       next.Reset();
+       Float_t sum = 0.;
+       while((entry = (AliGenCocktailEntry*)next())) {
+           sum += entry->Rate();
+       } 
+
+       next.Reset();
+       Int_t i = 0;
+       Float_t psum = 0.;
+       while((entry = (AliGenCocktailEntry*)next())) {
+           psum +=  entry->Rate() / sum;
+           fProb[i++] = psum;
+       }
+    }
+       next.Reset();
+}
+
+  void AliGenCocktail::FinishRun()
+{
+// Initialisation
+    TIter next(fEntries);
+    AliGenCocktailEntry *entry;
+    //
+    // Loop over generators and initialize
+    while((entry = (AliGenCocktailEntry*)next())) {
+       entry->Generator()->FinishRun();
+    }  
 }
 
  void AliGenCocktail::Generate()
@@ -101,41 +161,113 @@ AddGenerator(AliGenerator *Generator, char* Name, Float_t RateExp)
 //
 // Generate event 
     TIter next(fEntries);
-    AliGenCocktailEntry *entry;
-    AliGenCocktailEntry *e1;
-    AliGenCocktailEntry *e2;
-    TClonesArray *partArray = gAlice->Particles();
-    //
-    // Loop over generators and generate events
-    Int_t igen=0;
-    while((entry = (AliGenCocktailEntry*)next())) {
-       igen++;
-       if (igen ==1) {
+    AliGenCocktailEntry *entry = 0;
+    AliGenCocktailEntry *preventry = 0;
+    AliGenCocktailEntry *collentry = 0;
+    AliGenerator* gen = 0;
+    if (fHeader) delete fHeader;
+
+    
+    fHeader = new AliGenCocktailEventHeader("Cocktail Header");
+
+    const TObjArray *partArray = gAlice->GetMCApp()->Particles();
+
+//
+//  Generate the vertex position used by all generators
+//    
+    if(fVertexSmear == kPerEvent) Vertex();
+
+    TArrayF eventVertex;
+    eventVertex.Set(3);
+    for (Int_t j=0; j < 3; j++) eventVertex[j] = fVertex[j];
+
+    if (!fRandom) {
+       //
+       // Loop over generators and generate events
+       Int_t igen   = 0;
+       while((entry = (AliGenCocktailEntry*)next())) {
+          Int_t ntimes = 1;
+         if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
+         
+         igen++;
+         if (igen ==1) {
            entry->SetFirst(0);
-       } else {
+         } else {
            entry->SetFirst((partArray->GetEntriesFast())+1);
+         }
+         gen = entry->Generator();
+         if (gen->ProvidesCollisionGeometry()) collentry = entry; 
+         //
+         //      Handle case in which current generator needs collision geometry from previous generator
+          //
+         if (gen->NeedsCollisionGeometry() && (entry->Formula() == 0))
+           {
+             if (preventry && preventry->Generator()->ProvidesCollisionGeometry())
+               {
+                 gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
+               } else {
+               Fatal("Generate()", "No Collision Geometry Provided");
+             }
+           }
+         //
+         //      Number of signals is calculated from Collision Geometry
+         //
+         if (entry->Formula() != 0)
+           {
+             if (!collentry) {
+               Fatal("Generate()", "No Collision Geometry Provided");
+               return;
+             }
+             AliCollisionGeometry* coll = (collentry->Generator())->CollisionGeometry();
+             Float_t b  = coll->ImpactParameter();
+             Int_t nsig = Int_t(entry->Formula()->Eval(b));
+             if (nsig < 1) nsig = 1;
+             AliInfo(Form("Signal Events %13.3f %5d %5d\n", b, coll->HardScatters(), nsig));
+             ntimes = nsig;
+           }
+         
+         entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
+         
+         for (Int_t i = 0; i < ntimes; i++) entry->Generator()->Generate();
+         entry->SetLast(partArray->GetEntriesFast());
+         preventry = entry;
        }
-       entry->Generator()->Generate();
+    } else if (fRandom) {
+       //
+       // Select a generator randomly
+       //
+       Int_t i;
+       Float_t p0 =  gRandom->Rndm();
+
+       for (i = 0; i < fNGenerators; i++) {
+           if (p0 < fProb[i]) break;
+       }
+
+       entry = (AliGenCocktailEntry*) fEntries->At(i);
+       entry->SetFirst(0);
+       gen = entry->Generator();
+       gen->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
+       gen->Generate();
        entry->SetLast(partArray->GetEntriesFast());
-    }  
+    } 
+    
     next.Reset();
-    while((entry = (AliGenCocktailEntry*)next())) {
-       entry->PrintInfo();
-    }
-    for (entry=FirstGenerator();
-        entry;
-        entry=NextGenerator()
-       ) {
-       entry->PrintInfo();
-    }
-    for (FirstGeneratorPair(e1,e2);
-        (e1&&e2);
-        NextGeneratorPair(e1,e2)
-       ){
-       printf("\n -----------------------------");
-       e1->PrintInfo();
-       e2->PrintInfo();
-    }
+
+// Event Vertex
+    fHeader->SetPrimaryVertex(eventVertex);
+    fHeader->CalcNProduced();
+    gAlice->SetGenEventHeader(fHeader); 
+}
+
+void AliGenCocktail::SetVertexSmear(VertexSmear_t smear)
+{
+// Set vertex smearing and propagate it to the generators
+
+  AliGenerator::SetVertexSmear(smear);
+  TIter next(fEntries);
+  while (AliGenCocktailEntry* entry = (AliGenCocktailEntry*)next()) {
+    entry->Generator()->SetVertexSmear(smear);
+  }
 }
 
 AliGenCocktailEntry *  AliGenCocktail::FirstGenerator()
@@ -192,41 +324,12 @@ NextGeneratorPair(AliGenCocktailEntry*& e1, AliGenCocktailEntry*& e2)
     }
 }
 
-/*
-void AliGenCocktail::Streamer(TBuffer &R__b)
+void AliGenCocktail::AddHeader(AliGenEventHeader* header)
 {
-    // Stream an object of class AliGenCocktail.
-
-    AliGenCocktailEntry *entry;
-    
-    if (R__b.IsReading()) {
-       Version_t R__v = R__b.ReadVersion(); if (R__v) { }
-       AliGenerator::Streamer(R__b);
-       R__b >> fNGenerators;
-       R__b >> fEntries;
-       TIter next(fEntries);
-// Stream generation related information
-       while((entry = (AliGenCocktailEntry*)next())) {
-           entry->Streamer(R__b);
-       }  
-    } else {
-       R__b.WriteVersion(AliGenCocktail::IsA());
-       AliGenerator::Streamer(R__b);
-       R__b << fNGenerators;
-       R__b << fEntries;
-       TIter next(fEntries); 
-// Stream generation related information
-       while((entry = (AliGenCocktailEntry*)next())) {
-           entry->Streamer(R__b);
-       }  
-    }
+// Add a header to the list 
+    if (fHeader) fHeader->AddHeader(header);
 }
-*/
 
-AliGenCocktail& AliGenCocktail::operator=(const  AliGenCocktail& rhs)
-{
-// Assignment operator
-    return *this;
-}
+