Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
nadrino committed Dec 4, 2024
2 parents 37e2bf7 + afcb2e0 commit a729315
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ cmake_minimum_required (VERSION 3.5 FATAL_ERROR)

project(cpp-generic-toolbox)

# This toolbox is written in C++11
set (CMAKE_CXX_STANDARD 11)
# This toolbox is written in C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Include the header folder
include_directories(./include)
Expand Down
6 changes: 3 additions & 3 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "GenericToolbox.Thread.h" // extension
#include "GenericToolbox.Utils.h" // extension

//#ifdef ENABLE_ROOT_EXTENSION
//#include <GenericToolbox.Root.h>
//#endif
#ifdef ENABLE_ROOT_EXTENSION
#include <GenericToolbox.Root.h>
#endif

#include "classExample.h"

Expand Down
36 changes: 29 additions & 7 deletions include/GenericToolbox.Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,42 @@

#include <iostream>
#include <exception>
#include <sstream>
#include <string>
#include <iomanip>


// a very simple logger
namespace GenericToolbox{
namespace LoggerUtils{

inline std::string getTimeStr(){
std::stringstream ss;
time_t rawTime = std::time(nullptr);
struct tm timeInfo = *localtime(&rawTime);
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ <= 4)
char buffer[128];
std::strftime(buffer, sizeof(buffer), "%Y.%m.%d %H:%M:%S", &timeInfo);
ss << buffer;
#else
ss << std::put_time(&timeInfo, "%Y.%m.%d %H:%M:%S");
#endif
return ss.str();
}

}
}

// internals
#define GTLogBase std::string(__FILENAME__) + ":" + std::to_string(__LINE__) + ": "
#define GTLogBase std::string(__FILENAME__) + ":" + std::to_string(__LINE__) + "/" + __func__ + ": "

// basics
#define GTLogError std::cout << "[ERROR] " << GTLogBase
#define GTLogAlert std::cout << "[ALERT] " << GTLogBase
#define GTLogWarning std::cout << "[ WARN] " << GTLogBase
#define GTLogInfo std::cout << "[ INFO] " << GTLogBase
#define GTLogDebug std::cout << "[DEBUG] " << GTLogBase
#define GTLogTrace std::cout << "[TRACE] " << GTLogBase
#define GTLogError std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " ERROR " << GTLogBase
#define GTLogAlert std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " ALERT " << GTLogBase
#define GTLogWarning std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " WARN " << GTLogBase
#define GTLogInfo std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " INFO " << GTLogBase
#define GTLogDebug std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " DEBUG " << GTLogBase
#define GTLogTrace std::cout << GenericToolbox::LoggerUtils::getTimeStr() << " TRACE " << GTLogBase

// throw
#define GTLogThrow(message_) GTLogError << message_ << std::endl; throw std::runtime_error("exception thrown by the logger.")
Expand Down
19 changes: 19 additions & 0 deletions include/GenericToolbox.Root.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ namespace GenericToolbox{
// Function header
namespace GenericToolbox{

inline TMatrixD* toTMatrixD(TH2D* input_){
if( input_ == nullptr ){
GTLogError << "input TH2D is nullptr." << std::endl;
return nullptr;
}

std::unique_ptr<TMatrixD> out{nullptr};

out = std::make_unique<TMatrixD>(input_->GetNbinsX(), input_->GetNbinsY());

for( int iCol = 0 ; iCol < input_->GetNbinsX() ; iCol++ ){
for( int iRow = 0 ; iRow < input_->GetNbinsY() ; iRow++ ){
(*out)[iCol][iRow] = input_->GetBinContent(iCol, iRow);
}
}

return out.release();
}

//! Conversion Tools
inline TH1D* convertToTH1D(const TVectorD *yValuesPtr_, const std::string &histTitle_ = "", const std::string &yTitle_ = "", const std::string &xTitle_ = "Entry #", TVectorD *yErrorsPtr_ = nullptr);
inline TH1D* convertToTH1D(const std::vector<double> &Y_values_, const std::string &histTitle_ = "", const std::string &Y_title_ = "", const std::string &X_title_ = "Entry #", TVectorD *Y_errors_ = nullptr);
Expand Down
8 changes: 8 additions & 0 deletions include/GenericToolbox.Wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ namespace GenericToolbox{

// core
void waitUntilEqual(const T& val_) const;
template <typename U = T, typename = std::enable_if_t<std::is_same<U, bool>::value>> void waitUntilEqualThenToggle( bool val_){
waitUntilEqual(val_);
setValue(!val_);
}
template <typename U = T, typename = std::enable_if_t<std::is_same<U, bool>::value>> void toggleThenWaitUntilEqual( bool val_){
setValue(!val_);
waitUntilEqual(val_);
}

// Define operator++ (post-increment) using SFINAE
// Using operator++(int) and not operator++(T) to stay consistent with C++ conventions */
Expand Down

0 comments on commit a729315

Please sign in to comment.