# Energy calibrated histogram/spectrum

**URL:** <https://root-forum.cern.ch/t/energy-calibrated-histogram-spectrum/18159>\
**Category:** ROOT\
**Created:** [September 6, 2014, 11:52am UTC](https://root-forum.cern.ch/t/energy-calibrated-histogram-spectrum/18159 "2014-09-06T11:52:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 6, 2014, 11:52am UTC](https://root-forum.cern.ch/t/energy-calibrated-histogram-spectrum/18159/1 "2014-09-06T11:52:49Z")

</div>

I am trying to create a multiple histograms in one plot with data from the attached file. Actually I am trying to apply an energy callibration on the x-axis values which are channels i.e.

> [@](#):
>
> Energy = a\*channels+b

where a=2.7361 and b=-64.608 while channels = the old values and energy=the new values.

I am creating the histograms using the following code

[code]#include “Riostream.h”  
#include “TArrow.h”  
#include “TLatex.h”

void MgB2(){

```
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0000);
gStyle->SetOptFit(1111);
gStyle->SetOptTitle(0);

TPaveText *t = new TPaveText(0.005, 0.995, 0.85, 0.925, "brNDC");//left-up
t->AddText("Experimental and Simulated Spectra from MgB_{2}");
t->SetBorderSize(1);
t->SetFillColor(gStyle->GetTitleFillColor());

TCanvas *c = new TCanvas("c", "c");
c->SetFillColor(5);
c->SetFrameFillColor(10);

TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("MgB2.C","");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sMgB2.dat",dir.Data()));

Float_t x,y,z,w,k,l,m;
Int_t nlines = 0;
Int_t channels=1027;

Double_t xlow = 1.0;
TH1F *h1 = new TH1F("h1", "Total", channels, xlow, (channels + xlow));
TH1F *h2 = new TH1F("h2", "#alpha background", channels, xlow, (channels + xlow));
TH1F *h3 = new TH1F("h3", "#alpha background", channels, xlow, (channels + xlow));
TH1F *h4 = new TH1F("h4", "#alpha background", channels, xlow, (channels + xlow));
TH1F *h5 = new TH1F("h5", "#alpha background", channels, xlow, (channels + xlow));
TH1F *h6 = new TH1F("h6", "#alpha background", channels, xlow, (channels + xlow));
    
while (1) {

```

in \>\> x \>\> y \>\> z \>\> w \>\> k \>\> l \>\> m;  
if (!in.good()) break;  
if (nlines == 10) printf(“x=%8f, y=%8f, z=%8f\n”,x,y,z);  
h1-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, y);  
h2-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, z);  
h3-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, w);  
h4-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, k);  
h5-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, l);  
h6-\>Fill((x -64.608) / 2.7361 + xlow - 0.5, m);  
nlines++;  
}  
printf(“Found %d points\n”,nlines);

```
in.close();

h1->SetLineColor(8);
h1->Draw();
h1->GetXaxis()->SetTitle("Energy[keV]");
h1->GetYaxis()->SetTitle("Events"); 

h2->SetLineColor(kBlue);
h2->Draw("same"); 

leg = new TLegend(0.1,0.7,0.48,0.9);
leg->AddEntry("h1","with #delta E");
leg->AddEntry("h2","without #delta E");
leg->Draw();

t->Draw();

```

}[/code]

My output is the following

![](http://i.imgur.com/sfsGg7h.png)

You can see that there are some “lost” bins in the plot. Any idea why is this happening and how to fix it?  
Thank you very much in advance!  
[MgB2.dat](https://root-forum.cern.ch/uploads/default/original/2X/f/f85ce16b796ec01aaea81190d1cba989bd4b85a3.dat) (152 KB)

---

<div class="post-metadata">

**Author:** ![atha.stam](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atha.stam/32/14293_2.png) [@atha.stam](https://root-forum.cern.ch/u/atha.stam)\
**Post date:** [September 6, 2014, 12:31pm UTC](https://root-forum.cern.ch/t/energy-calibrated-histogram-spectrum/18159/2 "2014-09-06T12:31:43Z")

</div>

The way to solve this is to define the number of channels, the callibration parameters and the minimum bin that appears on the file.

`Int_t channels=1027;
Double_t xlow = 1.0;
Double_t a=2.7361;
Double_t b=-64.608;`

Then one has to create the histogram in a correct way. This means that the mumber of channels remains the same, but the minum and maximum values have to change according to the callibration.

```auto

```

Then, in order to fill the histogram, one has to modify the values that are parsed in the code, from the external ascii file. While y values remain the same, x values have to change

```auto

```
