Problem in Rebin using variable bin size

Dear Rooters,

My code does not work while I try to use Rebin with variable bin size.
There is no error but the rebinned histogram is not drawn.
Could you please help to correct it.

Thank you very much, NA.C (1.4 KB)
Run0001.root (1.3 MB)

NAnh,


_ROOT Version: 5.34
_Platform: Window
Compiler: Not Provided


When I run your macro I get:

root [0] 
Processing NA.C...
In file included from input_line_9:1:
/Users/couet/Downloads/NA.C:21:2: error: use of undeclared identifier 'Read_Simulation'
        Read_Simulation("Run0001.root","hExI0to36TSC_",20,"","");
        ^

I don’t understand. Here is my protocol:

D:\Download>root NA.C


  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 5.34/32 23 June 2015 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      http://root.cern.ch            *
    
  •                                     *
    

ROOT 5.34/32 (v5-34-32@v5-34-32, Jun 23 2015, 17:58:02 on win32)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0]
Processing NA.C…
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [1]

ah ok … got it . It is it because you are using 5.34. I fixed it for 6. Looking at it now.

Now i get more details with the very recent ROOT version

root [0] 
Processing NA.C...
Warning in <TH1D::Rebin>: Bin edge 1 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 2 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 3 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 4 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 5 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 6 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 7 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 8 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 9 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 10 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 11 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 12 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1] 

Thank you couet,

could you please upload your corrected version?

In your file just invert the order of the two functions.

Can you please explain why swap the order of the two functions solve the problem?

Your main function is calling a sub-function you have not declared at the beginning of the file. Either you declare it or you write the called function before the main function.

It turns out that if I uses .L NA.C then NA(), problem will be solved? But it is not, unfortunately.

The macro NA.C I have now is the following:

#include<string>
#include <sstream>



void Read_Simulation(string rootFileName, string prefix_TSCname, int number_of_Realization, string newhisName,string rootFileNametoWrite){
	// Open rootFileName
	TFile f(rootFileName.c_str());
	//f.GetListOfKeys()->Print(); //Test
	// Read First TSC spectrum (real = 0)
	string stmp1 = prefix_TSCname + "0";
	TH1F *h1 = (TH1F*)f.Get(stmp1.c_str());
	h1->SetDirectory(0);
	// Loop for all TSC spectra
	for (int i=1; i<number_of_Realization;i++)
	{
		stringstream ss; ss << i;
		string tmp = prefix_TSCname + ss.str();
		TH1F *h2 = (TH1F*)f.Get(tmp.c_str());
		h1->Add(h2);
		delete h2;
	}//End for loop
	// Scale histogram
	h1->Scale(1.0/number_of_Realization);
	double xbins[13]={0.1,0.5,1,1.5,2.0, 2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0};
	TH1F *h1new = (TH1F*)h1->Rebin(12,"h1new",xbins);
	//h1->Rebin(15);
	h1new->Draw();

}

void NA(){
	// TFile f("Run0001.root");
	// f.GetListOfKeys()->Print();
	// TH1F *h1 = (TH1F*)f.Get("hExI0to36TSC_1");
	// TH1F *h2 = (TH1F*)f.Get("hExI0to36TSC_2");
	// TH1F *h3 = (TH1F*)f.Get("hExI0to36TSC_3");
	// h1->SetDirectory(0);
	// h2->SetDirectory(0);
	// h3->SetDirectory(0);
	// h1->Add(h2);
	// h1->Add(h3);
	// h1->Draw();

	// string s = "abc";
	// stringstream ss; ss << 42;
	// s = s + "_1" + ss.str();
	// cout<<s<<endl;
	Read_Simulation("Run0001.root","hExI0to36TSC_",20,"","");
}

When I run it with the latest ROOT version I get the following messages:

$ root NA.C
   ------------------------------------------------------------
  | Welcome to ROOT 6.19/01                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for macosx64 on Jun 28 2019, 07:44:41                |
  | From heads/master@v6-19-01-295-g30065525ce                 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] 
Processing NA.C...
Warning in <TH1D::Rebin>: Bin edge 1 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 2 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 3 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 4 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 5 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 6 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 7 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 8 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 9 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 10 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 11 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Warning in <TH1D::Rebin>: Bin edge 12 of rebinned histogram does not much any bin edges of the old histogram. Result can be inconsistent
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1

Hi,
NA.C (1.5 KB)
there are 3 problems:

  1. Read_Simulation must be defined before used as couet pointed out
  2. h1new, h1rebin need SetDirectory(0) otherwise they go out of
    scope when macro terminates (TFile f was current dir)
  3. more serious:
    Rebin works only correctly if the new bin edges coincide with an
    edge in the original hist (bins can’t be divided)
    The best you can do refill a new histogram taking the
    bin centers and contents of the original. (see att macro)
    The right thing to do would to fill new histogram at the
    time you fill the originals when you still have the exact
    values

Cheers
Otto
PS: for the maintainer of TH1:
The word “much” in
"histogram does not much any bin edges "
should be “match” I guess

Hi Otto,
You are right. Now fixed. Thanks to have seen it.
Olivier

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