diff --git a/examples/programs/D3piTest.cxx b/examples/programs/D3piTest.cxx index f48dbc30..af04623d 100644 --- a/examples/programs/D3piTest.cxx +++ b/examples/programs/D3piTest.cxx @@ -114,11 +114,10 @@ int main( int argc, char** argv) // std::cout << "\nHelicity angle symmetrizations with " << M.helicityAngles()->maxSymmetrizationIndex() + 1 << " indices \n"; - D->printDecayChain(); - std::cout << "\n"; + LOG(INFO) << D->decayChainAsString(); - std::cout << *M.spinAmplitudeCache() << std::endl; - M.printDataAccessors(false); + LOG(INFO) << *M.spinAmplitudeCache() << std::endl; + LOG(INFO) << data_accessors_as_string(M, false); // get default Dalitz axes auto massAxes = M.massAxes(); @@ -136,8 +135,7 @@ int main( int argc, char** argv) data.push_back(P); } - LOG(INFO) << "AFTER"; - M.fourMomenta()->printMasses(data[0]); + LOG(INFO) << masses_as_string(*M.fourMomenta(), data[0]); for (auto p : M.fourMomenta()->finalStateMomenta(data[0])) LOG(INFO) << p; @@ -174,7 +172,7 @@ int main( int argc, char** argv) [](const std::string & s, const std::complex& c) { return s + "\t" + std::to_string(real(c)) + " + " + std::to_string(imag(c));}).erase(0, 1); - LOG(INFO) << M.initialStateParticle()->printDecayTrees(); + LOG(INFO) << to_string(M.initialStateParticle()->decayTrees()); LOG(INFO) << "alright!"; } diff --git a/examples/programs/D4piTest.cxx b/examples/programs/D4piTest.cxx index 0b8f6db3..d7c42741 100644 --- a/examples/programs/D4piTest.cxx +++ b/examples/programs/D4piTest.cxx @@ -3,6 +3,7 @@ #include "DataPoint.h" #include "DataSet.h" #include "DecayChannel.h" +#include "DecayTree.h" #include "FinalStateParticle.h" #include "FourMomenta.h" #include "FourVector.h" @@ -112,11 +113,10 @@ int main( int argc, char** argv) for (auto& pc_i : M.helicityAngles()->symmetrizationIndices()) std::cout << *pc_i.first << ": " << pc_i.second << "\n"; - D->printDecayChain(); - std::cout << "\n"; + LOG(INFO) << D->decayChainAsString(); - std::cout << *M.spinAmplitudeCache() << std::endl; - M.printDataAccessors(false); + LOG(INFO) << *M.spinAmplitudeCache() << std::endl; + LOG(INFO) << data_accessors_as_string(M, false); LOG(INFO) << "create dataPoints"; @@ -231,7 +231,7 @@ int main( int argc, char** argv) */ - LOG(INFO) << D->printDecayTrees(); + LOG(INFO) << to_string(M.initialStateParticle()->decayTrees()); std::cout << "alright! \n"; } diff --git a/examples/programs/DKKpiTest.cxx b/examples/programs/DKKpiTest.cxx index cbba8b46..d31a21ff 100644 --- a/examples/programs/DKKpiTest.cxx +++ b/examples/programs/DKKpiTest.cxx @@ -64,11 +64,10 @@ int main( int argc, char** argv) std::cout << "\nHelicity angle symmetrizations with " << M.helicityAngles()->nSymmetrizationIndices() << " indices \n"; - D->printDecayChain(); - std::cout << "\n"; + LOG(INFO) << D->decayChainAsString(); - std::cout << *M.spinAmplitudeCache() << std::endl; - M.printDataAccessors(false); + LOG(INFO) << *M.spinAmplitudeCache() << std::endl; + LOG(INFO) << data_accessors_as_string(M, false); // choose default Dalitz coordinates const yap::MassAxes massAxes = M.massAxes(); @@ -79,7 +78,7 @@ int main( int argc, char** argv) auto data = M.createDataSet(1); DEBUG("BEFORE"); - M.fourMomenta()->printMasses(data[0]); + DEBUG(masses_as_string(*M.fourMomenta(), data[0])); LOG(INFO) << "setting squared mass ..."; auto P = M.calculateFourMomenta(massAxes, m2, D); @@ -91,7 +90,7 @@ int main( int argc, char** argv) } DEBUG("AFTER"); - M.fourMomenta()->printMasses(data[0]); + DEBUG(masses_as_string(*M.fourMomenta(), data[0])); std::cout << "alright! \n"; } diff --git a/include/CachedDataValue.h b/include/CachedDataValue.h index aa7fe9c9..e4c13ce4 100644 --- a/include/CachedDataValue.h +++ b/include/CachedDataValue.h @@ -185,8 +185,12 @@ inline bool operator==(const CachedDataValue::Status& S, const VariableStatus& s inline bool operator!=(const CachedDataValue::Status& S, const VariableStatus& s) { return S.Variable != s; } +/// to_string for CachedDataValue::Status +std::string to_string(const CachedDataValue::Status& S); + /// streaming operator for CachedDataValue::Status -std::ostream& operator<<(std::ostream& str, const CachedDataValue::Status& S); +inline std::ostream& operator<<(std::ostream& str, const CachedDataValue::Status& S) +{ return str << to_string(S); } /// \class RealCachedDataValue /// \brief Class for managing a single real cached value inside a #DataPoint diff --git a/include/CalculationStatus.h b/include/CalculationStatus.h index ea414dde..ae456273 100644 --- a/include/CalculationStatus.h +++ b/include/CalculationStatus.h @@ -31,18 +31,21 @@ enum class CalculationStatus : bool { uncalculated = false }; -inline std::ostream& operator<<(std::ostream& str, const CalculationStatus& c) +inline std::string to_string(const CalculationStatus& c) { switch (c) { case CalculationStatus::calculated: - return str << "calculated"; + return "calculated"; case CalculationStatus::uncalculated: - return str << "uncalculated"; + return "uncalculated"; default: - return str << (int) c; + return std::to_string(static_cast(c)); } } +inline std::ostream& operator<<(std::ostream& str, const CalculationStatus& c) +{ return str << to_string(c); } + } #endif diff --git a/include/DataAccessor.h b/include/DataAccessor.h index fc65d36f..3769138a 100644 --- a/include/DataAccessor.h +++ b/include/DataAccessor.h @@ -65,9 +65,6 @@ class DataAccessor const unsigned nSymmetrizationIndices() const { return NIndices_; } - /// print ParticleCombination map - void printParticleCombinations() const; - /// \return CachedDataValueSet const CachedDataValueSet& cachedDataValues() const { return CachedDataValues_; } diff --git a/include/DecayTree.h b/include/DecayTree.h index 0890b388..3c54491f 100644 --- a/include/DecayTree.h +++ b/include/DecayTree.h @@ -162,6 +162,9 @@ FreeAmplitudeSet freeAmplitudes(const DecayTreeVector& DTV); /// \param vector of trees to check in const DecayTreeVector select_changed(const DecayTreeVector& dtv); +/// \return map of spin projections to decayTrees as string +std::string to_string(const std::map& decayTrees); + } #endif diff --git a/include/DecayingParticle.h b/include/DecayingParticle.h index 6a7cf984..634d993f 100644 --- a/include/DecayingParticle.h +++ b/include/DecayingParticle.h @@ -130,15 +130,13 @@ class DecayingParticle : public Particle /// @} - /// Print complete decay chain - void printDecayChain() const - { printDecayChainLevel(0); } + /// \return complete decay chain as (multiline) string + std::string decayChainAsString() const + { return decayChainLevelAsString(0); } /// \return raw pointer to Model through first DecayChannel const Model* model() const override; - std::string printDecayTrees() const; - /// grant friend status to DecayChannel to call fixSolitaryFreeAmplitudes() /// and storeBlattWeisskopf() friend DecayChannel; @@ -165,7 +163,7 @@ class DecayingParticle : public Particle /// if only one decay channel is available, fix its free amplitude to the current value void fixSolitaryFreeAmplitudes(); - void printDecayChainLevel(int level) const; + std::string decayChainLevelAsString(int level) const; /// modify a DecayTree /// \param dt DecayTree to modify diff --git a/include/FourMomenta.h b/include/FourMomenta.h index b89f7e03..fced8e07 100644 --- a/include/FourMomenta.h +++ b/include/FourMomenta.h @@ -101,9 +101,6 @@ class FourMomenta : public StaticDataAccessor /// @} - /// print all masses - std::ostream& printMasses(const DataPoint& d, std::ostream& os = std::cout) const; - /// grant friend status to Model to call addParticleCombination and setFinalStateMomenta friend class Model; @@ -138,6 +135,9 @@ class FourMomenta : public StaticDataAccessor }; +/// \return all masses as (multiline) string +std::string masses_as_string(const FourMomenta& fm, const DataPoint& d); + } #endif diff --git a/include/Model.h b/include/Model.h index 902f6f3b..b9b75032 100644 --- a/include/Model.h +++ b/include/Model.h @@ -209,12 +209,6 @@ class Model /// Set VariableStatus'es of all Parameter's to unchanged, or leave as fixed void setParameterFlagsToUnchanged(); - /// Print the list of DataAccessor's - void printDataAccessors(bool printParticleCombinations = true) const; - - /// Print all VariableStatus'es and CalculationStatus'es - void printFlags(const StatusManager& sm) const; - /// grant friend status to DataAccessor to register itself with this friend class DataAccessor; @@ -294,6 +288,14 @@ const double sumOfLogsOfSquaredAmplitudes(const Model& M, DataPartition& D); /// \param DP DataPartitionVector of partitions to use const double sumOfLogsOfSquaredAmplitudes(const Model& M, DataPartitionVector& DP); +/// \return the list of DataAccessor's as (multiline) string +/// \param m Model to print DataAccessor's of +/// \param printParticleCombinations also print ParticleCombination's of each DataAccessor +std::string data_accessors_as_string(const Model& m, bool printParticleCombinations = true); + +/// \return all VariableStatus'es and CalculationStatus'es as (multiline) string +std::string flags_as_string(const Model& m, const StatusManager& sm); + } #endif diff --git a/include/ParticleCombination.h b/include/ParticleCombination.h index 9ee4cd16..2081f625 100644 --- a/include/ParticleCombination.h +++ b/include/ParticleCombination.h @@ -107,7 +107,7 @@ class ParticleCombination : public std::enable_shared_from_this +inline std::string to_string(const ParticleCombinationMap& pcm) +{ + std::string s; + using std::to_string; + for (auto& kv : pcm) + s += to_string(kv.second) + " : " + to_string_with_parent(*(kv.first)); + return s; +} + /// streamer inline std::ostream& operator<<(std::ostream& os, const ParticleCombination& PC) { os << to_string(PC); return os; } diff --git a/include/VariableStatus.h b/include/VariableStatus.h index aec4a65b..e077b0e4 100644 --- a/include/VariableStatus.h +++ b/include/VariableStatus.h @@ -30,20 +30,23 @@ enum class VariableStatus : int { unchanged = +1, ///< Parameter is free but has not been changed }; -inline std::ostream& operator<<(std::ostream& str, const VariableStatus& s) +inline std::string to_string(const VariableStatus& s) { switch (s) { case VariableStatus::changed: - return str << "changed"; + return "changed"; case VariableStatus::fixed: - return str << "fixed"; + return "fixed"; case VariableStatus::unchanged: - return str << "unchanged"; + return "unchanged"; default: - return str << (int) s; + return std::to_string(static_cast(s)); } } +inline std::ostream& operator<<(std::ostream& str, const VariableStatus& c) +{ return str << to_string(c); } + } #endif diff --git a/src/CachedDataValue.cxx b/src/CachedDataValue.cxx index aea1f0b1..56875406 100644 --- a/src/CachedDataValue.cxx +++ b/src/CachedDataValue.cxx @@ -25,9 +25,9 @@ CachedDataValue::Status& CachedDataValue::Status::operator=(const VariableStatus } //------------------------- -std::ostream& operator<<(std::ostream& str, const CachedDataValue::Status& S) +std::string to_string(const CachedDataValue::Status& S) { - return str << S.Calculation << ", " << S.Variable; + return to_string(S.Calculation) + ", " + to_string(S.Variable); } //------------------------- diff --git a/src/DataAccessor.cxx b/src/DataAccessor.cxx index 69484089..6a5eeee9 100644 --- a/src/DataAccessor.cxx +++ b/src/DataAccessor.cxx @@ -15,13 +15,6 @@ DataAccessor::DataAccessor(const ParticleCombination::Equal& equal) : { } -//------------------------- -void DataAccessor::printParticleCombinations() const -{ - for (auto& kv : SymmetrizationIndices_) - LOG(INFO) << kv.second << " : " << to_string_with_parent(*(kv.first)); -} - //------------------------- bool DataAccessor::consistent() const { diff --git a/src/DecayTree.cxx b/src/DecayTree.cxx index 73bbcb86..b29c1b7a 100644 --- a/src/DecayTree.cxx +++ b/src/DecayTree.cxx @@ -176,4 +176,15 @@ const DecayTreeVector select_changed(const DecayTreeVector& dtv) return C; } +//------------------------- +std::string to_string(const std::map& decayTrees) +{ + std::string s; + for (const auto& m_dtv : decayTrees) { + for (const auto& dt : m_dtv.second) + s += "\ndepth = " + std::to_string(depth(*dt)) + "\n" + dt->asString() + "\n"; + } + return s; +} + } diff --git a/src/DecayingParticle.cxx b/src/DecayingParticle.cxx index 936a2c7d..b3ac26a5 100644 --- a/src/DecayingParticle.cxx +++ b/src/DecayingParticle.cxx @@ -260,8 +260,10 @@ std::vector< std::shared_ptr > DecayingParticle::finalStateP } //------------------------- -void DecayingParticle::printDecayChainLevel(int level) const +std::string DecayingParticle::decayChainLevelAsString(int level) const { + std::ostringstream os; + // get maximum length of particle names static size_t padding = 0; static size_t paddingSpinAmp = 0; @@ -271,33 +273,35 @@ void DecayingParticle::printDecayChainLevel(int level) const for (std::shared_ptr d : c->daughters()) { padding = std::max(padding, d->name().length()); if (std::dynamic_pointer_cast(d)) - std::static_pointer_cast(d)->printDecayChainLevel(-1); + os << std::static_pointer_cast(d)->decayChainLevelAsString(-1); paddingSpinAmp = std::max(paddingSpinAmp, to_string(c->spinAmplitudes()).length()); } } if (level == -1) - return; + return os.str(); } for (size_t i = 0; i < Channels_.size(); ++i) { if (i > 0) - std::cout << "\n" << std::setw(level * (padding * 3 + 8 + paddingSpinAmp)) << ""; + os << "\n" << std::setw(level * (padding * 3 + 8 + paddingSpinAmp)) << ""; - std::cout << std::left << std::setw(padding) << this->name() << " ->"; + os << std::left << std::setw(padding) << this->name() << " ->"; for (std::shared_ptr d : Channels_[i]->daughters()) - std::cout << " " << std::setw(padding) << d->name(); - std::cout << std::left << std::setw(paddingSpinAmp) + os << " " << std::setw(padding) << d->name(); + os << std::left << std::setw(paddingSpinAmp) << to_string(Channels_[i]->spinAmplitudes()); for (std::shared_ptr d : Channels_[i]->daughters()) if (std::dynamic_pointer_cast(d)) { - std::cout << ", "; - std::static_pointer_cast(d)->printDecayChainLevel(level + 1); + os << ", "; + os << std::static_pointer_cast(d)->decayChainLevelAsString(level + 1); } } if (level == 0) - std::cout << "\n"; + os << "\n"; + + return os.str(); } //------------------------- @@ -364,17 +368,6 @@ void DecayingParticle::modifyDecayTree(DecayTree& dt) const } } -//------------------------- -std::string DecayingParticle::printDecayTrees() const -{ - std::string s; - for (const auto& m_dtv : DecayTrees_) { - for (const auto& dt : m_dtv.second) - s += "\ndepth = " + std::to_string(depth(*dt)) + "\n" + dt->asString() + "\n"; - } - return s; -} - //------------------------- const std::complex amplitude(const std::map& m_dtv_map, const DataPoint& d) { diff --git a/src/FourMomenta.cxx b/src/FourMomenta.cxx index 2d4399bc..744351f9 100644 --- a/src/FourMomenta.cxx +++ b/src/FourMomenta.cxx @@ -134,8 +134,9 @@ void FourMomenta::calculate(DataPoint& d, StatusManager& sm) const } //------------------------- -std::ostream& print_mp_string(std::ostream& os, unsigned n, unsigned m_p, std::shared_ptr pc, double m, FourVector p, std::shared_ptr M = nullptr) +std::string mp_string(unsigned n, unsigned m_p, std::shared_ptr pc, double m, FourVector p, std::shared_ptr M = nullptr) { + std::ostringstream os; os << std::setw(n) << indices_string(*pc) << " : " << "m = " << std::setprecision(m_p) << m << " GeV/c^2"; if (M) @@ -143,61 +144,61 @@ std::ostream& print_mp_string(std::ostream& os, unsigned n, unsigned m_p, std::s else os << " " << std::setw(m_p) << " " << " "; os << "\tp = " << p << " GeV"; - return os; + return os.str(); } //------------------------- -std::ostream& FourMomenta::printMasses(const DataPoint& d, std::ostream& os) const +std::string masses_as_string(const FourMomenta& fm, const DataPoint& d) { - os << "Invariant masses:" << std::endl; - unsigned n_fsp = model()->finalStateParticles().size(); + std::string s = "Invariant masses:\n"; + unsigned n_fsp = fm.model()->finalStateParticles().size(); unsigned n = n_fsp + 2; unsigned m_p = 6; std::set used; - // print the ISP - for (auto& kv : symmetrizationIndices()) + // the ISP + for (auto& kv : fm.symmetrizationIndices()) // if ISP and not yet printed (should only print once) if (kv.first->indices().size() == n_fsp and used.find(kv.second) == used.end()) { - os << " ISP : "; - print_mp_string(os, n, m_p, kv.first, m(d, kv.first), p(d, kv.first)); - os << std::endl; + s += " ISP : "; + s += mp_string(n, m_p, kv.first, fm.m(d, kv.first), fm.p(d, kv.first)); + s += "\n"; used.insert(kv.second); } - // print the FSP's - for (size_t i = 0; i < FSPIndices_.size(); ++i) - for (auto& kv : symmetrizationIndices()) + // the FSP's + for (size_t i = 0; i < fm.model()->finalStateParticles().size(); ++i) + for (auto& kv : fm.symmetrizationIndices()) if (kv.first->isFinalStateParticle() and kv.first->indices()[0] == i and used.find(kv.second) == used.end()) { - os << " FSP : "; - print_mp_string(os, n, m_p, kv.first, m(d, kv.first), p(d, kv.first), model()->finalStateParticles()[i]->mass()); - os << std::endl; + s += " FSP : "; + s += mp_string(n, m_p, kv.first, fm.m(d, kv.first), fm.p(d, kv.first), fm.model()->finalStateParticles()[i]->mass()); + s += "\n"; used.insert(kv.second); } - // print the rest in increasing number of particle content + // the rest in increasing number of particle content for (unsigned i = 2; i < n_fsp; ++i) - for (auto& kv : symmetrizationIndices()) + for (auto& kv : fm.symmetrizationIndices()) // if i-particle mass and not yet printed if (kv.first->indices().size() == i and used.find(kv.second) == used.end()) { - os << " " << i << "-p : "; - print_mp_string(os, n, m_p, kv.first, m(d, kv.first), p(d, kv.first)); - os << std::endl; + s += " " + std::to_string(i) + "-p : "; + s += mp_string(n, m_p, kv.first, fm.m(d, kv.first), fm.p(d, kv.first)); + s += "\n"; used.insert(kv.second); } - // print unused - for (auto& kv : symmetrizationIndices()) + // unused + for (auto& kv : fm.symmetrizationIndices()) if (used.find(kv.second) == used.end()) { - os << " : "; - print_mp_string(os, n, m_p, kv.first, m(d, kv.first), p(d, kv.first)); - os << std::endl; + s += " : "; + s += mp_string(n, m_p, kv.first, fm.m(d, kv.first), fm.p(d, kv.first)); + s += "\n"; used.insert(kv.second); } - return os; + return s; } } diff --git a/src/Model.cxx b/src/Model.cxx index 49e846ed..c4ac8501 100644 --- a/src/Model.cxx +++ b/src/Model.cxx @@ -8,6 +8,7 @@ #include "DataSet.h" #include "DecayChannel.h" #include "DecayingParticle.h" +#include "DecayTree.h" #include "FinalStateParticle.h" #include "FourMomenta.h" #include "HelicityAngles.h" @@ -72,7 +73,7 @@ const double sum_of_logs_of_squared_amplitudes(const Model& M, DataPartition& D) // incoherently sum over initialStateParticles for (auto& kv : M.initialStateParticles()) { FDEBUG("calculate amplitude for " << to_string(*kv.first) << " with decay trees:"); - DEBUG(kv.first->printDecayTrees()); + DEBUG(to_string(kv.first->decayTrees())); L += log(kv.second->value() * norm(amplitude(kv.first->decayTrees(), d))); } } @@ -359,18 +360,15 @@ void Model::prepareDataAccessors() } -#ifndef ELPP_DISABLE_DEBUG_LOGS - std::cout << "StaticDataAccessors:\n"; - for (auto& D : StaticDataAccessors_) { - std::cout << std::endl; - D->printParticleCombinations(); - } - std::cout << "DataAccessors:\n"; - for (auto& D : DataAccessors_) { - std::cout << std::endl; - D->printParticleCombinations(); - } -#endif + // print all DataAccessors + DEBUG("StaticDataAccessors:"); + std::for_each(StaticDataAccessors_.begin(), StaticDataAccessors_.end(), + [] (const StaticDataAccessor* da) {DEBUG(to_string(da->symmetrizationIndices()))}); + + DEBUG("DataAccessors:"); + DEBUG("StaticDataAccessors:"); + std::for_each(DataAccessors_.begin(), DataAccessors_.end(), + [] (const DataAccessor* da) {DEBUG(to_string(da->symmetrizationIndices()))}); lock(); } @@ -625,47 +623,58 @@ void Model::setParameterFlagsToUnchanged() } //------------------------- -void Model::printDataAccessors(bool printParticleCombinations) const +std::string data_accessors_as_string(const Model& m, bool printParticleCombinations) { + using std::to_string; + // header - std::cout << "DataAccessors of \n" - << "index \tnSymIndices \taddress \tname"; + std::string s = "DataAccessors of \nindex \tnSymIndices \taddress \tname"; if (printParticleCombinations) - std::cout << "\t\tparticleCombinations"; + s += "\t\tparticleCombinations"; - std::cout << std::endl; + s += "\n"; - for (const auto& d : DataAccessors_) { - std::cout << d->index() << " \t" << d->nSymmetrizationIndices() << " \t\t" << d << " \t(" << typeid(*d).name() << ") \t"; + for (const auto& d : m.dataAccessors()) { + std::stringstream ss; + ss << d; + s += to_string(d->index()) + " \t" + to_string(d->nSymmetrizationIndices()) + " \t\t" + + ss.str() + " \t(" + typeid(*d).name() + ") \t"; if (printParticleCombinations) { - std::cout << " \t"; - - for (const auto& pc_i : d->symmetrizationIndices()) - std::cout << *pc_i.first << ":" << pc_i.second << "; "; + s += " \t"; + s += to_string(d->symmetrizationIndices()); } - std::cout << std::endl; + s += "\n"; } - std::cout << std::endl; + s += "\n"; + + return s; } //------------------------- -void Model::printFlags(const StatusManager& sm) const +std::string flags_as_string(const Model& m, const StatusManager& sm) { - for (const auto& d : DataAccessors_) { - std::cout << std::endl; + using std::to_string; + std::string s; + + for (const auto& d : m.dataAccessors()) { + s += "\n"; for (auto& c : d->cachedDataValues()) { - std::cout << " CachedDataValue " << c << ": "; + std::stringstream ss; + ss << c; + s += " CachedDataValue " + ss.str() + ": "; for (unsigned i = 0; i < d->nSymmetrizationIndices(); ++i) - std::cout << sm.status(*c, i) << "; "; - std::cout << "\n"; + s += to_string(sm.status(*c, i)) + "; "; + s += "\n"; } } - std::cout << std::endl; + s += "\n"; + + return s; } }