<commit_before>#include <iostream>
#include "TAxis.h"
#include "TArray.h"
#include "Corrector.h"

ClassImp(Corrector)

//--------------------------------------------------------------------
Corrector::Corrector(TString name) :
  fName(name),
  fInTree(NULL),
  fCorrectionFile(NULL),
  fCorrectionHist(NULL),
  fNumDims(0),
  fInCut('1'),
  fCutFormula(NULL)
{
  fInExpressions.resize(0);
  fFormulas.resize(0);
  fMins.resize(0);
  fMaxs.resize(0);
}
  
//--------------------------------------------------------------------
Corrector::~Corrector()
{
  if (fCorrectionFile != NULL) {
    if (fCorrectionFile->IsOpen())
      fCorrectionFile->Close();
  }

  if (fCutFormula != NULL)
    delete fCutFormula;
  
  for (UInt_t iFormula = 0; iFormula != fFormulas.size(); ++iFormula)
    delete fFormulas[iFormula];
}

//--------------------------------------------------------------------
void
Corrector::SetCorrectionHist(TString hist1, TString hist2)
{
  fCorrectionHist  = (TH1*) fCorrectionFile->Get(hist1);
  TH1* divisorHist = (TH1*) fCorrectionFile->Get(hist2);
  fCorrectionHist->Divide(divisorHist);
  SetMinMax();
}
  
//--------------------------------------------------------------------
Double_t
Corrector::GetFormulaResult(Int_t index)
{
  Double_t eval = fFormulas[index]->EvalInstance();
  if (eval < fMins[index])
    eval = fMins[index];
  else if (eval > fMaxs[index])
    eval = fMaxs[index];
  return eval;
}

//--------------------------------------------------------------------
Float_t
Corrector::Evaluate()
{
  if (fInTree == NULL)
    return 1.0;
  else {
    if (fCutFormula->EvalInstance() != 0) {
      if (fNumDims == 1) {
        Double_t evalX = GetFormulaResult(0);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX));
      }
      else if (fNumDims == 2) {
        Double_t evalX = GetFormulaResult(0);
        Double_t evalY = GetFormulaResult(1);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX),
                                              fCorrectionHist->FindBin(evalY));
      }
      else if (fNumDims == 3) {
        Double_t evalX = GetFormulaResult(0);
        Double_t evalY = GetFormulaResult(1);
        Double_t evalZ = GetFormulaResult(2);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX),
                                              fCorrectionHist->FindBin(evalY),
                                              fCorrectionHist->FindBin(evalZ));
      }
      else
        return 1.0;
    }
    else
      return 1.0;
  }
}

//--------------------------------------------------------------------
void
Corrector::InitializeTree()
{
  if (fCutFormula)
    delete fCutFormula;

  fCutFormula = new TTreeFormula(fInCut,fInCut,fInTree);

  if (fFormulas.size() != 0) {
    for (UInt_t iFormula = 0; iFormula != fFormulas.size(); ++iFormula)
      delete fFormulas[iFormula];
    fFormulas.resize(0);
  }

  TTreeFormula* tempFormula;
  for (UInt_t iExpression = 0; iExpression != fInExpressions.size(); ++iExpression) {
    tempFormula = new TTreeFormula(fInExpressions[iExpression],fInExpressions[iExpression],fInTree);
    fFormulas.push_back(tempFormula);
  }
}

