Misplaced initialisation of ntimes corrected.
[u/mrichter/AliRoot.git] / EVGEN / AliGenCocktail.cxx
index 89a4f66..7524d94 100644 (file)
 
 #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"
 
@@ -38,6 +40,7 @@ ClassImp(AliGenCocktail)
 AliGenCocktail::AliGenCocktail()
     :AliGenerator(), 
      fNGenerators(0),
+     fTotalRate(0.),
      fRandom(kFALSE),
      fUsePerEventRate(kFALSE),
      fProb(0),
@@ -61,13 +64,13 @@ AliGenCocktail::~AliGenCocktail()
 }
 
 void AliGenCocktail::
-AddGenerator(AliGenerator *Generator, const 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->TestBit(kPtRange)) && !(Generator->TestBit(kMomentumRange))) 
@@ -89,17 +92,17 @@ AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
     }
     Generator->SetTrackingFlag(fTrackIt);
     Generator->SetContainer(this);
-    
+
         
 //
 //  Add generator to list   
     char theName[256];
-    sprintf(theName, "%s_%d",Name, fNGenerators);
+    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;
@@ -160,13 +163,14 @@ AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
     TIter next(fEntries);
     AliGenCocktailEntry *entry = 0;
     AliGenCocktailEntry *preventry = 0;
+    AliGenCocktailEntry *collentry = 0;
     AliGenerator* gen = 0;
     if (fHeader) delete fHeader;
 
     
     fHeader = new AliGenCocktailEventHeader("Cocktail Header");
 
-    TObjArray *partArray = gAlice->GetMCApp()->Particles();
+    const TObjArray *partArray = gAlice->GetMCApp()->Particles();
 
 //
 //  Generate the vertex position used by all generators
@@ -180,34 +184,54 @@ AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
     if (!fRandom) {
        //
        // Loop over generators and generate events
-       Int_t igen=0;
+       Int_t igen   = 0;
        while((entry = (AliGenCocktailEntry*)next())) {
-           if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
-           
-           igen++;
-           if (igen ==1) {
-               entry->SetFirst(0);
-           } else {
-               entry->SetFirst((partArray->GetEntriesFast())+1);
-           }
-//
-//      Handle case in which current generator needs collision geometry from previous generator
-//
-           gen = entry->Generator();
-           if (gen->NeedsCollisionGeometry())
+          Int_t ntimes = 1;
+         if (fUsePerEventRate && (gRandom->Rndm() > entry->Rate())) continue;
+         
+         igen++;
+         if (igen ==1) {
+           entry->SetFirst(0);
+         } 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())
+             if (preventry && preventry->Generator()->ProvidesCollisionGeometry())
                {
-                   gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
+                 gen->SetCollisionGeometry(preventry->Generator()->CollisionGeometry());
                } else {
-                   Fatal("Generate()", "No Collision Geometry Provided");
-               }
+               Fatal("Generate()", "No Collision Geometry Provided");
+             }
            }
-           entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
-           entry->Generator()->Generate();
-           entry->SetLast(partArray->GetEntriesFast());
-           preventry = entry;
-       }  
+         //
+         //      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;
+       }
     } else if (fRandom) {
        //
        // Select a generator randomly
@@ -222,8 +246,8 @@ AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
        entry = (AliGenCocktailEntry*) fEntries->At(i);
        entry->SetFirst(0);
        gen = entry->Generator();
-       entry->Generator()->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
-       entry->Generator()->Generate();
+       gen->SetVertex(fVertex.At(0), fVertex.At(1), fVertex.At(2));
+       gen->Generate();
        entry->SetLast(partArray->GetEntriesFast());
     } 
     
@@ -231,6 +255,7 @@ AddGenerator(AliGenerator *Generator, const char* Name, Float_t RateExp)
 
 // Event Vertex
     fHeader->SetPrimaryVertex(eventVertex);
+    fHeader->CalcNProduced();
     gAlice->SetGenEventHeader(fHeader); 
 }