I am trying to compare (in the sense of seeing by eye their differences) 2 histograms that have a different binning and different ranges.
My idea was to take the one with the larger range (let’s call it big), rebin it to have the same binning as the one with the smaller range (let’s call it small), then clone the small one and using SetBinContent, copy the values of the rebined to the cloned!
My code to achieve that is the following
#include <TSystem.h>
#include <TH1.h>
#include <TFile.h>
#include "Riostream.h"
#include <fstream>
#include <iostream>
#include <iomanip>// std::setprecision
#include <stdio.h>
using namespace std;
using std::cout;
using std::endl;
TH1F* compress(TH1F* h_big, TH1F* h_small){
unsigned int nbins_big = h_big->GetNbinsX();
unsigned int nbins_small = h_small->GetNbinsX();
TH1F *h_compressed = (TH1F*) h_small->Clone();
h_compressed->Reset();
if ( nbins_big < nbins_small ){cout << "First histogram should be the bigger. Returning empty histogram." << endl; return (h_compressed);}
else if ( nbins_big == nbins_small ){cout << "Histograms have already the same binning.Returning the same input histogram." << endl; return (h_big);}
else{
if ( nbins_big % nbins_small != 0 ){cout << "Rebin cannot be performed. The two binnings are not dividable. Returning empty histogram." << endl; return (h_compressed);}
else{
unsigned int rebin_factor = nbins_big/nbins_small;
h_big->Rebin(rebin_factor);
for ( unsigned int i=0; i<nbins_small; ++i ){
float bin_content = (float) h_big->GetBinContent(i+1);
h_compressed->SetBinContent(i+1, bin_content);
}
h_compressed->Scale(1./rebin_factor);
return (h_compressed);
}
}
}
void caen(char * file_in_1, char * h_in_1, char * file_in_2, char * h_in_2){
TString file1(file_in_1);
TString h1(h_in_1);
TString file2(file_in_2);
TString h2(h_in_2);
cout << "Looking for histogram " << h1 << " in file " << file1 << endl;
cout << "Looking for histogram " << h2 << " in file " << file2 << endl;
// (1) Open the root file and get the histogram
TFile *fin1 = new TFile(file1,"READ");//Open the root file
if ( fin1->IsOpen() ) cout << "File " << fin1->GetName() << " opened successfully\n" << endl;//Check if file is opened
TH1F *h_Big = (TH1F*)fin1->Get(h1);
// (2) Read the ascii file, store its values in a vector and make a histogram with the values
std::ifstream fin2(file2);
std::vector<float> floats;
float value;
//Check if the float was successfully read from the file
cout << "Filling vector..." << endl;
while(fin2 >> value){
// store it in the vector.
floats.push_back(value);
}
TH1F* h_Small = new TH1F("h_Small","Input Amplitude Distribution; Amplitude (ADC units); Counts",floats.size(),0,floats.size());
cout << "Filling histogram..." << endl;
for (unsigned int i = 0; i < floats.size(); ++i){
h_Small->SetBinContent(i+1, floats[i]);
}
TH1F *h_Compressed = compress(h_Big, h_Small);
//h_Small->Draw("histo");
h_Big->Draw("histosame");
//h_Compressed->Draw("histosame");
}
To run it on a root session
.L compress.C++
caen("run106386_out.root", "hamp1", "amp_Pu240_5.csv", "h2")
The problem is that when I run it, it seems like the compressed histogram (red) is shifted compared to the original one (blue) as seen in this plot
Then while I was trying to debug and see what happened I noticed this strange behavior. If I try to plot only the h_Big, while using the compress() function, thus using the code as was pasted above, I get the following output
So in principle, I would expect to have the original histogram printed. Instead it seems that it’s rebined! Indeed if I comment out the line
TH1F *h_Compressed = compress(h_Big, h_Small);
Then I get this
which is the histogram a unrebined, as it supposed to be!
So it seems that when I call the function, the input histogram is rebined!
How can that be possible?
Thank you very much in advance!
Here is a link with the files that might be needed.
https://cernbox.cern.ch/index.php/s/4djYkzm3J5RYAJt
https://cernbox.cern.ch/index.php/s/59zHg4VqfHwadWs