///////////////////////////////////////////////////////////////////////////////
// //
-// Particles stack class
+// Particles stack class //
+// Implements the TMCVirtualStack of the Virtual Monte Carlo //
+// Holds the particles transported during simulation //
+// Is used to compare results of reconstruction with simulation //
+// Author A.Morsch //
// //
///////////////////////////////////////////////////////////////////////////////
-#include <Riostream.h>
#include <TObjArray.h>
#include <TParticle.h>
#include <TParticlePDG.h>
#include <TTree.h>
-#include <TFile.h>
-#include <TFolder.h>
-#include <TROOT.h>
-#include "AliStack.h"
-#include "AliRun.h"
-#include "AliModule.h"
+#include "AliLog.h"
#include "AliHit.h"
+#include "AliModule.h"
+#include "AliRun.h"
+#include "AliMC.h"
+#include "AliRunLoader.h"
+#include "AliStack.h"
ClassImp(AliStack)
fParticleMap(0),
fParticleFileMap(0),
fParticleBuffer(0),
+ fCurrentTrack(0),
fTreeK(0),
fNtrack(0),
fNprimary(0),
fCurrent(-1),
fCurrentPrimary(-1),
fHgwmk(0),
- fLoadPoint(0)
+ fLoadPoint(0),
+ fEventFolderName(AliConfig::GetDefaultEventFolderName())
{
//
// Default constructor
}
//_______________________________________________________________________
-AliStack::AliStack(Int_t size):
+AliStack::AliStack(Int_t size, const char* evfoldname):
fParticles(new TClonesArray("TParticle",1000)),
fParticleMap(new TObjArray(size)),
fParticleFileMap(0),
fParticleBuffer(0),
+ fCurrentTrack(0),
fTreeK(0),
fNtrack(0),
fNprimary(0),
fCurrent(-1),
fCurrentPrimary(-1),
fHgwmk(0),
- fLoadPoint(0)
+ fLoadPoint(0),
+ fEventFolderName(evfoldname)
{
//
// Constructor
fParticleMap(0),
fParticleFileMap(0),
fParticleBuffer(0),
+ fCurrentTrack(0),
fTreeK(0),
fNtrack(0),
fNprimary(0),
fCurrent(-1),
fCurrentPrimary(-1),
fHgwmk(0),
- fLoadPoint(0)
+ fLoadPoint(0),
+ fEventFolderName()
{
//
// Copy constructor
}
//_______________________________________________________________________
-void AliStack::Copy(AliStack&) const
+void AliStack::Copy(TObject&) const
{
- Fatal("Copy","Not implemented!\n");
+ AliFatal("Not implemented!");
}
//_______________________________________________________________________
delete fParticles;
}
delete fParticleMap;
- delete fTreeK;
+ //PH??? delete fTreeK;
}
//
//
//_____________________________________________________________________________
-void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
- Float_t *vpos, Float_t *polar, Float_t tof,
- TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
+void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg, Float_t *pmom,
+ Float_t *vpos, Float_t *polar, Float_t tof,
+ TMCProcess mech, Int_t &ntr, Float_t weight, Int_t is)
{
//
// Load a track on the stack
// mass,e,fNtrack,pdg,parent,done,vpos[0],vpos[1],vpos[2],pmom[0],pmom[1],pmom[2],kS);
- SetTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
+ PushTrack(done, parent, pdg, pmom[0], pmom[1], pmom[2], e,
vpos[0], vpos[1], vpos[2], tof, polar[0], polar[1], polar[2],
mech, ntr, weight, is);
} else {
- Warning("SetTrack", "Particle type %d not defined in PDG Database !\n", pdg);
- Warning("SetTrack", "Particle skipped !\n");
+ AliWarning(Form("Particle type %d not defined in PDG Database !", pdg));
+ AliWarning("Particle skipped !");
}
}
//_____________________________________________________________________________
-void AliStack::SetTrack(Int_t done, Int_t parent, Int_t pdg,
+void AliStack::PushTrack(Int_t done, Int_t parent, Int_t pdg,
Double_t px, Double_t py, Double_t pz, Double_t e,
Double_t vx, Double_t vy, Double_t vz, Double_t tof,
Double_t polx, Double_t poly, Double_t polz,
const Int_t kFirstDaughter=-1;
const Int_t kLastDaughter=-1;
-
+
+
TClonesArray &particles = *fParticles;
TParticle* particle
= new(particles[fLoadPoint++])
// Declare that the daughter information is valid
particle->SetBit(kDaughtersBit);
// Add the particle to the stack
+
+
fParticleMap->AddAtAndExpand(particle, fNtrack);//CHECK!!
if(parent>=0) {
if(particle->GetFirstDaughter()<0) particle->SetFirstDaughter(fNtrack);
}
else {
- printf("Error in AliStack::SetTrack: Parent %d does not exist\n",parent);
+ AliError(Form("Parent %d does not exist",parent));
}
}
else {
}
//_____________________________________________________________________________
-TParticle* AliStack::GetNextTrack(Int_t& itrack)
+TParticle* AliStack::PopNextTrack(Int_t& itrack)
{
//
// Returns next track from stack of particles
return track;
}
-/*
-//_____________________________________________________________________________
-void AliStack::GetNextTrack(Int_t& itrack, Int_t& pdg,
- Double_t& px, Double_t& py, Double_t& pz, Double_t& e,
- Double_t& vx, Double_t& vy, Double_t& vz, Double_t& tof,
- Double_t& polx, Double_t& poly, Double_t& polz)
-{
- //
- // Return next track from stack of particles
- //
-
-
- TParticle* track = GetNextParticle();
-// cout << "GetNextTrack():" << fCurrent << fNprimary << endl;
-
- if (track) {
- itrack = fCurrent;
- pdg = track->GetPdgCode();
- px = track->Px();
- py = track->Py();
- pz = track->Pz();
- e = track->Energy();
- vx = track->Vx();
- vy = track->Vy();
- vz = track->Vz();
- tof = track->T();
- TVector3 pol;
- track->GetPolarisation(pol);
- polx = pol.X();
- poly = pol.Y();
- polz = pol.Z();
- track->SetBit(kDoneBit);
-// cout << "Filled params" << endl;
- }
- else
- itrack = -1;
-
- //
- // stop and start timer when we start a primary track
- Int_t nprimaries = fNprimary;
- if (fCurrent >= nprimaries) return;
- if (fCurrent < nprimaries-1) {
- fTimer.Stop();
- track=(TParticle*) fParticleMap->At(fCurrent+1);
- // track->SetProcessTime(fTimer.CpuTime());
- }
- fTimer.Start();
-}
-
-*/
//_____________________________________________________________________________
-TParticle* AliStack::GetPrimaryForTracking(Int_t i)
+TParticle* AliStack::PopPrimaryForTracking(Int_t i)
{
//
// Returns i-th primary particle if it is flagged to be tracked,
return 0;
}
-
//_____________________________________________________________________________
void AliStack::PurifyKine()
{
TArrayI map(particles.GetLast()+1);
// Save in Header total number of tracks before compression
-
// If no tracks generated return now
if(fHgwmk+1 == fNtrack) return;
// First pass, invalid Daughter information
+
for(i=0; i<fNtrack; i++) {
// Preset map, to be removed later
if(i<=fHgwmk) map[i]=i ;
if((part=dynamic_cast<TParticle*>(particles.At(i)))) {
//
// Check of this track should be kept for physics reasons
- if (KeepPhysics(part)) KeepTrack(i);
+ if (KeepPhysics(part)) KeepTrack(i);
//
part->ResetBit(kDaughtersBit);
part->SetFirstDaughter(-1);
}
// Now loop on all registered hit lists
- TList* hitLists = gAlice->GetHitLists();
+ TList* hitLists = gAlice->GetMCApp()->GetHitLists();
TIter next(hitLists);
TCollection *hitList;
+
while((hitList = dynamic_cast<TCollection*>(next()))) {
TIter nexthit(hitList);
AliHit *hit;
while((hit = dynamic_cast<AliHit*>(nexthit()))) {
- hit->SetTrack(map[hit->GetTrack()]);
+ hit->SetTrack(map[hit->GetTrack()]);
}
}
// This for detectors which have a special mapping mechanism
// for hits, such as TPC and TRD
//
-
+
TObjArray* modules = gAlice->Modules();
TIter nextmod(modules);
AliModule *detector;
detector->RemapTrackHitIDs(map.GetArray());
detector->RemapTrackReferencesIDs(map.GetArray());
}
-
+ //
+ gAlice->GetMCApp()->RemapTrackReferencesIDs(map.GetArray());
+
// Now the output bit, from fHgwmk to nkeep we write everything and we erase
if(nkeep>fParticleFileMap.GetSize()) fParticleFileMap.Set(Int_t (nkeep*1.5));
for (i=fHgwmk+1; i<nkeep; ++i) {
fParticleBuffer = dynamic_cast<TParticle*>(particles.At(i));
- fParticleFileMap[i]=static_cast<Int_t>(fTreeK->GetEntries());
- fTreeK->Fill();
+ fParticleFileMap[i]=static_cast<Int_t>(TreeK()->GetEntries());
+ TreeK()->Fill();
particles[i]=fParticleBuffer=0;
- }
+ }
for (i=nkeep; i<fNtrack; ++i) particles[i]=0;
Int_t toshrink = fNtrack-fHgwmk-1;
fLoadPoint-=toshrink;
+
+
for(i=fLoadPoint; i<fLoadPoint+toshrink; ++i) fParticles->RemoveAt(i);
fNtrack=nkeep;
fHgwmk=nkeep-1;
- // delete [] map;
}
-//_____________________________________________________________________________
+void AliStack::ReorderKine()
+{
+//
+// In some transport code children might not come in a continuous sequence.
+// In this case the stack has to be reordered in order to establish the
+// mother daughter relation using index ranges.
+//
+ if(fHgwmk+1 == fNtrack) return;
+
+ //
+ // Howmany secondaries have been produced ?
+ Int_t nNew = fNtrack - fHgwmk - 1;
+
+ if (nNew > 0) {
+ Int_t i, j;
+ TObjArray &particles = *fParticleMap;
+ TArrayI map1(nNew);
+ //
+ // Copy pointers to temporary array
+ TParticle** tmp = new TParticle*[nNew];
+
+ for (i = 0; i < nNew; i++) {
+ if (particles.At(fHgwmk + 1 + i)) {
+// tmp[i] = (TParticle*) (particles.At(fHgwmk + 1 + i))->Clone();
+ tmp[i] = (TParticle*) (particles.At(fHgwmk + 1 + i));
+
+// if (((TParticle*) (particles.At(fHgwmk + 1 + i)))->TestBit(kKeepBit))
+// tmp[i]->SetBit(kKeepBit);
+// if (((TParticle*) (particles.At(fHgwmk + 1 + i)))->TestBit(kDoneBit))
+// tmp[i]->SetBit(kDoneBit);
+ } else {
+ tmp[i] = 0x0;
+ }
+ map1[i] = -99;
+ }
+
+
+ //
+ // Reset LoadPoint
+ //
+ fLoadPoint = fHgwmk + 1;
+ //
+ // Re-Push particles into stack
+ // The outer loop is over parents, the inner over children.
+ // -1 refers to the primary particle
+ //
+ for (i = -1; i < nNew - 1; i++) {
+ Int_t ipa;
+ TParticle* parP;
+ if (i == -1) {
+ ipa = tmp[0]->GetFirstMother();
+ parP =dynamic_cast<TParticle*>(particles.At(ipa));
+ } else {
+ ipa = (fHgwmk + 1 + i);
+ // Skip deleted particles
+ if (!tmp[i]) continue;
+ // Skip particles without children
+ if (tmp[i]->GetFirstDaughter() == -1) continue;
+ parP = tmp[i];
+ }
+ // Reset daughter information
+ parP->SetFirstDaughter(-1);
+ parP->SetLastDaughter(-1);
+ for (j = i + 1; j < nNew; j++) {
+ // Skip deleted particles
+ if (!tmp[j]) continue;
+ // Skip particles already handled
+ if (map1[j] != -99) continue;
+ Int_t jpa = tmp[j]->GetFirstMother();
+ // Check if daughter of current parent
+ if (jpa == ipa) {
+ particles[fLoadPoint] = tmp[j];
+ // Re-establish daughter information
+ parP->SetLastDaughter(fLoadPoint);
+ if (parP->GetFirstDaughter() == -1) parP->SetFirstDaughter(fLoadPoint);
+ // Set Mother information
+ if (i != -1) {
+ tmp[j]->SetFirstMother(map1[i]);
+ }
+ // Build the map
+ map1[j] = fLoadPoint;
+ // Increase load point
+ fLoadPoint++;
+ }
+ } // children
+ } // parents
+
+ delete[] tmp;
+
+ //
+ // Build map for remapping of hits
+ //
+ TArrayI map(fNtrack);
+ for (i = 0; i < fNtrack; i ++) {
+ if (i <= fHgwmk) {
+ map[i] = i;
+ } else{
+ map[i] = map1[i - fHgwmk -1];
+ }
+ }
+
+ // Now loop on all registered hit lists
+
+ TList* hitLists = gAlice->GetMCApp()->GetHitLists();
+ TIter next(hitLists);
+ TCollection *hitList;
+
+ while((hitList = dynamic_cast<TCollection*>(next()))) {
+ TIter nexthit(hitList);
+ AliHit *hit;
+ while((hit = dynamic_cast<AliHit*>(nexthit()))) {
+ hit->SetTrack(map[hit->GetTrack()]);
+ }
+ }
+
+ //
+ // This for detectors which have a special mapping mechanism
+ // for hits, such as TPC and TRD
+ //
+
+ TObjArray* modules = gAlice->Modules();
+ TIter nextmod(modules);
+ AliModule *detector;
+ while((detector = dynamic_cast<AliModule*>(nextmod()))) {
+ detector->RemapTrackHitIDs(map.GetArray());
+ detector->RemapTrackReferencesIDs(map.GetArray());
+ }
+ gAlice->GetMCApp()->RemapTrackReferencesIDs(map.GetArray());
+ } // new particles poduced
+}
+
Bool_t AliStack::KeepPhysics(TParticle* part)
{
//
//_____________________________________________________________________________
void AliStack::FinishEvent()
{
- //
- // Write out the kinematics that was not yet filled
- //
+//
+// Write out the kinematics that was not yet filled
+//
- // Update event header
+// Update event header
- if (!fTreeK) {
+ if (!TreeK()) {
// Fatal("FinishEvent", "No kinematics tree is defined.");
// Don't panic this is a probably a lego run
return;
-
}
CleanParents();
- if(fTreeK->GetEntries() ==0) {
+ if(TreeK()->GetEntries() ==0) {
// set the fParticleFileMap size for the first time
fParticleFileMap.Set(fHgwmk+1);
}
for(Int_t i=0; i<fHgwmk+1; ++i)
if((part=fParticleMap->At(i))) {
fParticleBuffer = dynamic_cast<TParticle*>(part);
- fParticleFileMap[i]= static_cast<Int_t>(fTreeK->GetEntries());
- fTreeK->Fill();
+ fParticleFileMap[i]= static_cast<Int_t>(TreeK()->GetEntries());
+ TreeK()->Fill();
//PH (*fParticleMap)[i]=fParticleBuffer=0;
fParticleBuffer=0;
fParticleMap->AddAt(0,i);
// When all primaries were filled no particle!=0
// should be left => to be removed later.
- if (allFilled) printf("Why != 0 part # %d?\n",i);
+ if (allFilled) AliWarning(Form("Why != 0 part # %d?\n",i));
}
- else {
+ else
+ {
// // printf("Why = 0 part # %d?\n",i); => We know.
// break;
- // we don't break now in order to be sure there is no
- // particle !=0 left.
- // To be removed later and replaced with break.
- if(!allFilled) allFilled = kTRUE;
- }
-// cout << "Nof particles: " << fNtrack << endl;
- //Reset();
+ // we don't break now in order to be sure there is no
+ // particle !=0 left.
+ // To be removed later and replaced with break.
+ if(!allFilled) allFilled = kTRUE;
+ }
}
-
//_____________________________________________________________________________
+
void AliStack::FlagTrack(Int_t track)
{
//
//
// Resets stack
//
-
+
fNtrack=0;
fNprimary=0;
fHgwmk=0;
fLoadPoint=0;
fCurrent = -1;
+ fTreeK = 0x0;
ResetArrays(size);
}
//
// Set high water mark for last track in event
//
-
+
+
fHgwmk = fNtrack-1;
fCurrentPrimary=fHgwmk;
-
// Set also number of primary tracks
fNprimary = fHgwmk+1;
fNtrack = fHgwmk+1;
// and the fParticleFileMap[i] give the same;
// give the fatal error if not
if (entry != fParticleFileMap[i]) {
- Fatal("Particle",
+ AliFatal(Form(
"!! The algorithmic way and map are different: !!\n entry: %d map: %d",
- entry, fParticleFileMap[i]);
+ entry, fParticleFileMap[i]));
}
- fTreeK->GetEntry(entry);
+ TreeK()->GetEntry(entry);
new ((*fParticles)[nentries]) TParticle(*fParticleBuffer);
fParticleMap->AddAt((*fParticles)[nentries],i);
}
}
//_____________________________________________________________________________
-Int_t AliStack::CurrentTrackParent() const
+Int_t AliStack::GetCurrentParentTrackNumber() const
{
//
// Return number of the parent of the current track
if (current)
return current->GetFirstMother();
else {
- Warning("CurrentTrackParent", "Current track not found in the stack");
+ AliWarning("Current track not found in the stack");
return -1;
}
}
Int_t i;
- printf(
- "\n\n=======================================================================\n");
+ printf("\n\n=======================================================================\n");
for (i=0;i<fNtrack;i++)
{
TParticle* particle = Particle(i);
Warning("DumpPStack", "No particle with id %d.", i);
}
- printf(
- "\n=======================================================================\n\n");
+ printf("\n=======================================================================\n\n");
// print particle file map
printf("\nParticle file map: \n");
particle = dynamic_cast<TParticle*>(fParticleMap->At(i));
if ((particle) && (!particle->TestBit(kDoneBit))) {
fCurrent=i;
- //cout << "GetNextParticle() - secondary "
- // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
return particle;
}
}
fCurrent = fCurrentPrimary;
particle = dynamic_cast<TParticle*>(fParticleMap->At(fCurrentPrimary--));
if ((particle) && (!particle->TestBit(kDoneBit))) {
- //cout << "GetNextParticle() - primary "
- // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
return particle;
}
}
// nothing to be tracked
fCurrent = -1;
- //cout << "GetNextParticle() - none "
- // << fNtrack << " " << fHgwmk << " " << fCurrent << endl;
+
+
return particle;
}
+//__________________________________________________________________________________________
+TTree* AliStack::TreeK()
+{
+//returns TreeK
+ if (fTreeK)
+ {
+ return fTreeK;
+ }
+ else
+ {
+ AliRunLoader *rl = AliRunLoader::GetRunLoader(fEventFolderName);
+ if (rl == 0x0)
+ {
+ AliFatal(Form("Can not get RunLoader from event folder named %s",fEventFolderName.Data()));
+ return 0x0;//pro forma
+ }
+ fTreeK = rl->TreeK();
+ if ( fTreeK )
+ {
+ ConnectTree();
+ }
+ else
+ {
+ //don't panic - could be Lego
+ AliWarning(Form("Can not get TreeK from RL. Ev. Folder is %s",fEventFolderName.Data()));
+ }
+ }
+ return fTreeK;//never reached
+}
//__________________________________________________________________________________________
-void AliStack::MakeTree(Int_t event, const char * /*file*/)
+
+void AliStack::ConnectTree()
{
//
-// Make Kine tree and creates branch for writing particles
-//
- TBranch *branch=0;
- // Make Kinematics Tree
- char hname[30];
- if (!fTreeK) {
- sprintf(hname,"TreeK%d",event);
- fTreeK = new TTree(hname,"Kinematics");
- // Create a branch for particles
- branch = fTreeK->Branch("Particles", "TParticle", &fParticleBuffer, 4000);
- fTreeK->Write(0,TObject::kOverwrite);
- }
+// Creates branch for writing particles
+//
+ AliDebug(1, "Connecting TreeK");
+ if (fTreeK == 0x0)
+ {
+ if (TreeK() == 0x0)
+ {
+ AliFatal("Parameter is NULL");//we don't like such a jokes
+ return;
+ }
+ return;//in this case TreeK() calls back this method (ConnectTree)
+ //tree after setting fTreeK, the rest was already executed
+ //it is safe to return now
+ }
+
+ // Create a branch for particles
+
+ AliDebug(2, Form("Tree name is %s",fTreeK->GetName()));
+
+ if (fTreeK->GetDirectory())
+ {
+ AliDebug(2, Form("and dir is %s",fTreeK->GetDirectory()->GetName()));
+ }
+ else
+ AliWarning("DIR IS NOT SET !!!");
+
+ TBranch *branch=fTreeK->GetBranch(AliRunLoader::GetKineBranchName());
+ if(branch == 0x0)
+ {
+ branch = fTreeK->Branch(AliRunLoader::GetKineBranchName(), "TParticle", &fParticleBuffer, 4000);
+ AliDebug(2, "Creating Branch in Tree");
+ }
+ else
+ {
+ AliDebug(2, "Branch Found in Tree");
+ branch->SetAddress(&fParticleBuffer);
+ }
+ if (branch->GetDirectory())
+ {
+ AliDebug(1, Form("Branch Dir Name is %s",branch->GetDirectory()->GetName()));
+ }
+ else
+ AliWarning("Branch Dir is NOT SET");
}
+//__________________________________________________________________________________________
-//_____________________________________________________________________________
-void AliStack::BeginEvent(Int_t event)
+
+void AliStack::BeginEvent()
{
// start a new event
-//
-//
- fNprimary = 0;
- fNtrack = 0;
-
- char hname[30];
- if(fTreeK) {
- fTreeK->Reset();
- sprintf(hname,"TreeK%d",event);
- fTreeK->SetName(hname);
- }
+ Reset();
}
//_____________________________________________________________________________
void AliStack::FinishRun()
{
// Clean TreeK information
- if (fTreeK) {
- delete fTreeK; fTreeK = 0;
- }
}
-
//_____________________________________________________________________________
-Bool_t AliStack::GetEvent(Int_t event)
+
+Bool_t AliStack::GetEvent()
{
//
// Get new event from TreeK
// Reset/Create the particle stack
- if (fTreeK) delete fTreeK;
-
- // Get Kine Tree from file
- char treeName[20];
- sprintf(treeName,"TreeK%d",event);
- fTreeK = dynamic_cast<TTree*>(gDirectory->Get(treeName));
+ fTreeK = 0x0;
- if (fTreeK)
- fTreeK->SetBranchAddress("Particles", &fParticleBuffer);
- else {
- // Error("GetEvent","cannot find Kine Tree for event:%d\n",event);
- Warning("GetEvent","cannot find Kine Tree for event:%d\n",event);
+ if (TreeK() == 0x0) //forces connecting
+ {
+ AliError("cannot find Kine Tree for current event");
return kFALSE;
- }
-// printf("\n primaries %d", fNprimary);
-// printf("\n tracks %d", fNtrack);
+ }
- Int_t size = static_cast<Int_t>(fTreeK->GetEntries());
+ Int_t size = (Int_t)TreeK()->GetEntries();
ResetArrays(size);
return kTRUE;
}
+//_____________________________________________________________________________
+
+void AliStack::SetEventFolderName(const char* foldname)
+{
+ //Sets event folder name
+ fEventFolderName = foldname;
+}