<commit_before>#include <vector>

#include "TH1D.h"
#include "TList.h"

#include "RooAddPdf.h"
#include "RooArgSet.h"
#include "RooRealVar.h"
#include "RooCategory.h"
#include "RooDataSet.h"
#include "RooKeysPdf.h"

#include "FitTools.h"

using namespace RooFit;

ClassImp(FitTools)


//--------------------------------------------------------------------
FitTools::FitTools()
{
  SetMultiplyLumi(true);
}

//--------------------------------------------------------------------
FitTools::~FitTools()
{ }

//--------------------------------------------------------------------
void
FitTools::GetJointPdf(const char* name, std::vector<TString> files, FileType type)
{
  std::vector<FileInfo*> *info = GetFileInfo(type);

  // Open the files and get the TTrees
  OpenFiles(files);

  // Initialize an empty dataset
  const char *dataname = TString::Format("Dataset_%s", name);
  RooDataSet dataset(dataname, dataname, 
                     RooArgSet(*fWorkspace.var(fDefaultExpr),
                               *fWorkspace.cat("categories")));

  // Add the data from each tree to the dataset
  for (UInt_t iTree = 0; iTree != fInTrees.size(); ++iTree) {
    const char *datasetname = TString::Format("Dataset_%s_%i", name, iTree).Data();
    RooDataSet *tempData = new RooDataSet(datasetname, datasetname, fInTrees[iTree],
                                          RooArgSet(*fWorkspace.var(fDefaultExpr),
                                                    *fWorkspace.cat("categories")),
                                          fBaseCut, fMCWeight);
    dataset.append(*tempData);
  }

  // Split and add these pdfs together with the proper relative weights, if needed
  const char *pdfName = TString::Format("pdf_%s", name);

  if (name[0] == 'F') {

    TList *dataList = dataset.split(*fWorkspace.cat("categories"));

    Message(eDebug, "Dataset list size: %i", dataList->GetSize());

    RooArgSet pdfSet;
    RooArgSet coeffSet;

    for (UInt_t iBin = 0; iBin != fCategoryNames.size(); ++iBin) {

      const char *pdfNameTemp = TString::Format("pdf_%s_%i", name, iBin);
      RooKeysPdf *tempPdf = new RooKeysPdf(pdfNameTemp, pdfNameTemp, *fWorkspace.var(fDefaultExpr),
                                           *(static_cast<RooDataSet*>(dataList->At(iBin))));
      pdfSet.add(*tempPdf);

      if (iBin != fCategoryNames.size() - 1) {
        const char *coeffName = TString::Format("coeff_%s_%i", name, iBin);
        RooRealVar *tempVar = new RooRealVar(coeffName, coeffName, 0.0, 1.0);
        coeffSet.add(*tempVar);
      }
    }

    RooAddPdf finalPdf(pdfName, pdfName, pdfSet, coeffSet);
    fWorkspace.import(finalPdf);

  }
  else {

    RooKeysPdf finalPdf(pdfName, pdfName, *fWorkspace.var(fDefaultExpr), dataset);
    fWorkspace.import(finalPdf);

  }

  CloseFiles();

  // create the pdf

}

//--------------------------------------------------------------------
void
FitTools::FitCategories(TString CategoryVar, Int_t NumCategories,
                        Double_t Shape_Min, Double_t Shape_Max, const char* ShapeLabel)
{

  DisplayFunc(__func__);
  Message(eDebug, "About to fit %i categories %s to shape of %s",
          NumCategories, CategoryVar.Data(), fDefaultExpr.Data());
  Message(eDebug, "Data cut is %s", fDataBaseCut.GetTitle());
  Message(eDebug, " MC  cut is %s", fBaseCut.GetTitle());
  if (fSignalName != "")
    Message(eDebug, "Only reweighting %s", fSignalName.Data());

  Int_t NumBins = 1;
  Double_t *XBins;
  ConvertToArray(NumBins, Shape_Min, Shape_Max, XBins);

  // Create the variable being plotted
  RooRealVar variable = RooRealVar(fDefaultExpr, ShapeLabel, Shape_Min, Shape_Max);

  // Create category variable
  RooCategory category = RooCategory("categories", "categories");

  // Set category names
  for (UInt_t iCat = 0; iCat != fCategoryNames.size(); ++iCat)
    category.defineType(fCategoryNames[iCat], iCat);

  fWorkspace.import(variable);
  fWorkspace.import(category);

  TH1D *FloatSize = GetHist(NumBins, XBins, fSignalType, fSignalName, fSearchBy, true);

  // Get the pdf that we'll be floating
  GetJointPdf("Floating", ReturnFileNames(fSignalType, fSignalName, fSearchBy, true), fSignalType);

  TH1D *StaticSize = GetHist(NumBins, XBins, kBackground, fSignalName, fSearchBy, false);

  // Get the other background files that will be static
  GetJointPdf("Static", ReturnFileNames(kBackground, fSignalName, fSearchBy, false));

}<commit_msg>Fixed the allocation of the array.<commit_after>#include <vector>

#include "TH1D.h"
#include "TList.h"

