/*
<img src="picts/AliRunClass.gif">
*/
//End_Html
/*
<img src="picts/alirun.gif">
*/
//End_Html
#include <TROOT.h>
#include <TRandom3.h>
#include <TSystem.h>
#include <TVirtualMC.h>
#include <TGeoManager.h>
#include <TTree.h>
#include "AliLog.h"
#include "AliDetector.h"
#include "AliHeader.h"
#include "AliMC.h"
#include "AliPDG.h"
#include "AliRun.h"
#include "AliStack.h"
#include "AliCDBManager.h"
#include "AliAlignObj.h"
#include "AliSimulation.h"
#include "AliLego.h"
AliRun *gAlice;
ClassImp(AliRun)
AliRun::AliRun():
fEventNrInRun(-1),
fModules(0),
fMCApp(0),
fNdets(0),
fConfigFunction(""),
fBaseFileName(""),
fRunLoader(0x0)
{
AliConfig::Instance();
}
AliRun::AliRun(const char *name, const char *title):
TNamed(name,title),
fEventNrInRun(-1),
fModules(new TObjArray(77)),
fMCApp(new AliMC(GetName(),GetTitle())),
fNdets(0),
fConfigFunction("Config();"),
fBaseFileName(""),
fRunLoader(0x0)
{
gAlice = this;
gRandom = new TRandom3();
if (gSystem->Getenv("CONFIG_SEED")) {
gRandom->SetSeed(static_cast<UInt_t>(atoi(gSystem->Getenv("CONFIG_SEED"))));
}
gROOT->GetListOfBrowsables()->Add(this,name);
}
AliRun::~AliRun()
{
gROOT->GetListOfBrowsables()->Remove(this);
if (fRunLoader)
{
TFolder* evfold = fRunLoader->GetEventFolder();
TFolder* modfold = dynamic_cast<TFolder*>(evfold->FindObjectAny(AliConfig::GetModulesFolderName()));
if(!modfold) AliFatal(Form("Folder %s not found\n",AliConfig::GetModulesFolderName().Data()));
TIter next(fModules);
AliModule *mod;
while((mod = (AliModule*)next()))
{
modfold->Remove(mod);
}
}
delete fMCApp;
delete TVirtualMC::GetMC();
if (fModules) {
fModules->Delete();
delete fModules;
}
}
void AliRun::InitLoaders()
{
AliDebug(1, "");
TIter next(fModules);
AliModule *mod;
while((mod = (AliModule*)next()))
{
mod->SetRunLoader(fRunLoader);
AliDetector *det = dynamic_cast<AliDetector*>(mod);
if (det)
{
AliDebug(2, Form("Adding %s", det->GetName()));
fRunLoader->AddLoader(det);
}
}
AliDebug(1, "Done");
}
void AliRun::Announce() const
{
printf("%70s",
"****************************************************************\n");
printf("%6s","*");printf("%64s","*\n");
printf("%6s","*");
printf(" You are running AliRoot version NewIO\n");
printf("%6s","*");
printf(" The SVN version for the current program is $Id$\n");
printf("%6s","*");printf("%64s","*\n");
printf("%70s",
"****************************************************************\n");
}
AliModule *AliRun::GetModule(const char *name) const
{
return dynamic_cast<AliModule*>(fModules->FindObject(name));
}
AliDetector *AliRun::GetDetector(const char *name) const
{
return dynamic_cast<AliDetector*>(fModules->FindObject(name));
}
Int_t AliRun::GetModuleID(const char *name) const
{
Int_t i=-1;
TObject *mod=fModules->FindObject(name);
if(mod) i=fModules->IndexOf(mod);
return i;
}
Int_t AliRun::GetEvent(Int_t event)
{
if (fRunLoader == 0x0)
{
AliError("RunLoader is not set. Can not load data.");
return -1;
}
fMCApp->ResetHits();
fMCApp->ResetTrackReferences();
fMCApp->ResetDigits();
fMCApp->ResetSDigits();
AliRunLoader::Instance()->GetEvent(event);
TIter next(fModules);
AliDetector *detector;
while((detector = dynamic_cast<AliDetector*>(next())))
{
detector->SetTreeAddress();
}
return AliRunLoader::Instance()->GetHeader()->GetNtrack();
}
void AliRun::SetBaseFile(const char *filename)
{
fBaseFileName = filename;
}
void AliRun::Hits2Digits(const char *selected)
{
for (Int_t nevent=0; nevent<AliRunLoader::Instance()->TreeE()->GetEntries(); nevent++) {
GetEvent(nevent);
Hits2SDigits(selected);
SDigits2Digits(selected);
}
}
void AliRun::Tree2Tree(Option_t *option, const char *selected)
{
const char *oS = strstr(option,"S");
const char *oD = strstr(option,"D");
const char *oR = strstr(option,"R");
TObjArray *detectors = Detectors();
TIter next(detectors);
AliDetector *detector = 0;
while((detector = dynamic_cast<AliDetector*>(next()))) {
if (selected)
if (strcmp(detector->GetName(),selected)) continue;
if (detector->IsActive())
{
AliLoader* loader = detector->GetLoader();
if (loader == 0x0) continue;
if (oS)
{
AliDebug(1, Form("Processing Hits2SDigits for %s ...", detector->GetName()));
loader->LoadHits("read");
if (loader->TreeS() == 0x0) loader->MakeTree("S");
detector->MakeBranch(option);
detector->SetTreeAddress();
detector->Hits2SDigits();
loader->UnloadHits();
loader->UnloadSDigits();
}
if (oD)
{
AliDebug(1, Form("Processing SDigits2Digits for %s ...", detector->GetName()));
loader->LoadSDigits("read");
if (loader->TreeD() == 0x0) loader->MakeTree("D");
detector->MakeBranch(option);
detector->SetTreeAddress();
detector->SDigits2Digits();
loader->UnloadSDigits();
loader->UnloadDigits();
}
if (oR)
{
AliDebug(1, Form("Processing Digits2Reco for %s ...", detector->GetName()));
loader->LoadDigits("read");
if (loader->TreeR() == 0x0) loader->MakeTree("R");
detector->MakeBranch(option);
detector->SetTreeAddress();
detector->Digits2Reco();
loader->UnloadDigits();
loader->UnloadRecPoints();
}
}
}
}
void AliRun::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
if (!gAlice) gAlice = this;
AliRun::Class()->ReadBuffer(R__b, this);
gROOT->GetListOfBrowsables()->Add(this,"Run");
gRandom = new TRandom3();
} else {
AliRun::Class()->WriteBuffer(R__b, this);
}
}
void AliRun::SetGenEventHeader(AliGenEventHeader* header)
{
AliRunLoader::Instance()->GetHeader()->SetGenEventHeader(header);
}
Int_t AliRun::GetEvNumber() const
{
if (fRunLoader == 0x0)
{
AliError("RunLoader is not set. Can not load data.");
return -1;
}
return fRunLoader->GetEventNumber();
}
void AliRun::SetRunLoader(AliRunLoader* rloader)
{
fRunLoader = rloader;
if (fRunLoader == 0x0) return;
TString evfoldname;
TFolder* evfold = fRunLoader->GetEventFolder();
if (evfold) evfoldname = evfold->GetName();
else AliFatal("Did not get Event Folder from Run Loader");
if ( fRunLoader->GetAliRun() )
{
if (fRunLoader->GetAliRun() != this )
{
AliFatal("AliRun is already in Folder and it is not this object");
return;
}
}
else
{
evfold->Add(this);
}
TIter next(fModules);
AliModule *module;
while((module = (AliModule*)next()))
{
if (evfold) AliConfig::Instance()->Add(module,evfoldname);
module->SetRunLoader(fRunLoader);
AliDetector* detector = dynamic_cast<AliDetector*>(module);
if (detector)
{
AliLoader* loader = fRunLoader->GetLoader(detector);
if (loader == 0x0)
{
AliError(Form("Can not get loader for detector %s", detector->GetName()));
}
else
{
AliDebug(1, Form("Setting loader for detector %s", detector->GetName()));
detector->SetLoader(loader);
}
}
}
}
void AliRun::AddModule(AliModule* mod)
{
if (mod == 0x0) return;
if (strlen(mod->GetName()) == 0) return;
if (GetModuleID(mod->GetName()) >= 0) return;
AliDebug(1, mod->GetName());
if (fRunLoader == 0x0) AliConfig::Instance()->Add(mod);
else AliConfig::Instance()->Add(mod,fRunLoader->GetEventFolder()->GetName());
Modules()->Add(mod);
fNdets++;
}