Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:15:32

0001 #ifndef CALOCDB_MYUTILS_H
0002 #define CALOCDB_MYUTILS_H
0003 
0004 // ROOT includes --
0005 #include <TFitResultPtr.h>
0006 #include <TH1.h>
0007 
0008 // -- c++ includes --
0009 #include <concepts>
0010 #include <filesystem>
0011 #include <fstream>
0012 #include <functional>
0013 #include <iostream>
0014 #include <string>
0015 #include <vector>
0016 
0017 template <typename Func>
0018 concept InvocableWithString = std::invocable<Func, const std::string&>;
0019 
0020 class myUtils
0021 {
0022  public:
0023   static void setEMCalDim(TH1* hist);
0024 
0025   static std::pair<int, int> getSectorIB(int iphi, int ieta);
0026   static std::pair<int, int> getSectorIB(int towerIndex);
0027   static std::vector<std::string> split(const std::string& s, char delimiter);
0028   static TFitResultPtr doGausFit(TH1* hist, Double_t start, Double_t end, const std::string& name = "fitFunc");
0029 
0030   /**
0031    * @brief Reads a CSV (or any line-delimited) file and applies a handler function to each line.
0032    *
0033    * @tparam Callable The type of the function/lambda to be called for each line.
0034    * Must be invocable with a 'const std::string&'.
0035    * @param filePath The path to the input file.
0036    * @param lineHandler A function, lambda, or functor that takes a 'const std::string&' (the line)
0037    * and processes it.
0038    * @param skipHeader If true, the first line of the file will be read and discarded.
0039    * @return true if the file was successfully opened and read, false otherwise.
0040    */
0041   template <InvocableWithString Callable>  // Using the more general concept for wider applicability
0042   static Bool_t readCSV(const std::filesystem::path& filePath, Callable lineHandler, Bool_t skipHeader = true)
0043   {
0044     std::ifstream file(filePath);
0045 
0046     if (!file.is_open())
0047     {
0048       std::cout << "Error: [" << filePath.string() << "] Could not open file." << std::endl;
0049       return false;
0050     }
0051 
0052     std::string line;
0053 
0054     if (skipHeader && std::getline(file, line))
0055     {
0056       // First line read and discarded (header)
0057     }
0058 
0059     while (std::getline(file, line))
0060     {
0061       // Optional: Handle potential Windows CRLF (\r\n) issues if the file might
0062       // have them and you're on a system that only expects \n.
0063       // std::getline usually handles this, but if \r remains:
0064       // if (!line.empty() && line.back() == '\r') {
0065       //     line.pop_back();
0066       // }
0067       lineHandler(line);  // Call the user-provided function for each line
0068     }
0069 
0070     // Check for errors during read operations (other than EOF)
0071     if (file.bad())
0072     {
0073       std::cout << "Error: [" << filePath.string() << "] I/O error while reading file." << std::endl;
0074       return false;
0075     }
0076     // file.eof() will be true if EOF was reached.
0077     // file.fail() might be true if getline failed not due to eof (e.g., badbit also set).
0078     // If badbit is not set and eof() is true, it's a successful read to the end.
0079 
0080     return true;  // Successfully processed or reached EOF
0081   }
0082 };
0083 
0084 #endif