# Doubts on histogram sum

**URL:** <https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314>\
**Category:** ROOT\
**Created:** [January 28, 2023, 3:25pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314 "2023-01-28T15:25:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 28, 2023, 3:25pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/1 "2023-01-28T15:25:59Z")

</div>

Hi, in this macro I sum the two MC simulations (6997 and 6998) and BIB to compare the sum to data reaction.

But looking the each one of the MC histograms, I see peaks that I don’t see in the sum. For example, approximately at 2300keV the MC 6997 (magenta) has a huge peack (near the legend) that isn’t present in the sum (orange).  
Is my code wrong?

Ps. root files are quite large…could you check the code without the files? Otherwise I will upload them

 ![Rea0611-MC6998-MC6997-BIB0735_GePD](https://root-forum.cern.ch/uploads/default/original/3X/6/f/6f2272fa56ab6880fe57b6b51009af948df239e8.png)

[mcdata.c](https://root-forum.cern.ch/uploads/short-url/4vv7ioGNoHx6LnYoQajTINgByXz.c) (7.3 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 28, 2023, 8:53pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/2 "2023-01-28T20:53:49Z")

</div>

Draw the “sum” as the very last one or, even better, draw all these histograms on separate pads (you will then clearly see each of them).

Then, you need to talk to your colleagues and/or your supervisor about the choice of histograms and cuts.

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 29, 2023, 10:00am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/3 "2023-01-29T10:00:52Z")

</div>

Hi @Wile_E_Coyote

> [@Wile\_E\_Coyote](#):
>
> Draw the “sum” as the very last one or, even better, draw all these histograms on separate pads (you will then clearly see each of them).

Here the plot with some over other plots. As you can see the peaks are missing

 ![Rea0611-MC6998-MC6997-BIB0735_GePD](https://root-forum.cern.ch/uploads/default/original/3X/d/6/d6fb961955f3fed01b9504ef85d6ec450e5b9997.png)  
[mcdata.c](https://root-forum.cern.ch/uploads/short-url/pqtps4LsvsBrTaANwn8n9cRdpFq.c) (7.0 KB)

> [@Wile\_E\_Coyote](#):
>
> even better, draw all these histograms on separate pads (you will then clearly see each of them).

How should I do it? I’m using the code

```auto
tBIB->Draw(hStringBIB0,cut0a && cut0b,"same");	
TH1F *hBIB0 = (TH1F*)gPad->GetPrimitive("htempBIB0");

```

It gets data and draws them. How just get data to sum the plot without draw partial plots?  
Should I fill the TH1F without drawing?Or is there faster way?

> [@Wile\_E\_Coyote](#):
>
> Then, you need to talk to your colleagues and/or your supervisor about the choice of histograms and cuts.

The cuts are right:  
I use:

```auto
TCut cut0a = TString::Format("e0>0").Data();
TCut cut0c = TString::Format("Edep[0]>0").Data();

```

to plot only detected events (i.e. events depositing energy in the detectors).

and

`TCut cut0b = TString::Format("e2==0").Data();`

to exclude signals from the pulser. Anyway, the problem is that peaks are missing when I sum the histograms. I mean, if I sum two histograms I should see the peaks in the sum…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 29, 2023, 10:21am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/4 "2023-01-29T10:21:49Z")

</div>

The simplest “visual check” could be to put at the end of your macro:

```cpp
TCanvas *c = new TCanvas("c", "c");
c->Divide(3, 2);
c->cd(1); gPad->SetLogy(); hRea0->Draw();
c->cd(2); gPad->SetLogy(); hBIB0->Draw();
c->cd(3); gPad->SetLogy(); hMC970->Draw();
c->cd(4); gPad->SetLogy(); hMC980->Draw();
c->cd(5); gPad->SetLogy(); hSum0->Draw();
c->cd(6); gPad->SetLogy(); diff0->Draw();
c->cd(0);

```

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 29, 2023, 11:33am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/6 "2023-01-29T11:33:21Z")

</div>

Hi @Wile_E_Coyote

 ![Test](https://root-forum.cern.ch/uploads/default/original/3X/0/d/0d5591c285c9be22726cea94c8740d382fed9823.png)

actually, when plotting the sum histogram alone, the peaks are the peaks.  
Then, maybe is there any error when I superimpose the plots?

EDIT:  
I tried to only superimpose reaction data and the sum and I get this plot

 ![Rea0611-MC6998-MC6997-BIB0735-JustSum_GePD](https://root-forum.cern.ch/uploads/default/original/3X/8/a/8af29ec74ee4f34bef99944cdf1e5676a687a8af.png)

it looks like to me that when I superimpose the plots, the peaks of the sum are not printed as line (as it happen when I plot the sum alone) but just by few points…then it’s difficult to see the peks

[mcdata.c](https://root-forum.cern.ch/uploads/short-url/jzRZcDoS5VobWf351g2d6nKT68x.c) (7.8 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 29, 2023, 11:47am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/7 "2023-01-29T11:47:35Z")

</div>

Looks like you miss the `"HIST"` drawing option in the “`Draw`” call.

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 29, 2023, 11:48am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/8 "2023-01-29T11:48:56Z")

</div>

Yes, I know it looks like that HIST option is missing, but as you can see in the macro

[mcdata.c](https://root-forum.cern.ch/uploads/short-url/jzRZcDoS5VobWf351g2d6nKT68x.c) (7.8 KB)

the HIST option is set.

EDIT: Solved! I had to set HIST option also in Draw() function! example:

```auto
hRea0->Draw("HIST");
   	hSum0->Draw("HIST SAME");

```

I forgot it and I just set  
`h->SetOption("HIST");`

Thanks @Wile_E_Coyote

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 29, 2023, 12:19pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/9 "2023-01-29T12:19:10Z")

</div>

One more question @Wile_E_Coyote please.

As you can see in the plot the sum histogram (orange) is a factor alpha higher than the reaction plot (blue). Then I should rescale the sum plot to have the same height of the reaction one. Ie. I should plot the sum plot as

alpha\*(MC97+MC98+BIB/Q\_BIB)

Is there an automatic way to get the alpha coefficient?

 ![Rea0611-MC6998-MC6997-BIB0735-JustSum_GePD](https://root-forum.cern.ch/uploads/default/original/3X/a/c/acf63fc6581eee9efc3a62068083f78c95f8a219.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 29, 2023, 12:28pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/10 "2023-01-29T12:28:05Z")

</div>

[TFractionFitter](https://root.cern/doc/master/classTFractionFitter.html)

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [January 29, 2023, 1:01pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/11 "2023-01-29T13:01:56Z")

</div>

Hello @Wile_E_Coyote

I tried

```auto
TCanvas *c03 = new TCanvas("c01","c01",1280,1024);
    gPad->SetLeftMargin(margl);
   	gPad->SetRightMargin(margr);
   	 TObjArray *mcbib = new TObjArray(3); // MC and BIB histograms are put in this array
   	 mcbib->Add(hMC970);
   	 mcbib->Add(hMC980);
   	 mcbib->Add(hBIB0);
   	 TFractionFitter* fit = new TFractionFitter(hRea0, mcbib); // initialise
   fit->Constrain(1,0.0,1.0); // constrain fraction 1 to be between 0 and 1
   fit->SetRangeX(1,15); // use only the first 15 bins in the fit
   Int_t status = fit->Fit(); // perform the fit
   std::cout << "fit status: " << status << std::endl;
   if (status == 0) { // check on fit status
     TH1F* result = (TH1F*) fit->GetPlot();
     hRea0->Draw("Ep");
     result->Draw("same");
 }
     c03->Print("Simulazione/Rea0611-MC6998-MC6997-BIB0735-Fitter_GePD.png");

```

but I get

```auto
**********
 **1** SET PRINT 0
 **********
 **********
 **2** SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO. NAME VALUE STEP SIZE LIMITS
     1 frac0 3.33333e-01 1.00000e-02 no limits
     2 frac1 3.33333e-01 1.00000e-02 0.00000e+00 1.00000e+00
     3 frac2 3.33333e-01 1.00000e-02 no limits
 **********
 **3** SET ERR 0.5
 **********
 **********
 **4** SET PRINT 0
 **********
 **********
 **5** SET STR 1
 **********
 **********
 **6** MIGRAD 1345 0.01
 **********
 MINUIT WARNING IN MIGRAD  
 ============== Negative diagonal element 1 in Error Matrix
 MINUIT WARNING IN MIGRAD  
 ============== 1.00339 added to diagonal of error matrix
 MINUIT WARNING IN HESSE   
 ============== Negative diagonal element 1 in Error Matrix
 MINUIT WARNING IN HESSE   
 ============== 115119 added to diagonal of error matrix
 MIGRAD TERMINATED WITHOUT CONVERGENCE.
 FCN=-34764.7 FROM MIGRAD STATUS=FAILED 70 CALLS 71 TOTAL
                     EDM=1.26678e+08 STRATEGY= 1 ERR MATRIX APPROXIMATE
  EXT PARAMETER APPROXIMATE STEP FIRST   
  NO. NAME VALUE ERROR SIZE DERIVATIVE 
   1 frac0 -2.64959e+01 1.00066e+00 -0.00000e+00 1.59066e+04
   2 frac1 2.18605e-01 1.21803e-03 0.00000e+00 2.66754e+01
   3 frac2 7.20523e-01 2.93541e-03 0.00000e+00 -4.26068e+02
                               ERR DEF= 0.5
Warning in <TFractionFitter::Fit>: Abnormal termination of minimization.
fit status: 4
Info in <TCanvas::Print>: png file Simulazione/Rea0611-MC6998-MC6997-BIB0735-Fitter_GePD.png has been created

 ***Break*** segmentation violation

```

[mcdata.c](https://root-forum.cern.ch/uploads/short-url/lFzTc7jkPLtIx6H4Ou6ti2fbedC.c) (9.3 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 31, 2023, 6:55pm UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/12 "2023-01-31T18:55:59Z")

</div>

This looks strange in your case:

```cpp
fit->Constrain(1,0.0,1.0); // constrain fraction 1 to be between 0 and 1
fit->SetRangeX(1,15); // use only the first 15 bins in the fit

```

Another approach (not using the [TFractionFitter](https://root.cern/doc/master/classTFractionFitter.html)) has been proposed in:

> [@Newbie Fit/RooFit Question](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334):
>
> Hi, I’m pretty new to Fit/RooFit, and I need some help to get started with my current project. My goal is to find the best-fit parameters to weigh histograms into the desired shape. This means I have three TH1s (mc1, mc2, data); I want to give mc1 and mc2 different weights to make them fit the shape of data (sth like par[0]\*mc1+par[1]\*mc2=data maybe by minimizing chi2 between the weighted MC and Data). Since par[0]+par[1] !=1, TFractionFitter does not work for me. Is there any way I can achiev…

---

<div class="post-metadata">

**Author:** ![faca87](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/b19c9b/32.png) [@faca87](https://root-forum.cern.ch/u/faca87)\
**Post date:** [February 5, 2023, 7:24am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/13 "2023-02-05T07:24:14Z")

</div>

Hi @Wile_E_Coyote

> [@Wile\_E\_Coyote](#):
>
> This looks strange in your case:
> 
> ```cpp
> fit->Constrain(1,0.0,1.0); // constrain fraction 1 to be between 0 and 1
> fit->SetRangeX(1,15); // use only the first 15 bins in the fit
> 
> ```

Hi deleted these lines, but I still get error (see attachment)

[error.txt](https://root-forum.cern.ch/uploads/short-url/eQ9UHO3hu2jVI9OUFHZmpUB4KL4.txt) (4.6 KB)

> [@Wile\_E\_Coyote](#):
>
> Another approach (not using the [TFractionFitter](https://root.cern/doc/master/classTFractionFitter.html)) has been proposed in:
> 
> [Newbie Fit/RooFit Question](https://root-forum.cern.ch/t/newbie-fit-roofit-question/53334)

I’m going to read this post too.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [February 19, 2023, 7:24am UTC](https://root-forum.cern.ch/t/doubts-on-histogram-sum/53314/14 "2023-02-19T07:24:52Z")

</div>

Closed on February 19, 2023, 7:24am UTC

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