<commit_before>#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

#include "TFile.h"
#include "TH1.h"

#include "LUtils/Debugging.hxx"
#include "LUtils/Utils.hxx"

namespace Opts {
int LowBinEdgeColumn = 0;
int UpBinEdgeColumn = -1;
int ValueColumn = 1;
std::string InputFName = "";
std::string OutputFName = "";
bool TextInput = true;
bool DoPDF = true;
std::string InputTHName = "";
}

int WriteFile(float *BinCenters, float *BinWidths, float *BinValues,
              size_t NBins) {
  float Integral = 0;
  float WidthIntegral = 0;
  for (size_t i = 0; Opts::DoPDF && (i < NBins); ++i) {
    Integral += BinValues[i];
    WidthIntegral += BinWidths[i] * BinValues[i];
  }

  std::ofstream of(Opts::OutputFName);
  of << "# input flux integral: " << Integral
     << " (width integral: " << WidthIntegral << ")" << std::endl;
  if (!of.good()) {
    std::cerr << "[ERROR]: File \"" << Opts::OutputFName
              << " could not be opened for writing." << std::endl;
    return 4;
  }

  for (size_t i = 0; i < NBins; ++i) {
    of << BinCenters[i] << " "
       << (BinValues[i] / (Opts::DoPDF ? Integral : 1.0)) << std::endl;
  }
  of.close();
  return 0;
}

int ROOTTH_ToBinCenterPDFFlux_Text() {
  TFile *inpf = TFile::Open(Opts::InputFName.c_str(), "READ");
  if (!inpf || !inpf->IsOpen()) {
    std::cerr << "[ERROR]: Could not open " << Opts::InputFName
              << " ROOT file for reading." << std::endl;
    return 1;
  }
  TH1 *inph = dynamic_cast<TH1 *>(inpf->Get(Opts::InputTHName.c_str()));
  if (!inph) {
    std::cerr << "[ERROR]: ROOT file " << Opts::InputFName
              << " does not appear to contain a TH1 named: "
              << Opts::InputTHName << std::endl;
    return 2;
  }

  float *BinCenters = new float[inph->GetXaxis()->GetNbins()];
  float *BinWidths = new float[inph->GetXaxis()->GetNbins()];
  float *BinValues = new float[inph->GetXaxis()->GetNbins()];
  for (Int_t i = 0; i < inph->GetXaxis()->GetNbins(); ++i) {
    BinCenters[i] = inph->GetXaxis()->GetBinCenter(i + 1);
    BinWidths[i] = (inph->GetXaxis()->GetBinLowEdge(i + 2) -
                    inph->GetXaxis()->GetBinLowEdge(i + 1));
    BinValues[i] = inph->GetBinContent(i + 1);
    std::cout << "[ROOT] Bin: " << (i + 1) << ", center: " << BinCenters[i]
              << ", width: " << BinWidths[i] << ", value: " << BinValues[i]
              << std::endl;
  }
  bool res =
      WriteFile(BinCenters, BinWidths, BinValues, inph->GetXaxis()->GetNbins());
  delete BinCenters;
  delete BinWidths;
  delete BinValues;
  return res;
}

/// I hate TFL
struct tfl {
  float lbe;
  float ube;
  float v;
};