//--------------------------------------------------------------------
void
Corrector::SetMinMax()
{
  for (Int_t iDim = 0; iDim != fNumDims; ++iDim) {
    TAxis* theAxis;
    if (iDim == 0)
      theAxis = fCorrectionHist->GetXaxis();
    else if (iDim == 1)
      theAxis = fCorrectionHist->GetYaxis();
    else if (iDim == 2)
      theAxis = fCorrectionHist->GetZaxis();
    else {
      std::cout << "I don't support this many axes at the moment." << std::endl;
      exit(3);
    }
    const TArray* theArray = theAxis->GetXbins();
    fMins.push_back(theArray->GetAt(0));
    fMaxs.push_back(theArray->GetAt(theArray->GetSize() - 1));<commit_msg>Made more flexible<commit_after>#include <iostream>
#include "TAxis.h"
#include "Corrector.h"

ClassImp(Corrector)

//--------------------------------------------------------------------
Corrector::Corrector(TString name) :
  fName(name),
  fInTree(NULL),
  fCorrectionFile(NULL),
  fCorrectionHist(NULL),
  fNumDims(0),
  fInCut('1'),
  fCutFormula(NULL)
{
  fInExpressions.resize(0);
  fFormulas.resize(0);
  fMins.resize(0);
  fMaxs.resize(0);
}
  
//--------------------------------------------------------------------
Corrector::~Corrector()
{
  if (fCorrectionFile != NULL) {
    if (fCorrectionFile->IsOpen())
      fCorrectionFile->Close();
  }

  if (fCutFormula != NULL)
    delete fCutFormula;
  
  for (UInt_t iFormula = 0; iFormula != fFormulas.size(); ++iFormula)
    delete fFormulas[iFormula];
}

//--------------------------------------------------------------------
void
Corrector::SetCorrectionHist(TString hist1, TString hist2)
{
  fCorrectionHist  = (TH1*) fCorrectionFile->Get(hist1);
  TH1* divisorHist = (TH1*) fCorrectionFile->Get(hist2);
  fCorrectionHist->Divide(divisorHist);
  SetMinMax();
}
  
//--------------------------------------------------------------------
Double_t
Corrector::GetFormulaResult(Int_t index)
{
  Double_t eval = fFormulas[index]->EvalInstance();
  if (eval < fMins[index])
    eval = fMins[index];
  else if (eval > fMaxs[index])
    eval = fMaxs[index];
  return eval;
}

//--------------------------------------------------------------------
Float_t
Corrector::Evaluate()
{
  if (fInTree == NULL)
    return 1.0;
  else {
    if (fCutFormula->EvalInstance() != 0) {
      if (fNumDims == 1) {
        Double_t evalX = GetFormulaResult(0);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX));
      }
      else if (fNumDims == 2) {
        Double_t evalX = GetFormulaResult(0);
        Double_t evalY = GetFormulaResult(1);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX),
                                              fCorrectionHist->FindBin(evalY));
      }
      else if (fNumDims == 3) {
        Double_t evalX = GetFormulaResult(0);
        Double_t evalY = GetFormulaResult(1);
        Double_t evalZ = GetFormulaResult(2);
        return fCorrectionHist->GetBinContent(fCorrectionHist->FindBin(evalX),
                                              fCorrectionHist->FindBin(evalY),
                                              fCorrectionHist->FindBin(evalZ));
      }
      else
        return 1.0;
    }
    else
      return 1.0;
  }
}

//--------------------------------------------------------------------
void
Corrector::InitializeTree()
{
  if (fCutFormula)
    delete fCutFormula;

  fCutFormula = new TTreeFormula(fInCut,fInCut,fInTree);

  if (fFormulas.size() != 0) {
    for (UInt_t iFormula = 0; iFormula != fFormulas.size(); ++iFormula)
      delete fFormulas[iFormula];
    fFormulas.resize(0);
  }

  TTreeFormula* tempFormula;
  for (UInt_t iExpression = 0; iExpression != fInExpressions.size(); ++iExpression) {
    tempFormula = new TTreeFormula(fInExpressions[iExpression],fInExpressions[iExpression],fInTree);
    fFormulas.push_back(tempFormula);
  }
}

//--------------------------------------------------------------------
void
Corrector::SetMinMax()
{
  for (Int_t iDim = 0; iDim != fNumDims; ++iDim) {
    TAxis* theAxis;
    if (iDim == 0)
      theAxis = fCorrectionHist->GetXaxis();
    else if (iDim == 1)
      theAxis = fCorrectionHist->GetYaxis();
    else if (iDim == 2)
      theAxis = fCorrectionHist->GetZaxis();
    else {
      std::cout << "I don't support this many axes at the moment." << std::endl;
      exit(3);
    }
    fMins.push_back(theAxis->GetBinLowEdge(theAxis->GetFirst()));
    fMaxs.push_back(theAxis->GetBinUpEdge(theAxis->GetLast()));
{'fraction_non_alphanumeric': 0.24533472530340597, 'fraction_numerical': 0.010439775544825787, 'mean_word_length': 1.9274255156608098, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2620802', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2444, 'n_words': 422}