#include "RooAddPdf.h"
#include "RooArgSet.h"
#include "RooRealVar.h"
#include "RooCategory.h"
#include "RooDataSet.h"
#include "RooKeysPdf.h"

#include "FitTools.h"

using namespace RooFit;

ClassImp(FitTools)


//--------------------------------------------------------------------
FitTools::FitTools()
{
  SetMultiplyLumi(true);
}

//--------------------------------------------------------------------
FitTools::~FitTools()
{ }

//--------------------------------------------------------------------
void
FitTools::GetJointPdf(const char* name, std::vector<TString> files, FileType type)
{
  std::vector<FileInfo*> *info = GetFileInfo(type);

  // Open the files and get the TTrees
  OpenFiles(files);

  // Initialize an empty dataset
  const char *dataname = TString::Format("Dataset_%s", name);
  RooDataSet dataset(dataname, dataname, 
                     RooArgSet(*fWorkspace.var(fDefaultExpr),
                               *fWorkspace.cat("categories")));

  // Add the data from each tree to the dataset
  for (UInt_t iTree = 0; iTree != fInTrees.size(); ++iTree) {
    const char *datasetname = TString::Format("Dataset_%s_%i", name, iTree).Data();
    RooDataSet *tempData = new RooDataSet(datasetname, datasetname, fInTrees[iTree],
                                          RooArgSet(*fWorkspace.var(fDefaultExpr),
                                                    *fWorkspace.cat("categories")),
                                          fBaseCut, fMCWeight);
    dataset.append(*tempData);
  }

  // Split and add these pdfs together with the proper relative weights, if needed
  const char *pdfName = TString::Format("pdf_%s", name);

  if (name[0] == 'F') {

    TList *dataList = dataset.split(*fWorkspace.cat("categories"));

    Message(eDebug, "Dataset list size: %i", dataList->GetSize());

    RooArgSet pdfSet;
    RooArgSet coeffSet;

    for (UInt_t iBin = 0; iBin != fCategoryNames.size(); ++iBin) {

      const char *pdfNameTemp = TString::Format("pdf_%s_%i", name, iBin);
      RooKeysPdf *tempPdf = new RooKeysPdf(pdfNameTemp, pdfNameTemp, *fWorkspace.var(fDefaultExpr),
                                           *(static_cast<RooDataSet*>(dataList->At(iBin))));
      pdfSet.add(*tempPdf);

      if (iBin != fCategoryNames.size() - 1) {
        const char *coeffName = TString::Format("coeff_%s_%i", name, iBin);
        RooRealVar *tempVar = new RooRealVar(coeffName, coeffName, 0.0, 1.0);
        coeffSet.add(*tempVar);
      }
    }

    RooAddPdf finalPdf(pdfName, pdfName, pdfSet, coeffSet);
    fWorkspace.import(finalPdf);

  }
  else {

    RooKeysPdf finalPdf(pdfName, pdfName, *fWorkspace.var(fDefaultExpr), dataset);
    fWorkspace.import(finalPdf);

  }

  CloseFiles();

  // create the pdf

}

//--------------------------------------------------------------------
void
FitTools::FitCategories(TString CategoryVar, Int_t NumCategories,
                        Double_t Shape_Min, Double_t Shape_Max, const char* ShapeLabel)
{

  DisplayFunc(__func__);
  Message(eDebug, "About to fit %i categories %s to shape of %s",
          NumCategories, CategoryVar.Data(), fDefaultExpr.Data());
  Message(eDebug, "Data cut is %s", fDataBaseCut.GetTitle());
  Message(eDebug, " MC  cut is %s", fBaseCut.GetTitle());
  if (fSignalName != "")
    Message(eDebug, "Only reweighting %s", fSignalName.Data());

  Double_t XBins[2];
  ConvertToArray(1, Shape_Min, Shape_Max, XBins);

  // Create the variable being plotted
  RooRealVar variable = RooRealVar(fDefaultExpr, ShapeLabel, Shape_Min, Shape_Max);

  // Create category variable
  RooCategory category = RooCategory("categories", "categories");

  // Set category names
  for (UInt_t iCat = 0; iCat != fCategoryNames.size(); ++iCat)
    category.defineType(fCategoryNames[iCat], iCat);

  fWorkspace.import(variable);
  fWorkspace.import(category);

  TH1D *FloatSize = GetHist(1, XBins, fSignalType, fSignalName, fSearchBy, true);

  // Get the pdf that we'll be floating, named "pdf_Floating"
  GetJointPdf("Floating", ReturnFileNames(fSignalType, fSignalName, fSearchBy, true), fSignalType);

  TH1D *StaticSize = GetHist(1, XBins, kBackground, fSignalName, fSearchBy, false);

  // Get the other background files that will be static, named "pdf_Static"
  GetJointPdf("Static", ReturnFileNames(kBackground, fSignalName, fSearchBy, false));

}
{'fraction_non_alphanumeric': 0.18466741826381058, 'fraction_numerical': 0.003269447576099211, 'mean_word_length': 2.65512978986403, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6512861', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2758, 'n_words': 632}