41 lines
1.4 KiB
C++
41 lines
1.4 KiB
C++
|
void Try() {
|
||
|
// Define your data
|
||
|
std::vector<double> times = {1, 1.2, 1.5, 1.7, 2.1, 2.2, 2.9};
|
||
|
std::vector<double> values = {5, 6, 7, 8, 9, 10, 11};
|
||
|
|
||
|
// Define your bin edges
|
||
|
std::vector<double> binEdges = {1, 2, 3}; // Define your desired bin edges here
|
||
|
|
||
|
// Create a histogram with custom binning
|
||
|
TH1F *hist = new TH1F("custom_hist", "Custom Histogram", binEdges.size() - 1, &binEdges[0]);
|
||
|
|
||
|
// Initialize vectors to store bin contents and counts
|
||
|
std::vector<double> binContents(binEdges.size() - 1, 0.0);
|
||
|
std::vector<int> binCounts(binEdges.size() - 1, 0);
|
||
|
|
||
|
// Loop through data and fill the histogram
|
||
|
for (size_t i = 0; i < times.size(); ++i) {
|
||
|
double val = values[i];
|
||
|
for (size_t j = 0; j < binEdges.size() - 1; ++j) {
|
||
|
if (times[i] >= binEdges[j] && times[i] < binEdges[j + 1]) {
|
||
|
binContents[j] += val;
|
||
|
binCounts[j]++;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Calculate the means for each bin and set the bin content
|
||
|
for (size_t i = 0; i < binEdges.size() - 1; ++i) {
|
||
|
double binMean = (binCounts[i] > 0) ? binContents[i] / binCounts[i] : 0;
|
||
|
hist->SetBinContent(i + 1, binMean); // Set the bin content to the mean
|
||
|
}
|
||
|
|
||
|
// Create a canvas and draw the histogram
|
||
|
TCanvas *canvas = new TCanvas("canvas", "Custom Histogram", 800, 600);
|
||
|
hist->Draw();
|
||
|
canvas->SaveAs("custom_histogram.png");
|
||
|
|
||
|
delete canvas;
|
||
|
}
|