# Shift along x-axis an existing TProfile keeping the uncertainty bars

**URL:** <https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290>\
**Category:** Newbie\
**Tags:** root\
**Created:** [July 22, 2024, 3:00pm UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290 "2024-07-22T15:00:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erFross](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7feea3/32.png) [@erFross](https://root-forum.cern.ch/u/erFross)\
**Post date:** [July 22, 2024, 3:00pm UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290/1 "2024-07-22T15:00:32Z")

</div>

Hello all,  
I’m trying without success to load a TProfile inside a .root file, rescaling it to its maximum and shifting it to have a new TProfile with the same features as the first one but shifted to have the peak in x=0.  
I succeeded in shifting and normalizing it but the uncertainty bars vanish, or they become too large.  
What I guess it’s the function `SetBinError` is not working properly.  
I’ll attach a version of my code that does it and a couple of screenshots of the output.  
Thanks in advance for the help.

Code:

```auto

#include <TFile.h>
#include <TProfile.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TString.h>
#include <iostream>

using namespace std;

void template_shifter() {
    TString profile_path = "/path/";
    TString filenames = profile_path + "template_library2.root";
    
    TFile *file = TFile::Open(filenames);

    TFile *outputFile = new TFile(profile_path + "shifted_templates.root", "RECREATE");

    for(Int_t i =1;i<=4;i++){
        TProfile *profile = (TProfile*)file->Get(Form("template_mcp_ch00%d",i));

        int max_bin = profile->GetMaximumBin();
        double max_position = profile->GetBinCenter(max_bin);
        double max_value = profile->GetBinContent(max_bin);

        profile->Write();
        profile->Scale(1./max_value);
        TProfile *shiftedProfile = new TProfile(Form("shifted_template_mcp_ch00%d", i), profile->GetTitle(), profile->GetNbinsX(), -max_position, profile->GetXaxis()->GetXmax() - max_position);

        // Shift del profilo
        for (int bin = 1; bin <= profile->GetNbinsX(); bin++) {
            double original_bin_center = profile->GetBinCenter(bin);
            double shifted_bin_center = original_bin_center - max_position;
            double bin_content = profile->GetBinContent(bin);
            double bin_error = profile->GetBinError(bin)/max_position;
            shiftedProfile->Fill(shifted_bin_center, bin_content);
            shiftedProfile->SetBinError(shifted_bin_center, bin_error);
        }
        shiftedProfile->Write();
    }
    
outputFile->Close();
file->Close();
exit(0);
}

```

Loaded TProfile:

 ![Screenshot 2024-07-22 alle 16.47.24](https://root-forum.cern.ch/uploads/default/original/3X/a/d/ad5f28501ea8e6957d2eef3d2284f846403a34da.png)

Output:

 ![Screenshot 2024-07-22 alle 16.57.12](https://root-forum.cern.ch/uploads/default/original/3X/b/8/b8fa92528c4e01a96e233553a3768864a89542d1.png)

_ROOT Version:_ 6.26/11  
_Compiler:_ g++ (GCC) 11.4.1

---

<div class="post-metadata">

**Author:** ![devajith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/devajith/32/37165_2.png) [@devajith](https://root-forum.cern.ch/u/devajith)\
**Post date:** [July 23, 2024, 7:06am UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290/2 "2024-07-23T07:06:36Z")

</div>

Thank you for your question @erFross  
Let me add @moneta in the loop.

Cheers,  
Devajith

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 23, 2024, 7:14am UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290/3 "2024-07-23T07:14:35Z")

</div>

Have you tried to use SeBinContent instead of Fill ?

---

<div class="post-metadata">

**Author:** ![erFross](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7feea3/32.png) [@erFross](https://root-forum.cern.ch/u/erFross)\
**Post date:** [July 23, 2024, 8:43am UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290/4 "2024-07-23T08:43:09Z")

</div>

Hello, thanks for the answers.  
Yes, I tried but no difference

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 12, 2024, 3:27pm UTC](https://root-forum.cern.ch/t/shift-along-x-axis-an-existing-tprofile-keeping-the-uncertainty-bars/60290/5 "2024-08-12T15:27:01Z")

</div>

Hello,  
For a TProfile you cannot call `SetBinError` and `SetBinContent` since both the content and the errors are computed on the fly with the existing information. You need to set the correct bin sum of y values, bin sum of y square and number of bin entries. If the TProfile is weighted you nedd also the bin sum of weight square.  
Here is an example code to copy the TProfile information. You can use a sstarting point to shift the content horizontally

Lorenzo

[CopyTProfile.C](https://root-forum.cern.ch/uploads/short-url/s050dqgDX6uoyhdz8OPBCU4Fohr.C) (1.3 KB)
