diff --git a/data/CMakeLists.txt b/data/CMakeLists.txt index b0b2b5c..0b429ed 100644 --- a/data/CMakeLists.txt +++ b/data/CMakeLists.txt @@ -64,6 +64,8 @@ io/IOManager.cxx io/Reader.cxx io/TriggerTask.cxx io/InputDataInfo.cxx +io/VirtualSource.cxx +io/VirtualIOManager.cxx ) diff --git a/data/HalDataLinkDef.h b/data/HalDataLinkDef.h index 99adb45..d0aa85d 100644 --- a/data/HalDataLinkDef.h +++ b/data/HalDataLinkDef.h @@ -68,6 +68,8 @@ #pragma link C++ class Hal::BranchInfo + ; #pragma link C++ class Hal::InputDataInfo + ; #pragma link C++ class Hal::InputRootDataInfo + ; +#pragma link C++ class Hal::VirtualSource + ; +#pragma link C++ class Hal::VirtualIOManager + ; #pragma link C++ class std::vector < Hal::BranchInfo> + ; #pragma link C++ class std::vector < Hal::TriggerTask> + ; #endif diff --git a/data/io/VirtualIOManager.cxx b/data/io/VirtualIOManager.cxx new file mode 100644 index 0000000..6d1037e --- /dev/null +++ b/data/io/VirtualIOManager.cxx @@ -0,0 +1,74 @@ +/* + * VirtualIOManager2.cxx + * + * Created on: 19 gru 2024 + * Author: daniel + */ + +#include "VirtualIOManager.h" + +#include +#include + +#include "Cout.h" +#include "InputDataInfo.h" +#include "Std.h" +#include "VirtualSource.h" + +namespace Hal { + VirtualIOManager::VirtualIOManager(VirtualSource* source, Int_t events) : + Hal::IOManager(new Hal::InputDataInfo("/dev/null")), + fInFileName("/dev/null"), + fOutTreeName("HalTree"), + fEntries(events), + fInFile(nullptr), + fOutFile(nullptr), + fOutTree(nullptr), + fSource(source) {} + + Bool_t VirtualIOManager::InitInternal() { + Hal::Cout::PrintInfo("IO manager internal init", Hal::EInfo::kDebugInfo); + Hal::Cout::PrintInfo(fInFileName, Hal::EInfo::kDebugInfo); + fInFile = new TFile(fInFileName, "recreate"); + fOutFile = new TFile(fOutFileName, "recreate"); + fOutTree = new TTree(fOutTreeName, fOutTreeName); + Hal::Cout::PrintInfo(Form("Creating output tree %s", fOutTreeName.Data()), Hal::EInfo::kDebugInfo); + fSource->RegisterInputs(); + return kTRUE; + } + + Int_t VirtualIOManager::GetEntries() const { return fEntries; } + + void VirtualIOManager::RegisterInternal(const char* name, const char* folderName, TNamed* obj, Bool_t toFile) { + if (toFile) { fOutTree->Branch(name, obj); } + } + + void VirtualIOManager::RegisterInternal(const char* name, const char* Foldername, TCollection* obj, Bool_t toFile) { + if (toFile) { fOutTree->Branch(name, obj); } + } + + Int_t VirtualIOManager::GetEntry(Int_t i, Int_t flag) { + if (i < fEntries) { + if (fSource) fSource->GetEvent(i, flag); + return 1; + } + return 0; + } + + TFile* VirtualIOManager::GetInFile() { return fInFile; } + + void VirtualIOManager::SetInChain(TChain* /*tempChain*/, Int_t /*ident*/) {} + + void VirtualIOManager::FillTree() { fOutTree->Fill(); } + + void VirtualIOManager::CloseManager() { + fOutTree->Write(); + fOutFile->Close(); + } + + VirtualIOManager::~VirtualIOManager() { + if (fOutFile) delete fOutFile; + if (fInFile) delete fInFile; + } + +} /* namespace Hal */ diff --git a/data/io/VirtualIOManager.h b/data/io/VirtualIOManager.h new file mode 100644 index 0000000..2248bfa --- /dev/null +++ b/data/io/VirtualIOManager.h @@ -0,0 +1,55 @@ +/* + * VirtualIOManager2.h + * + * Created on: 19 gru 2024 + * Author: daniel + */ + +#ifndef HAL_DATA_IO_VIRTUALIOMANAGER_H_ +#define HAL_DATA_IO_VIRTUALIOMANAGER_H_ + +#include +#include + +#include "IOManager.h" + +class TTree; +namespace Hal { + class VirtualSource; +} /* namespace Hal */ + +namespace Hal { + + class VirtualIOManager : public IOManager { + TString fInFileName; + TString fOutFileName; + TString fOutTreeName; + Int_t fEntries; + TFile* fInFile; + TFile* fOutFile; + TTree* fOutTree; + VirtualSource* fSource; + + protected: + virtual void RegisterInternal(const char* name, const char* folderName, TNamed* obj, Bool_t toFile); + virtual void RegisterInternal(const char* name, const char* Foldername, TCollection* obj, Bool_t toFile); + virtual Bool_t InitInternal(); + + public: + VirtualIOManager(VirtualSource* source = nullptr, Int_t events = 10000); + void SetOutput(TString name) { fOutFileName = name; } + void SetOutTreeName(TString name) { fOutTreeName = name; } + Int_t GetEntries() const; + virtual Int_t GetEntry(Int_t i, Int_t flag); + virtual TFile* GetInFile(); + virtual void AddFriend(TString /*name*/) {}; + virtual void SetInChain(TChain* tempChain, Int_t ident = -1); + virtual void FillTree(); + virtual void CloseManager(); + virtual ~VirtualIOManager(); + ClassDef(VirtualIOManager, 1) + }; + +} /* namespace Hal */ + +#endif /* */ diff --git a/data/io/VirtualSource.cxx b/data/io/VirtualSource.cxx new file mode 100644 index 0000000..33dc1f7 --- /dev/null +++ b/data/io/VirtualSource.cxx @@ -0,0 +1,30 @@ +/* + * VirtualRootSource.cxx + * + * Created on: 19 gru 2024 + * Author: daniel + */ + +#include "VirtualSource.h" + +#include +#include + +#include "Cout.h" +#include "InputDataInfo.h" +#include "Std.h" +#include "VirtualIOManager.h" + +namespace Hal { + + VirtualSource::VirtualSource(Int_t nevents) : Hal::Source("/dev/null"), fEvents(nevents) { + fManager = new VirtualIOManager(this, nevents); + } + + VirtualSource::VirtualSource(IOManager* mngr, Int_t nevents) : Source(mngr), fEvents(nevents) {} + + Bool_t VirtualSource::Init() { return kTRUE; } + + void VirtualSource::GetEvent(Int_t /*i*/, Int_t /*flag*/) { ++fEvents; } + +} /* namespace Hal */ diff --git a/data/io/VirtualSource.h b/data/io/VirtualSource.h new file mode 100644 index 0000000..675a6bc --- /dev/null +++ b/data/io/VirtualSource.h @@ -0,0 +1,51 @@ +/* + * VirtualRootSource.h + * + * Created on: 19 gru 2024 + * Author: daniel + */ + +#ifndef HAL_DATA_IO_VIRTUALSOURCE_H_ +#define HAL_DATA_IO_VIRTUALSOURCE_H_ + +#include +#include +#include + +#include "IOManager.h" +#include "Source.h" + +class TTree; + +namespace Hal { + class VirtualIOManager; + /** + * class that represents source without a physical input (e.g. for generation data on the fly) + */ + class VirtualSource : public Source { + friend class VirtualIOManager; + + protected: + Int_t fEvents = {0}; + Bool_t fRegister = {kFALSE}; + /** + * register input data and sent pointers to generators, this cannot be done until Init method + * because data manager does not exist yet + */ + virtual void RegisterInputs() {}; + VirtualSource(IOManager* mngr, Int_t nevents = 1000); + + public: + VirtualSource(Int_t nevents = 1000); + virtual Bool_t Init(); + /** + * register generated data in output + */ + virtual void Register() { fRegister = kTRUE; } + virtual void GetEvent(Int_t i, Int_t flag); + virtual ~VirtualSource() {}; + ClassDef(VirtualSource, 1) + }; +} /* namespace Hal */ + +#endif /* HAL_DATA_IO_VIRTUALSOURCE_H_ */ diff --git a/examples/onthefly/OTFIOManager.cxx b/examples/onthefly/OTFIOManager.cxx index d624809..c23403f 100644 --- a/examples/onthefly/OTFIOManager.cxx +++ b/examples/onthefly/OTFIOManager.cxx @@ -10,6 +10,7 @@ #include "Cout.h" #include "InputDataInfo.h" +#include "VirtualSource.h" #include "OTFSource.h" @@ -24,59 +25,8 @@ namespace HalOTF { - IOManager::IOManager(TString name, HalOTF::Source* source, Int_t entries) : - Hal::IOManager(new Hal::InputDataInfo(name)), - fInFileName(name), - fOutTreeName("HalTree"), - fEntries(entries), - fInFile(nullptr), - fOutFile(nullptr), - fOutTree(nullptr), - fSource(source) {} + IOManager::IOManager(HalOTF::Source* source, Int_t entries) : Hal::VirtualIOManager(source, entries) {} - Bool_t IOManager::InitInternal() { - Hal::Cout::PrintInfo(fInFileName, Hal::EInfo::kLowWarning); - fInFile = new TFile(fInFileName, "recreate"); - fOutFile = new TFile(fOutFileName, "recreate"); - fOutTree = new TTree(fOutTreeName, fOutTreeName); - Hal::Cout::PrintInfo(Form("CREATING TREE %s", fOutTreeName.Data()), Hal::EInfo::kError); - fSource->RegisterOutputs(this); - return kTRUE; - } - - Int_t IOManager::GetEntries() const { return fEntries; } - - IOManager::~IOManager() { - if (fInFile) delete fInFile; - if (fOutFile) delete fOutFile; - gSystem->Exec(Form("rm %s", fInFileName.Data())); - } - - TFile* IOManager::GetInFile() { return fInFile; } - - void IOManager::RegisterInternal(const char* name, const char* /*folderName*/, TNamed* obj, Bool_t toFile) { - if (toFile) { fOutTree->Branch(name, obj); } - } - - void IOManager::RegisterInternal(const char* name, const char* /*Foldername*/, TCollection* obj, Bool_t toFile) { - if (toFile) { fOutTree->Branch(name, obj); } - } - - void IOManager::SetInChain(TChain* /*tempChain*/, Int_t /*ident*/) {} - - Int_t IOManager::GetEntry(Int_t i, Int_t /*flag*/) { - if (i < fEntries) { - if (fSource) fSource->GetEvent(); - return 1; - } - return -1; - } - - void IOManager::FillTree() { fOutTree->Fill(); } - - void IOManager::CloseManager() { - fOutTree->Write(); - fOutFile->Close(); - } + IOManager::~IOManager() {} } // namespace HalOTF diff --git a/examples/onthefly/OTFIOManager.h b/examples/onthefly/OTFIOManager.h index 0a4f052..7bf9b0b 100644 --- a/examples/onthefly/OTFIOManager.h +++ b/examples/onthefly/OTFIOManager.h @@ -10,6 +10,8 @@ #define HAL_EXAMPLES_ONTHEFLY_OTFIOMANAGER_H_ #include "IOManager.h" +#include "VirtualIOManager.h" + #include #include @@ -17,34 +19,12 @@ class TTree; class TBranch; + namespace HalOTF { class Source; - class IOManager : public Hal::IOManager { - TString fInFileName; - TString fOutFileName; - TString fOutTreeName; - Int_t fEntries; - TFile* fInFile; - TFile* fOutFile; - TTree* fOutTree; - HalOTF::Source* fSource; - - protected: - void RegisterInternal(const char* name, const char* folderName, TNamed* obj, Bool_t toFile); - void RegisterInternal(const char* name, const char* Foldername, TCollection* obj, Bool_t toFile); - Bool_t InitInternal(); - + class IOManager : public Hal::VirtualIOManager { public: - IOManager(TString name = "root_virtual.root", HalOTF::Source* source = nullptr, Int_t entries = 1); - void SetOutput(TString name) { fOutFileName = name; } - void SetOutTreeName(TString name) { fOutTreeName = name; } - Int_t GetEntries() const; - Int_t GetEntry(Int_t i, Int_t flag); - TFile* GetInFile(); - void AddFriend(TString /*name*/) {}; - void SetInChain(TChain* tempChain, Int_t ident = -1); - void FillTree(); - virtual void CloseManager(); + IOManager(HalOTF::Source* source = nullptr, Int_t entries = 1); virtual ~IOManager(); ClassDef(IOManager, 1) }; diff --git a/examples/onthefly/OTFSource.cxx b/examples/onthefly/OTFSource.cxx index 240284b..22e5b6e 100644 --- a/examples/onthefly/OTFSource.cxx +++ b/examples/onthefly/OTFSource.cxx @@ -9,20 +9,18 @@ #include "OTFSource.h" +#include "Cout.h" #include "DataManager.h" #include "OTFData.h" #include "OTFEventGenerator.h" #include "OTFIOManager.h" +#include "VirtualIOManager.h" +#include "VirtualSource.h" namespace HalOTF { - Source::Source(Int_t events) : Hal::Source("root_virtual.root"), fEvents(events) { - fManager = new HalOTF::IOManager("root_virtual.root", this, fEvents); - } - - Hal::IOManager* Source::GetIOManager() const { return fManager; } + Source::Source(Int_t events) : Hal::VirtualSource(new HalOTF::IOManager(this, events), events) {} Source::~Source() { - if (fManager) delete fManager; if (fMcEvent) delete fMcEvent; if (fRecoEvent) delete fRecoEvent; } @@ -34,7 +32,8 @@ namespace HalOTF { } return kTRUE; } - void Source::GetEvent() { + + void Source::GetEvent(Int_t /*i*/, Int_t /*flag*/) { fRecoEvent->Clear(); fMcEvent->Clear(); for (auto& gen : fGenerators) @@ -42,11 +41,11 @@ namespace HalOTF { ++fEvents; } - void Source::RegisterOutputs(HalOTF::IOManager* mngr) { + void Source::RegisterInputs() { fMcEvent = new OTF::McEvent(); fRecoEvent = new OTF::RecoEvent(); - mngr->Register("OTF::McEvent.", "HalEvents", fMcEvent, fRegister); - mngr->Register("OTF::RecoEvent.", "HalEvents", fRecoEvent, fRegister); + fManager->Register("OTF::McEvent.", "HalEvents", fMcEvent, fRegister); + fManager->Register("OTF::RecoEvent.", "HalEvents", fRecoEvent, fRegister); for (auto& gen : fGenerators) gen->SetEvents(fMcEvent, fRecoEvent); } diff --git a/examples/onthefly/OTFSource.h b/examples/onthefly/OTFSource.h index 8ab46be..13b619d 100644 --- a/examples/onthefly/OTFSource.h +++ b/examples/onthefly/OTFSource.h @@ -14,10 +14,12 @@ #include #include "Source.h" +#include "VirtualSource.h" namespace Hal { class IOManager; -} + class VirtualIOManager; +} // namespace Hal namespace HalOTF { class EventGenerator; class IOManager; @@ -28,10 +30,9 @@ namespace OTF { } // namespace OTF namespace HalOTF { - class Source : public Hal::Source { + class Source : public Hal::VirtualSource { friend class HalOTF::IOManager; - Int_t fEvents = {0}; - Bool_t fRegister = {kFALSE}; + friend class Hal::VirtualIOManager; std::vector fGenerators; OTF::McEvent* fMcEvent = {nullptr}; OTF::RecoEvent* fRecoEvent = {nullptr}; @@ -41,15 +42,13 @@ namespace HalOTF { * register output data and sent pointers to generators, this cannot be done ini Init method * because data manager does not exist yet */ - void RegisterOutputs(HalOTF::IOManager* mngr); + virtual void RegisterInputs(); public: Source(Int_t entries = 0); - virtual Hal::IOManager* GetIOManager() const; void AddEventGenerator(HalOTF::EventGenerator* evgen) { fGenerators.push_back(evgen); } Bool_t Init(); - void Register() { fRegister = kTRUE; } - void GetEvent(); + void GetEvent(Int_t i, Int_t flag); virtual ~Source(); ClassDef(Source, 1) };