Skip to content

Commit

Permalink
Refactoring of sources - making separate class for virtual sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielWielanek committed Dec 19, 2024
1 parent 7e3d0fc commit d8fbdf8
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 96 deletions.
2 changes: 2 additions & 0 deletions data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ io/IOManager.cxx
io/Reader.cxx
io/TriggerTask.cxx
io/InputDataInfo.cxx
io/VirtualSource.cxx
io/VirtualIOManager.cxx

)

Expand Down
2 changes: 2 additions & 0 deletions data/HalDataLinkDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
74 changes: 74 additions & 0 deletions data/io/VirtualIOManager.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* VirtualIOManager2.cxx
*
* Created on: 19 gru 2024
* Author: daniel
*/

#include "VirtualIOManager.h"

#include <TFile.h>
#include <TTree.h>

#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 */
55 changes: 55 additions & 0 deletions data/io/VirtualIOManager.h
Original file line number Diff line number Diff line change
@@ -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 <RtypesCore.h>
#include <TString.h>

#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 /* */
30 changes: 30 additions & 0 deletions data/io/VirtualSource.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* VirtualRootSource.cxx
*
* Created on: 19 gru 2024
* Author: daniel
*/

#include "VirtualSource.h"

#include <TFile.h>
#include <TTree.h>

#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 */
51 changes: 51 additions & 0 deletions data/io/VirtualSource.h
Original file line number Diff line number Diff line change
@@ -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 <RtypesCore.h>
#include <TString.h>
#include <vector>

#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_ */
56 changes: 3 additions & 53 deletions examples/onthefly/OTFIOManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Cout.h"
#include "InputDataInfo.h"
#include "VirtualSource.h"

#include "OTFSource.h"

Expand All @@ -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
30 changes: 5 additions & 25 deletions examples/onthefly/OTFIOManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,21 @@
#define HAL_EXAMPLES_ONTHEFLY_OTFIOMANAGER_H_

#include "IOManager.h"
#include "VirtualIOManager.h"

#include <vector>

#include <RtypesCore.h>
#include <TString.h>

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)
};
Expand Down
19 changes: 9 additions & 10 deletions examples/onthefly/OTFSource.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -34,19 +32,20 @@ namespace HalOTF {
}
return kTRUE;
}
void Source::GetEvent() {

void Source::GetEvent(Int_t /*i*/, Int_t /*flag*/) {
fRecoEvent->Clear();
fMcEvent->Clear();
for (auto& gen : fGenerators)
gen->GenerateEvent();
++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);
}
Expand Down
Loading

0 comments on commit d8fbdf8

Please sign in to comment.