From 2ccfce5aa80e02f6d6e9a5f38c463ccdcf88c0b4 Mon Sep 17 00:00:00 2001 From: 2xB <2xB@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:04:41 +0100 Subject: [PATCH] KSWriteASCII: Improve code structure --- Kassiopeia/Writers/Include/KSWriteASCII.h | 10 +++++++++- Kassiopeia/Writers/Source/KSWriteASCII.cxx | 23 +++++++++------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Kassiopeia/Writers/Include/KSWriteASCII.h b/Kassiopeia/Writers/Include/KSWriteASCII.h index 3d78c4e0..b47340a3 100644 --- a/Kassiopeia/Writers/Include/KSWriteASCII.h +++ b/Kassiopeia/Writers/Include/KSWriteASCII.h @@ -20,9 +20,10 @@ class KSWriteASCII : public KSComponentTemplate ~Data(); void Start(const unsigned int& anIndex); - void Fill(); + std::string ValuesAsString(); void MakeTitle(KSComponent* aComponent, int aTrack); + private: std::string fLabel; std::string fType; @@ -65,6 +66,7 @@ class KSWriteASCII : public KSComponentTemplate void SetPrecision(const unsigned int& aValue); katrin::KTextFile* TextFile(); + void Write(std::string str); int Precision() const; protected: @@ -163,6 +165,12 @@ inline katrin::KTextFile* KSWriteASCII::TextFile() { return fTextFile; } + +inline void KSWriteASCII::Write(std::string str) +{ + for (char& it : str) + fTextFile->File()->put(it); +} inline int KSWriteASCII::Precision() const { diff --git a/Kassiopeia/Writers/Source/KSWriteASCII.cxx b/Kassiopeia/Writers/Source/KSWriteASCII.cxx index d36eefa3..f08dd4e7 100644 --- a/Kassiopeia/Writers/Source/KSWriteASCII.cxx +++ b/Kassiopeia/Writers/Source/KSWriteASCII.cxx @@ -106,24 +106,19 @@ void KSWriteASCII::Data::Start(const unsigned int& anIndex) return; } -void KSWriteASCII::Data::Fill() +std::string KSWriteASCII::Data::ValuesAsString() { KSComponent* tComponent; - OutputObjectASCII* tOutputObjectASCII; vector::iterator tIt; + string result; for (tIt = fComponents.begin(); tIt != fComponents.end(); ++tIt) { tComponent = (*tIt); tComponent->PullUpdate(); } - string str; - for (auto& outputObjectASCII : fOutputObjectASCIIs) { - tOutputObjectASCII = outputObjectASCII; - str = tOutputObjectASCII->getValue(); - - for (char& it : str) - fWriter->TextFile()->File()->put(it); + for (OutputObjectASCII*& outputObjectASCII : fOutputObjectASCIIs) { + result += outputObjectASCII->getValue(); } for (tIt = fComponents.begin(); tIt != fComponents.end(); ++tIt) { @@ -132,7 +127,7 @@ void KSWriteASCII::Data::Fill() } fLength++; - return; + return result; } void KSWriteASCII::Data::MakeTitle(KSComponent* aComponent, int aTrack) @@ -520,7 +515,7 @@ void KSWriteASCII::ExecuteRun() fRunLastStepIndex = fStepIndex - 1; for (auto& activeRunComponent : fActiveRunComponents) - activeRunComponent.second->Fill(); + Write(activeRunComponent.second->ValuesAsString()); fRunIndex++; fRunFirstEventIndex = fEventIndex; @@ -541,7 +536,7 @@ void KSWriteASCII::ExecuteEvent() fEventLastStepIndex = fStepIndex - 1; for (auto& activeEventComponent : fActiveEventComponents) - activeEventComponent.second->Fill(); + Write(activeEventComponent.second->ValuesAsString()); fEventIndex++; fEventFirstTrackIndex = fTrackIndex; @@ -558,7 +553,7 @@ void KSWriteASCII::ExecuteTrack() fTextFile->File()->put('\n'); for (auto& activeTrackComponent : fActiveTrackComponents) - activeTrackComponent.second->Fill(); + Write(activeTrackComponent.second->ValuesAsString()); wtrmsg(eNormal) << "ASCII output was written to file <" << fTextFile->GetName() << ">" << eom; fTextFile->Close(); @@ -603,7 +598,7 @@ void KSWriteASCII::ExecuteStep() wtrmsg_debug("ASCII writer <" << GetName() << "> is filling a step" << eom); for (auto& activeStepComponent : fActiveStepComponents) - activeStepComponent.second->Fill(); + Write(activeStepComponent.second->ValuesAsString()); } fStepIndex++;