示例#1
0
void BuildAPdf() {
    // Build the pdf
    AxisCollection ax;
    ax.AddAxis(PdfAxis("energy", 0, 3, 100));
    ax.AddAxis(PdfAxis("radius", 0, 1, 100));
    ax.AddAxis(PdfAxis("psd", 0, 1, 100));
    ax.AddAxis(PdfAxis("psd2", 0, 1, 100));
    BinnedPdf pdf(ax);
    std::vector<size_t> indicies;
    indicies.push_back(0);
    indicies.push_back(1);
    indicies.push_back(2);
    indicies.push_back(3);
    pdf.SetDataRep(DataRepresentation(indicies));


    // Get the data
    ROOTHandle rHandle(fileName, treeName);
    unsigned nEntries = rHandle.GetNEntries();
    std::cout << nEntries << " Entries to fill" << std::endl;
    // Fill the Pdf
    for(size_t i = 0; i < nEntries; i++) {
        pdf.Fill(rHandle.GetEntry(i));
    }

    return;
}
示例#2
0
#include <catch.hpp>
#include <AxisCollection.h>

TEST_CASE("Correct Indexing for 3x3 Uniform Axes","[AxisCollection]"){

    PdfAxis axis1("axis1", 0,  20, 100);
    PdfAxis axis2("axis2", 10, 30, 219);
    PdfAxis axis3("axis3", 0, 1, 2341);

    AxisCollection axes;
    axes.AddAxis(axis1);
    axes.AddAxis(axis2);
    axes.AddAxis(axis3);

    SECTION("Correct Number of Bins"){
        REQUIRE(axes.GetNBins() == (100 * 219 * 2341));
    }

    SECTION("Correct Number of Dimensions"){
        REQUIRE(axes.GetNDimensions() == 3);
    }


    SECTION("Global Bin Conversions for first and last bin"){
        std::vector<size_t> firstBins(3, 0);
        std::vector<size_t> lastBins;
        lastBins.push_back(99);
        lastBins.push_back(218);
        lastBins.push_back(2340);

        REQUIRE(axes.FlattenIndicies(firstBins) == 0);
示例#3
0
int main(){        
    ////////////////////        
    // 1. Set Up PDFs //        
    ////////////////////        
         
    // Set up binning        
    AxisCollection axes;        
    axes.AddAxis(PdfAxis("energy", 2, 3, 10, "Energy"));               
         
    // Only interested in first bit of data ntuple        
    DataRepresentation dataRep(0);        
         
    // Set up pdf with these bins in this observable        
    BinnedPdf bgPdf(axes);      bgPdf.SetDataRep(dataRep);        
    BinnedPdf signalPdf(axes);  signalPdf.SetDataRep(dataRep);        
         
    std::cout << "Initialised Pdfs" << std::endl;        
         
    /////////////////////////////////////        
    // 2. Fill with data and normalise //        
    /////////////////////////////////////        
         
    ROOTNtuple bgMC("filename.root", "treename");        
    ROOTNtuple signalMC("filename.root", "treename");        
         
    for(size_t i = 0; i < 10; i++){        
        bgPdf.Fill(bgMC.GetEntry(i));        
    }        
          
    for(size_t i = 0; i < 10; i++){        
        signalPdf.Fill(signalMC.GetEntry(i));        
    }        
             
         
    bgPdf.Normalise();        
    signalPdf.Normalise();        
         
    std::cout << "Filled pdfs " << std::endl;        
         
    ////////////////////////////        
    // 3. Set Up LH function  //        
    ////////////////////////////        
         
    BinnedNLLH lhFunction;        
    lhFunction.SetDataSet(&signalMC); // initialise withe the data set        
    lhFunction.AddPdf(bgPdf);        
    lhFunction.AddPdf(signalPdf);        
    
         
    std::cout << "Built LH function " << std::endl;        
         
    // Set up the optimisation        
    GridSearch gSearch(&lhFunction);        
             
    std::vector<double> minima(2, 0);        
    std::vector<double> maxima(2, 100);        
    std::vector<double> stepsizes(2, 1);        
         
    gSearch.SetMaxima(maxima);        
    gSearch.SetMinima(minima);        
    gSearch.SetStepSizes(stepsizes);        
    
         
    ////////////        
    // 4. Fit //        
    ////////////        
    gSearch.Optimise();        
         
    std::vector<double> fit = gSearch.GetFitResult().GetBestFit();        
             
    std::cout << "Best Fit: " << std::endl;        
    for(size_t i = 0; i < fit.size(); i++)        
        std::cout << fit.at(i) << "\t";        
    std::cout << std::endl;        
    
    return 0;        
}
示例#4
0
BinnedPdf::BinnedPdf(const AxisCollection& axes_){
    fHistogram.SetAxes(axes_);
    fNDims = axes_.GetNDimensions();
}
示例#5
0
BinnedED
BinnedEDShrinker::ShrinkDist(const BinnedED& dist_) const{

    // No buffer no problem. FIXME: what about if all the values are zero?
    if (!fBuffers.size())
        return dist_;
    
    size_t nDims = dist_.GetNDims();

    // FIXME Add a check to see if the non zero entries of fBuffers are in the pdf and give warning

    // 1. Build new axes. ShrinkPdf method just makes a copy if buffer size is zero
    AxisCollection newAxes;
    const std::vector<size_t> distDataIndices = dist_.GetObservables().GetIndices();
    size_t dataIndex = 0;
    
    for(size_t i = 0; i < nDims; i++){
        dataIndex = distDataIndices.at(i);
        if (!fBuffers.count(dataIndex))
            newAxes.AddAxis(dist_.GetAxes().GetAxis(i));
        else
            newAxes.AddAxis(ShrinkAxis(dist_.GetAxes().GetAxis(i), 
                                       fBuffers.at(dataIndex).first,  
                                       fBuffers.at(dataIndex).second));
    }

    // 2. Initialise the new pdf with same data rep
    BinnedED newDist(dist_.GetName() + "_shrunk", newAxes);
    newDist.SetObservables(dist_.GetObservables());

    // 3. Fill the axes
    std::vector<size_t> newIndices(dist_.GetNDims());  // same as old, just corrected for overflow
    int   offsetIndex = 0; // note taking difference of two unsigneds
    size_t newBin = 0;     //  will loop over dims and use this to assign bin # corrected for overflow

    const AxisCollection& axes = dist_.GetAxes();
    double content = 0;
    // bin by bin of old pdf
    for(size_t i = 0; i < dist_.GetNBins(); i++){
        content = dist_.GetBinContent(i);
        if(!content) // no content no problem
            continue;

        // work out the index of this bin in the new shrunk pdf. 
        for(size_t j = 0; j < nDims; j++){
            offsetIndex = axes.UnflattenIndex(i, j);            // the index in old pdf
            if (fBuffers.count(distDataIndices.at(j)))          // offset by lower buffer if nonzero
                offsetIndex -= fBuffers.at(distDataIndices.at(j)).first;

            // Correct the ones that fall in the buffer regions
            // bins in the lower buffer have negative index. Put in first bin in fit region or ignore
            if (offsetIndex < 0){
                offsetIndex = 0;
                if(!fUsingOverflows)
                    content = 0;

            }

            // bins in the upper buffer have i > number of bins in axis j. Do the same
            if (offsetIndex >= newAxes.GetAxis(j).GetNBins()){
                offsetIndex = newAxes.GetAxis(j).GetNBins() - 1;

                if (!fUsingOverflows)
                    content = 0;
            }

            newIndices[j] = offsetIndex;
        }
        // Fill 
        newBin = newAxes.FlattenIndices(newIndices);
        newDist.AddBinContent(newBin, content);
    }

    return newDist;
}