Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added fmt as external dependency and printing example #278

Merged
merged 4 commits into from
Sep 14, 2022

Conversation

lobis
Copy link
Member

@lobis lobis commented Jul 31, 2022

lobis Medium: 107

Added fmt (https://github.com/fmtlib/fmt) as dependency to help with string formatting.

I also moved -lstdc++fs outside of that block, I think it was a mistake (it should always be linked).

I added some simple class / methods in the fmt namespace and placed in TRestTools.h/cpp to help with printing.

Methods such as fmt::ToEnergyString take a number in the default REST energy scale and return the appropiate string, for example fmt::ToEnergyString(10000) returns "10.00 MeV". Other methods such as fmt::ToLengthString have been added and its easy to implement more.

I updated the print for TRestEvent (and TRestGeant4Event/Track in another PR) to showcase what can be done with this library.

For instance one can define a custom formatter for any class, such as TVector3 to simplify printing:

template <>
struct fmt::formatter<TVector3> : formatter<string> {
    auto format(TVector3 c, format_context& ctx) {
        string s = fmt::format("({:0.3f}, {:0.3f}, {:0.3f})", c.X(), c.Y(), c.Z());
        return formatter<string>::format(s, ctx);
    }
};

TVector3 myVector = {1,2,3};
fmt::print("{}", myVector); // will print "(1.000, 2.000, 3.000)"

Its very easy to add colors to the terminal which helps when reading the output:

image

@lobis lobis changed the title Added fmt as external dependency Added fmt as external dependency and printing example Aug 1, 2022
@lobis lobis added enhancement New feature or request development To define issues with development proposals labels Aug 1, 2022
@lobis lobis requested review from nkx111, jgalan and juanangp and removed request for nkx111 August 1, 2022 10:22
@lobis lobis marked this pull request as ready for review August 1, 2022 10:23
@nkx111
Copy link
Member

nkx111 commented Aug 7, 2022

Is this dependence necessary?

We already have ToString() method for TVector3, through TRestReflector, and colored output methods in TRestStringOutput. We can also modify this class for energy output methods.

If fmt library is really better, I suggest to modify TRestStringOutput and try to adapt it. For example, we can make RESTMetadata<<"xxx"<<RESTEndl; calling print(fg(color::green) | emphasis::bold, "xxx"); internally.

@jgalan
Copy link
Member

jgalan commented Sep 2, 2022

I think it is good that we keep a unified way to output things on screen. I.e. using REST_xyz macros, I think that is the point from @nkx111 in the last comment.

@lobis lobis merged commit 87e26a3 into master Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
development To define issues with development proposals enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants