X-Git-Url: http://git.uio.no/git/?a=blobdiff_plain;f=STEER%2FAliRunDigitizer.cxx;h=027c19b811c3b248c080328282ab5c344df19c81;hb=909eee575ce9f77645113230cc0acd685d40bc71;hp=44b9658e10a46a0f9ecbbbf0924031513f974b39;hpb=f34e67f98fbafd1c7c5f62f7de4929ca2d3b46f4;p=u%2Fmrichter%2FAliRoot.git diff --git a/STEER/AliRunDigitizer.cxx b/STEER/AliRunDigitizer.cxx index 44b9658e10a..027c19b811c 100644 --- a/STEER/AliRunDigitizer.cxx +++ b/STEER/AliRunDigitizer.cxx @@ -13,29 +13,9 @@ * provided "as is" without express or implied warranty. * **************************************************************************/ -/* -$Log$ -Revision 1.8 2001/10/21 18:38:43 hristov -Several pointers were set to zero in the default constructors to avoid memory management problems +/* $Id$ */ -Revision 1.7 2001/10/04 15:56:07 jchudoba -TTask inheritance - -Revision 1.4 2001/09/19 06:23:50 jchudoba -Move some tasks to AliStream and AliMergeCombi classes - -Revision 1.3 2001/07/30 14:04:18 jchudoba -correct bug in the initialization - -Revision 1.2 2001/07/28 10:44:32 hristov -Loop variable declared once; typos corrected - -Revision 1.1 2001/07/27 12:59:00 jchudoba -Manager class for merging/digitization - -*/ - -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ // // AliRunDigitizer.cxx // @@ -55,7 +35,7 @@ Manager class for merging/digitization // and the I/O configured (you have to specify input files // and an output file). The manager connects appropriate trees from // the input files according a combination returned by AliMergeCombi -// classcreates. It creates TreeD in the output and runs once per +// class. It creates TreeD in the output and runs once per // event Digitize method of all existing AliDetDigitizers // (without any option). AliDetDigitizers ask manager // for a TTree with input (manager->GetInputTreeS(Int_t i), @@ -77,307 +57,439 @@ Manager class for merging/digitization // same geometry in all input files), gAlice is taken from the first // input file on the first stream. // -// Example with MUON digitizer: +// Example with MUON digitizer, no merging, just digitization +// +// AliRunDigitizer * manager = new AliRunDigitizer(1,1); +// manager->SetInputStream(0,"galice.root"); +// AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager); +// manager->Exec(""); +// +// Example with MUON digitizer, merge all events from +// galice.root (signal) file with events from bgr.root +// (background) file. Number of merged events is +// min(number of events in galice.root, number of events in bgr.root) +// +// AliRunDigitizer * manager = new AliRunDigitizer(2,1); +// manager->SetInputStream(0,"galice.root"); +// manager->SetInputStream(1,"bgr.root"); +// AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager); +// manager->Exec(""); +// +// Example with MUON digitizer, save digits in a new file digits.root, +// process only 1 event // // AliRunDigitizer * manager = new AliRunDigitizer(2,1); -// manager->SetInputStream(0,"1track_10events_phi45_60.root"); -// manager->SetInputStream(1,"1track_10events_phi120_135.root"); +// manager->SetInputStream(0,"galice.root"); +// manager->SetInputStream(1,"bgr.root"); // manager->SetOutputFile("digits.root"); // AliMUONDigitizer *dMUON = new AliMUONDigitizer(manager); // manager->SetNrOfEventsToWrite(1); // manager->Exec(""); // -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ // system includes -#include - // ROOT includes -#include "TFile.h" -#include "TTree.h" -#include "TList.h" +#include +class TList; // AliROOT includes -#include "AliRunDigitizer.h" +#include "AliLog.h" #include "AliDigitizer.h" -#include "AliRun.h" #include "AliHeader.h" -#include "TParticle.h" -#include "AliStream.h" +#include "AliLoader.h" #include "AliMergeCombi.h" +#include "AliRun.h" +#include "AliRunDigitizer.h" +#include "AliRunLoader.h" ClassImp(AliRunDigitizer) -//////////////////////////////////////////////////////////////////////// -AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb) : TTask("AliRunDigitizer","The manager for Merging") +const TString AliRunDigitizer::fgkDefOutFolderName("Output"); +const TString AliRunDigitizer::fgkBaseInFolderName("Input"); + + +//_______________________________________________________________________ +AliRunDigitizer::AliRunDigitizer(): TTask("AliRunDigitizer","The manager for Merging"), + fkMASKSTEP(0), + fOutputFileName(0), + fOutputDirName(0), + fEvent(0), + fNrOfEventsToWrite(0), + fNrOfEventsWritten(0), + fCopyTreesFromInput(0), + fNinputs(0), + fNinputsGiven(0), + fInputStreams(0x0), + fOutRunLoader(0x0), + fOutputInitialized(kFALSE), + fCombi(0), + fCombination(0), + fCombinationFileName(0) { -// default ctor - if (nInputStreams == 0) { - Error("AliRunDigitizer","Specify nr of input streams"); + // + // root requires default ctor, where no new objects can be created + // do not use this ctor, it is supplied only for root needs +} + +//_______________________________________________________________________ +AliRunDigitizer::AliRunDigitizer(Int_t nInputStreams, Int_t sperb): + TTask("AliRunDigitizer","The manager for Merging"), + fkMASKSTEP(10000000), + fOutputFileName(""), + fOutputDirName("."), + fEvent(0), + fNrOfEventsToWrite(-1), + fNrOfEventsWritten(0), + fCopyTreesFromInput(-1), + fNinputs(nInputStreams), + fNinputsGiven(0), + fInputStreams(new TClonesArray("AliStream",nInputStreams)), + fOutRunLoader(0x0), + fOutputInitialized(kFALSE), + fCombi(new AliMergeCombi(nInputStreams,sperb)), + fCombination(MAXSTREAMSTOMERGE), + fCombinationFileName(0) + +{ + // + // ctor which should be used to create + // a manager for merging/digitization + // + if (nInputStreams == 0) + {//kidding + AliFatal("Specify nr of input streams"); return; - } + } Int_t i; - fNinputs = nInputStreams; - fOutputFileName = ""; - fOutputDirName = "."; - fCombination.Set(MAXSTREAMSTOMERGE); - for (i=0;i2) - cerr<<"AliRunDigitizer::AliRunDigitizer() called"<Add(digitizer); } -//////////////////////////////////////////////////////////////////////// -void AliRunDigitizer::SetInputStream(Int_t i, char *inputFile) +//_______________________________________________________________________ +void AliRunDigitizer::SetInputStream(Int_t i, const char *inputFile, TString foldername) { +// +// Sets the name of the input file +// if (i > fInputStreams->GetLast()) { - Error("SetInputStream","Input stream number too high"); + AliError("Input stream number too high"); return; } - static_cast(fInputStreams->At(i))->AddFile(inputFile); + AliStream * stream = static_cast(fInputStreams->At(i)) ; + if ( !foldername.IsNull() ) { + if ( i > 0 ) + foldername += i ; // foldername should stay unchanged for the default output + stream->SetFolderName(foldername) ; + } + stream->AddFile(inputFile); } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ void AliRunDigitizer::Digitize(Option_t* option) { -// get a new combination of inputs, connect input trees and loop -// over all digitizers +// get a new combination of inputs, loads events to folders - if (!InitGlobal()) { - cerr<<"False from InitGlobal"<(fInputStreams->At(0))->ImportgAlice()) { - cerr<<"gAlice object not found in the first file of " - <<"the 1st stream"<(fInputStreams->At(0))->ImportgAlice()) + { + AliError("Error occured while getting gAlice from Input 0"); + return; + } + + if (!InitGlobal()) //calls Init() for all (sub)digitizers + { + AliError("InitGlobal returned error"); + return; + } + Int_t eventsCreated = 0; // loop until there is anything on the input in case fNrOfEventsToWrite < 0 - while ((eventsCreated++ < fNrOfEventsToWrite) || (fNrOfEventsToWrite < 0)) { - if (!ConnectInputTrees()) break; - InitEvent(); -// loop over all registered digitizers and let them do the work - ExecuteTasks(""); - CleanTasks(); - FinishEvent(); - } + while ((eventsCreated++ < fNrOfEventsToWrite) || (fNrOfEventsToWrite < 0)) + { + if (!ConnectInputTrees()) break; + InitEvent();//this must be after call of Connect Input tress. + if (fOutRunLoader) + { + fOutRunLoader->SetEventNumber(eventsCreated-1); + } + static_cast(fInputStreams->At(0))->ImportgAlice(); // use gAlice of the first input stream + ExecuteTasks(option);// loop over all registered digitizers and let them do the work + FinishEvent(); + CleanTasks(); + } FinishGlobal(); } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ Bool_t AliRunDigitizer::ConnectInputTrees() { -// fill arrays fArrayTreeS, fArrayTreeH and fArrayTreeTPCS with -// pointers to the correct events according fCombination values -// null pointers can be in the output, AliDigitizer has to check it - - TTree *tree; - char treeName[50]; - Int_t serialNr; + // + // loads events + // Int_t eventNr[MAXSTREAMSTOMERGE], delta[MAXSTREAMSTOMERGE]; fCombi->Combination(eventNr, delta); - for (Int_t i=0;i(fInputStreams->At(i)); - if (!iStream->NextEventInStream(serialNr)) return kFALSE; - fInputFiles[i]=iStream->CurrentFile(); - sprintf(treeName,"TreeS%d",serialNr); - tree = static_cast(iStream->CurrentFile()->Get(treeName)); - fArrayTreeS[i] = tree; - sprintf(treeName,"TreeH%d",serialNr); - tree = static_cast(iStream->CurrentFile()->Get(treeName)); - fArrayTreeH[i] = tree; - sprintf(treeName,"TreeS_75x40_100x60_%d",serialNr); - tree = static_cast(iStream->CurrentFile()->Get(treeName)); - fArrayTreeTPCS[i] = tree; - sprintf(treeName,"TreeS%d_TRD",serialNr); - tree = static_cast(iStream->CurrentFile()->Get(treeName)); - fArrayTreeTRDS[i] = tree; - } else if (delta[i] != 0) { - Error("ConnectInputTrees","Only delta 0 or 1 is implemented"); + for (Int_t i=0;i(fInputStreams->At(i));//gets the "i" defined in combination + if (!iStream->NextEventInStream()) return kFALSE; //sets serial number + } + else if (delta[i] != 0) + { + AliError("Only delta 0 or 1 is implemented"); return kFALSE; - } - } + } + } + return kTRUE; } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ Bool_t AliRunDigitizer::InitGlobal() { -// called once before Digitize() is called, initialize digitizers and output - + // + // Method called once before Digitize() is called + // initialize digitizers and output + // + fOutputInitialized = kFALSE; TList* subTasks = this->GetListOfTasks(); if (subTasks) { - subTasks->ForEach(AliDigitizer,Init)(); - } + TIter next(subTasks); + while (AliDigitizer * dig = (AliDigitizer *) next()) + dig->Init(); + } return kTRUE; } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ void AliRunDigitizer::SetOutputFile(TString fn) -// the output will be to separate file, not to the signal file { + // + // The output will be to separate file, + // not to the signal file here should be protection + //to avoid setting the same file as any input + // + AliInfo(Form("Setting Output File Name %s ",fn.Data())); fOutputFileName = fn; - (static_cast(fInputStreams->At(0)))->ChangeMode("READ"); - InitOutputGlobal(); + // InitOutputGlobal(); } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ Bool_t AliRunDigitizer::InitOutputGlobal() { -// Creates the output file, called by InitEvent() - - TString fn; - fn = fOutputDirName + '/' + fOutputFileName; - fOutput = new TFile(fn,"recreate"); - if (GetDebug()>2) { - cerr<<"AliRunDigitizer::InitOutputGlobal(): file "<2) - cerr<<"AliRunDigitizer::InitEvent: fEvent = "<(fInputStreams->At(0)))->CurrentFile(); - } - fOutput->cd(); - char treeName[30]; - sprintf(treeName,"TreeD%d",fEvent); - fTreeD = static_cast(fOutput->Get(treeName)); - if (!fTreeD) { - fTreeD = new TTree(treeName,"Digits"); - fTreeD->Write(0,TObject::kOverwrite); - } - -// special tree for TPC - sprintf(treeName,"TreeD_75x40_100x60_%d",fEvent); - fTreeDTPC = static_cast(fOutput->Get(treeName)); - if (!fTreeDTPC) { - fTreeDTPC = new TTree(treeName,"TPC_Digits"); - fTreeDTPC->Write(0,TObject::kOverwrite); - } - -// special tree for TRD - sprintf(treeName,"TreeD%d_TRD",fEvent); - fTreeDTRD = static_cast(fOutput->Get(treeName)); - if (!fTreeDTRD) { - fTreeDTRD = new TTree(treeName,"TRD_Digits"); - fTreeDTRD->Write(0,TObject::kOverwrite); - } - + // + // redirects output properly + // + AliDebug(3,Form("fEvent = %d",fEvent)); + AliDebug(3,Form("fOutputFileName \"%s\"",fOutputFileName.Data())); + if (fOutputInitialized == kFALSE) InitOutputGlobal(); + + // if fOutputFileName was not given, write output to signal directory } +//_______________________________________________________________________ -//////////////////////////////////////////////////////////////////////// void AliRunDigitizer::FinishEvent() { -// called at the end of loop over digitizers - - fOutput->cd(); - if (fCopyTreesFromInput > -1) { - char treeName[20]; - Int_t i = fCopyTreesFromInput; - sprintf(treeName,"TreeK%d",fCombination[i]); - fInputFiles[i]->Get(treeName)->Clone()->Write(); - sprintf(treeName,"TreeH%d",fCombination[i]); - fInputFiles[i]->Get(treeName)->Clone()->Write(); - } + // + // called at the end of loop over digitizers + // + + if (GetOutRunLoader() == 0x0) + { + AliError("fOutRunLoader is null"); + return; + } + fEvent++; fNrOfEventsWritten++; - if (fTreeD) { - delete fTreeD; - fTreeD = 0; - } - if (fTreeDTPC) { - delete fTreeDTPC; - fTreeDTPC = 0; - } - if (fTreeDTRD) { - delete fTreeDTRD; - fTreeDTRD = 0; + + if (fOutRunLoader) + { + AliRunLoader* inrl = AliRunLoader::GetRunLoader(GetInputFolderName(0)); + AliHeader* outheader = fOutRunLoader->GetHeader(); + AliHeader* inheader = inrl->GetHeader(); + if (inheader == 0x0) + { + inrl->LoadHeader(); + inheader = inrl->GetHeader(); + if (inheader == 0x0) AliFatal("Can not get header from input 0"); + } + + outheader->SetNprimary(inheader->GetNprimary()); + outheader->SetNtrack(inheader->GetNtrack()); + outheader->SetEvent(inheader->GetEvent()); + outheader->SetEventNrInRun(inheader->GetEventNrInRun()); + outheader->SetNvertex(inheader->GetNvertex()); + fOutRunLoader->TreeE()->Fill(); } + + if (fCopyTreesFromInput > -1) + { + //this is sensless since no information would be coherent in case of merging + // + AliWarning("Copy trees from input: Copy or link files manually"); + return; + } } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ + void AliRunDigitizer::FinishGlobal() { -// called at the end of Exec -// save unique objects to the output file - - fOutput->cd(); - this->Write(); - if (fCopyTreesFromInput > -1) { - fInputFiles[fCopyTreesFromInput]->Get("TE")->Clone()->Write(); - gAlice->Write(); - } - fOutput->Close(); + // + // called at the end of Exec + // save unique objects to the output file + // + if (GetOutRunLoader() == 0x0) + { + AliError("Can not get RunLoader from Output Stream folder"); + return; + } + GetOutRunLoader()->CdGAFile(); + this->Write(NULL, TObject::kOverwrite); + if (fOutRunLoader) + { + fOutRunLoader->WriteHeader("OVERWRITE"); + fOutRunLoader->WriteRunLoader("OVERWRITE"); + TFolder* outfolder = fOutRunLoader->GetEventFolder(); + if (outfolder == 0x0) + { + AliError("Can not get Event Folder"); + return; + } + + AliRunLoader* inRN = AliRunLoader::GetRunLoader(GetInputFolderName(0)); + outfolder->Add(inRN->GetAliRun()); + fOutRunLoader->WriteAliRun("OVERWRITE"); + } + + if (fCopyTreesFromInput > -1) + { + //copy files manually + } } +//_______________________________________________________________________ - -//////////////////////////////////////////////////////////////////////// -Int_t AliRunDigitizer::GetNParticles(Int_t event) +Int_t AliRunDigitizer::GetNParticles(Int_t event) const { // return number of particles in all input files for a given // event (as numbered in the output file) @@ -392,9 +504,9 @@ Int_t AliRunDigitizer::GetNParticles(Int_t event) } return sum; } +//_______________________________________________________________________ -//////////////////////////////////////////////////////////////////////// -Int_t AliRunDigitizer::GetNParticles(Int_t event, Int_t input) +Int_t AliRunDigitizer::GetNParticles(Int_t /*event*/, Int_t /*input*/) const { // return number of particles in input file input for a given // event (as numbered in this input file) @@ -404,36 +516,10 @@ Int_t AliRunDigitizer::GetNParticles(Int_t event, Int_t input) return -1; -/* - TFile *file = ConnectInputFile(input); - if (!file) { - Error("GetNParticles","Cannot open input file"); - return -1; - } - -// find the header and get Nprimaries and Nsecondaries - TTree* tE = (TTree *)file->Get("TE") ; - if (!tE) { - Error("GetNParticles","input file does not contain TE"); - return -1; - } - AliHeader* header; - header = 0; - tE->SetBranchAddress("Header", &header); - if (!tE->GetEntry(event)) { - Error("GetNParticles","event %d not found",event); - return -1; - } - if (GetDebug()>2) { - cerr<<"Nprimary: "<< header->GetNprimary()<GetNsecondary()<GetNprimary() + header->GetNsecondary(); -*/ } -//////////////////////////////////////////////////////////////////////// -Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) +//_______________________________________________________________________ +Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) const { // return pointer to an int array with input event numbers which were // merged in the output event event @@ -445,8 +531,8 @@ Int_t* AliRunDigitizer::GetInputEventNumbers(Int_t event) } return a; } -//////////////////////////////////////////////////////////////////////// -Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t input) +//_______________________________________________________________________ +Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t /*input*/) const { // return an event number of an eventInput from input file input // which was merged to create output event event @@ -454,8 +540,8 @@ Int_t AliRunDigitizer::GetInputEventNumber(Int_t event, Int_t input) // simplified for now, implement later return event; } -//////////////////////////////////////////////////////////////////////// -TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) +//_______________________________________________________________________ +TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) const { // return pointer to particle with index i (index with mask) @@ -464,8 +550,8 @@ TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t event) return GetParticle(i,input,GetInputEventNumber(event,input)); } -//////////////////////////////////////////////////////////////////////// -TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t input, Int_t event) +//_______________________________________________________________________ +TParticle* AliRunDigitizer::GetParticle(Int_t /*i*/, Int_t /*input*/, Int_t /*event*/) const { // return pointer to particle with index i in the input file input // (index without mask) @@ -475,62 +561,69 @@ TParticle* AliRunDigitizer::GetParticle(Int_t i, Int_t input, Int_t event) // Must be revised in the version with AliStream return 0; -/* - TFile *file = ConnectInputFile(input); - if (!file) { - Error("GetParticle","Cannot open input file"); - return 0; - } - -// find the header and get Nprimaries and Nsecondaries - TTree* tE = (TTree *)file->Get("TE") ; - if (!tE) { - Error("GetParticle","input file does not contain TE"); - return 0; - } - AliHeader* header; - header = 0; - tE->SetBranchAddress("Header", &header); - if (!tE->GetEntry(event)) { - Error("GetParticle","event %d not found",event); - return 0; - } - -// connect TreeK - char treeName[30]; - sprintf(treeName,"TreeK%d",event); - TTree* tK = static_cast(file->Get(treeName)); - if (!tK) { - Error("GetParticle","input file does not contain TreeK%d",event); - return 0; - } - TParticle *particleBuffer; - particleBuffer = 0; - tK->SetBranchAddress("Particles", &particleBuffer); - - -// algorithmic way of getting entry index -// (primary particles are filled after secondaries) - Int_t entry; - if (iGetNprimary()) - entry = i+header->GetNsecondary(); - else - entry = i-header->GetNprimary(); - Int_t bytesRead = tK->GetEntry(entry); -// new ((*fParticles)[nentries]) TParticle(*fParticleBuffer); - if (bytesRead) - return particleBuffer; - return 0; -*/ } -//////////////////////////////////////////////////////////////////////// +//_______________________________________________________________________ void AliRunDigitizer::ExecuteTask(Option_t* option) { -// overwrite ExecuteTask to do Digitize only - + // + // overwrite ExecuteTask to do Digitize only + // if (!IsActive()) return; Digitize(option); fHasExecuted = kTRUE; return; } + +//_______________________________________________________________________ +const TString& AliRunDigitizer::GetInputFolderName(Int_t i) const +{ + // + // Get the input Folder Name + // + AliStream* stream = dynamic_cast(fInputStreams->At(i)); + if (stream == 0x0) + AliFatal(Form("Can not get the input stream. Index = %d. Exiting",i)); + return stream->GetFolderName(); +} +//_______________________________________________________________________ + +const char* AliRunDigitizer::GetOutputFolderName() +{ + // + // Get output folder name + // + return GetOutRunLoader()->GetEventFolder()->GetName(); +} +//_______________________________________________________________________ + +AliRunLoader* AliRunDigitizer::GetOutRunLoader() +{ + // + // Returns Run Loader + // + if (fOutRunLoader) return fOutRunLoader; + + if ( fOutputFileName.IsNull() ) + {//guard that sombody calls it without settting file name + AliDebug(1,"Output file name is empty. Using Input 0 for output"); + return AliRunLoader::GetRunLoader(GetInputFolderName(0)); + } +// InitOutputGlobal(); + return fOutRunLoader; +} +//_______________________________________________________________________ + +TString AliRunDigitizer::GetInputFileName(Int_t input, Int_t order) const +{ +// returns file name of the order-th file in the input stream input +// returns empty string if such file does not exist +// first input stream is 0 +// first file in the input stream is 0 + TString fileName(""); + if (input >= fNinputs) return fileName; + AliStream * stream = static_cast(fInputStreams->At(input)); + if (order > stream->GetNInputFiles()) return fileName; + fileName = stream->GetFileName(order); + return fileName; +}