Fill different colors to the different bin ranges defined for the multiplicity histogram for different centrality ranges

I am making a histogram for the particle multiplicity as shown in the code(attached a link to download). I wanted to fill the color in the histogram for the different centrality bins 0-5%, 5-10%,… 90-100%. I don’t know how can I do it in the function named “void Draw_MultiplicityHistogram(){}” I have defined in the code.
void Draw_MultiplicityHistogram(TH1F* histogram, const TString& xTitle, const TString& yTitle, bool logY = false) {

TCanvas* canvas = new TCanvas("", " ", 800, 600);

//canvas->SetFillColor(kWhite); // Set canvas background color
histogram->GetXaxis()->SetTitle(xTitle);
histogram->GetYaxis()->SetTitle(yTitle);

if (logY) {
    canvas->SetLogy(); // Set logarithmic Y axis
}
double Total_Entries = histogram->GetEntries();

cout << "Total Entries / Integral is : " << Total_Entries << endl;

Int_t NofBins = histogram->GetNbinsX();

cout << "Total bin content is : " << NofBins << endl;

// Define percentile ranges
Double_t Percentiles[] = {0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
Double_t Nof_Percent_Range = sizeof(Percentiles) / sizeof(Percentiles[0]);

cout << "Total percentile : " << Nof_Percent_Range << endl;

cout << "Percentile Ranges:" << endl;

// Loop through each percentile range
for (Int_t i = 0; i < Nof_Percent_Range - 1; ++i) {

    Double_t Start = Percentiles[i];
    Double_t End = Percentiles[i + 1];

    Double_t RangeDifference = End - Start;
    Double_t ThresholdEntries = RangeDifference * (Total_Entries / 100.0);

    cout << "Percent : " << " " << End << " - " << Start << " = " << RangeDifference << " Threshold 
    Entries:  " << ThresholdEntries << endl;

    Double_t integral = 0;
    Int_t binIndex = NofBins;

    // Calculate integral in reverse order until it fulfills threshold
    while (binIndex >= 0 && integral < ThresholdEntries) {

        Double_t binContent = histogram->GetBinContent(binIndex);
        
        if (binContent > 0) {
            integral += binContent;
        }
        binIndex--;
    }
    cout << "Integral: " << integral << endl;

    // Check if integral matches the threshold entries
    if (integral >= ThresholdEntries) {
       
        histogram->SetFillColor(i + 2);
        histogram->Draw();
    }
}


canvas->Update();

}
download link:
https://drive.google.com/drive/folders/1TZsDKZhiWEHBphI-GhdvyLYl4C3H5qwO?usp=sharing

The histogram color applies on all the bins. You will need to define several histogram for each ranges and specify the color you need for each of them. Then you can group them in a THStack and draw it with option “nostack”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.