int Text_BinEdgeToBinCenterPDFFlux_Text() {
  std::ifstream ifs(Opts::InputFName);
  if (!ifs.good()) {
    std::cerr << "[ERROR]: File \"" << Opts::InputFName
              << " could not be opened for reading." << std::endl;
    return 1;
  }
  std::string line;

  size_t ln = 0;
  std::vector<tfl> BinEdgeVals;
  while (std::getline(ifs, line)) {
    if (line[0] == '#') {  // ignore comments
      ln++;
      continue;
    }

    std::vector<float> splitLine =
        Utils::StringVToFloatV(Utils::SplitStringByDelim(line, " "));<commit_msg>std::string initialised fstreams...<commit_after>#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

#include "TFile.h"
#include "TH1.h"

#include "LUtils/Debugging.hxx"
#include "LUtils/Utils.hxx"

namespace Opts {
int LowBinEdgeColumn = 0;
int UpBinEdgeColumn = -1;
int ValueColumn = 1;
std::string InputFName = "";
std::string OutputFName = "";
bool TextInput = true;
bool DoPDF = true;
std::string InputTHName = "";
}

int WriteFile(float *BinCenters, float *BinWidths, float *BinValues,
              size_t NBins) {
  float Integral = 0;
  float WidthIntegral = 0;
  for (size_t i = 0; Opts::DoPDF && (i < NBins); ++i) {
    Integral += BinValues[i];
    WidthIntegral += BinWidths[i] * BinValues[i];
  }

  std::ofstream of(Opts::OutputFName.c_str());
  of << "# input flux integral: " << Integral
     << " (width integral: " << WidthIntegral << ")" << std::endl;
  if (!of.good()) {
    std::cerr << "[ERROR]: File \"" << Opts::OutputFName
              << " could not be opened for writing." << std::endl;
    return 4;
  }

  for (size_t i = 0; i < NBins; ++i) {
    of << BinCenters[i] << " "
       << (BinValues[i] / (Opts::DoPDF ? Integral : 1.0)) << std::endl;
  }
  of.close();
  return 0;
}

int ROOTTH_ToBinCenterPDFFlux_Text() {
  TFile *inpf = TFile::Open(Opts::InputFName.c_str(), "READ");
  if (!inpf || !inpf->IsOpen()) {
    std::cerr << "[ERROR]: Could not open " << Opts::InputFName
              << " ROOT file for reading." << std::endl;
    return 1;
  }
  TH1 *inph = dynamic_cast<TH1 *>(inpf->Get(Opts::InputTHName.c_str()));
  if (!inph) {
    std::cerr << "[ERROR]: ROOT file " << Opts::InputFName
              << " does not appear to contain a TH1 named: "
              << Opts::InputTHName << std::endl;
    return 2;
  }

  float *BinCenters = new float[inph->GetXaxis()->GetNbins()];
  float *BinWidths = new float[inph->GetXaxis()->GetNbins()];
  float *BinValues = new float[inph->GetXaxis()->GetNbins()];
  for (Int_t i = 0; i < inph->GetXaxis()->GetNbins(); ++i) {
    BinCenters[i] = inph->GetXaxis()->GetBinCenter(i + 1);
    BinWidths[i] = (inph->GetXaxis()->GetBinLowEdge(i + 2) -
                    inph->GetXaxis()->GetBinLowEdge(i + 1));
    BinValues[i] = inph->GetBinContent(i + 1);
    std::cout << "[ROOT] Bin: " << (i + 1) << ", center: " << BinCenters[i]
              << ", width: " << BinWidths[i] << ", value: " << BinValues[i]
              << std::endl;
  }
  bool res =
      WriteFile(BinCenters, BinWidths, BinValues, inph->GetXaxis()->GetNbins());
  delete BinCenters;
  delete BinWidths;
  delete BinValues;
  return res;
}

/// I hate TFL
struct tfl {
  float lbe;
  float ube;
  float v;
};

int Text_BinEdgeToBinCenterPDFFlux_Text() {
  std::ifstream ifs(Opts::InputFName.c_str());
  if (!ifs.good()) {
    std::cerr << "[ERROR]: File \"" << Opts::InputFName
              << " could not be opened for reading." << std::endl;
    return 1;
  }
  std::string line;

  size_t ln = 0;
  std::vector<tfl> BinEdgeVals;
  while (std::getline(ifs, line)) {
    if (line[0] == '#') {  // ignore comments
      ln++;
      continue;
    }

    std::vector<float> splitLine =
        Utils::StringVToFloatV(Utils::SplitStringByDelim(line, " "));
{'fraction_non_alphanumeric': 0.18777224642190418, 'fraction_numerical': 0.008089607965152458, 'mean_word_length': 3.1584734799482534, 'pattern_counts': {'":': 0, '<': 167, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5794713', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2358, 'n_words': 522}