# How to overlap two different histograms of two different root files but its not working

**URL:** <https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170>\
**Category:** ROOT\
**Created:** [December 4, 2017, 9:39am UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170 "2017-12-04T09:39:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 4, 2017, 9:39am UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/1 "2017-12-04T09:39:35Z")

</div>

```auto
NewTProfileMacro(void)
{
  TFile *file = new TFile("sohail1_OT_2017.root");
  TFile *f = new TFile("sohail2_OT_2017.root");
   if(!file) {
        std::cout<<"FILE CAN'T BE OPENED"<<std::endl;
              }

   file->cd("prod/SensorType;1");
     f->cd("prod/SensorType;1");
vector<float>* localx_ ,rhlocalx_;
 int i;
//TTree *mytree = new TTree("ntuples","mytree");
//TTree *tree = new TTree("ntuples","tree");
TH1D *h_localx_rhlocalx = (TH1D*)file->Get("angle_vs_amplitude_IB1"); TH1D *h_localx_rhlocalx = (TH1D*)f->Get("angle_vs_amplitude_IB1");

//mytree->Branch("angle_vs_amplitude_IB1",&angle_vs_amplitude_IB1);
///tree->Branch("angle_vs_amplitude_IB1",&angle_vs_amplitude_IB1);

           TCanvas * c11 = new TCanvas();
                  c11->cd();

                      stringstream ss1;
                ss1<< i;
              // string ss = "lorentz_angle_diff_profile_"+ss1.str()+".pdf";
         string ss = "overlaping_profile_"+ss1.str()+".pdf";
            c11->Print(ss.c_str(),".pdf") ;
           angle_vs_amplitude_IB1->Draw();

```

---

<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:** [December 4, 2017, 10:11am UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/2 "2017-12-04T10:11:31Z")

</div>

I am not sure I fully understand what you are trying to do. I tried to guess from the title of your post.

So you want to take to histograms in two different file and plot them ?

The Get method you are using is correct but do not use the same pointer for the 2 histos … you used `h_localx_rhlocalx` twice …

Then you do not plot the histograms you got but an other one (which is not declared (angle\_vs\_amplitude\_IB1) …

To summarise, the piece of code you sent seems very incomplete …

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [December 4, 2017, 2:59pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/3 "2017-12-04T14:59:02Z")

</div>

Properly indenting your code can help show problems. You didn’t name a return type for your function, your function doesn’t have a closing bracket. As mentioned, you don’t draw the histogram on the canvas you created. There are quite a few lines that aren’t doing anything useful. And you try to dereference variables that haven’t been declared. You also didn’t include dependencies, which can isn’t always required with ROOT, but is good practice.

Try using the compiler in ROOT to tell you of you errors. Here is the output I got, after adding the correct include files. (There seems to be a separate issue with `TString.h` here that I will open [another post](https://root-forum.cern.ch/t/tstring-h-no-template-name-basic-string-view/27176) for.)

```auto
root [0] .L file.C+
Info in <TMacOSXSystem::ACLiC>: creating shared library ./file_C.so
Warning in cling::IncrementalParser::CheckABICompatibility():
  Possible C++ standard library mismatch, compiled with _LIBCPP_VERSION '3700'
  Extraction of runtime standard library version was: '4000'
In file included from input_line_12:9:
In file included from ././file.C:3:
In file included from /opt/root/6.11.02/include/TFile.h:26:
In file included from /opt/root/6.11.02/include/TDirectoryFile.h:24:
In file included from /opt/root/6.11.02/include/TDirectory.h:25:
In file included from /opt/root/6.11.02/include/TNamed.h:26:
In file included from /opt/root/6.11.02/include/TString.h:28:
/opt/root/6.11.02/include/RStringView.h:35:12: error: no template named 'basic_string_view'
   typedef basic_string_view<char> string_view;
           ^
/opt/root/6.11.02/include/RStringView.h:36:12: error: no template named 'basic_string_view'
   typedef basic_string_view<char16_t> u16string_view;
           ^
/opt/root/6.11.02/include/RStringView.h:37:12: error: no template named 'basic_string_view'
   typedef basic_string_view<char32_t> u32string_view;
           ^
/opt/root/6.11.02/include/RStringView.h:38:12: error: no template named 'basic_string_view'
   typedef basic_string_view<wchar_t> wstring_view;
           ^
In file included from input_line_12:9:
././junk.C:7:1: error: C++ requires a type specifier for all declarations
NewTProfileMacro(void)
^
././junk.C:21:78: error: redefinition of 'h_localx_rhlocalx'
        TH1D *h_localx_rhlocalx = (TH1D*)file->Get("angle_vs_amplitude_IB1"); TH1D *h_localx_rhlocalx = (TH1D*)f->Get("angle_vs_amplitude_IB1");
                                                                                    ^
././junk.C:21:8: note: previous definition is here
        TH1D *h_localx_rhlocalx = (TH1D*)file->Get("angle_vs_amplitude_IB1"); TH1D *h_localx_rhlocalx = (TH1D*)f->Get("angle_vs_amplitude_IB1");
              ^
././junk.C:34:2: error: use of undeclared identifier 'angle_vs_amplitude_IB1'
        angle_vs_amplitude_IB1->Draw();
        ^
<<< cling interactive line includer >>>:1:1: error: expected '}'
^
././junk.C:8:1: note: to match this '{'
{
^
Error in <ACLiC>: Dictionary generation failed!

```

I’ve corrected the tabbing on your file and added comments to point out where the issues are. (Some of this can be gleaned quickly from the compiler error messages above.)

```auto
//Should list includes you need: sstream, iostream, TH1D, TFile, TCanvas
//Should declare return type.
NewTProfileMacro(void) {
   TFile *file = new TFile("sohail1_OT_2017.root");
   TFile *f = new TFile("sohail2_OT_2017.root");
   //Should check if `f` is valid.
   if(!file) {
      std::cout<<"FILE CAN'T BE OPENED"<<std::endl;
   }          

   file->cd("prod/SensorType;1");
   f->cd("prod/SensorType;1");
   //The following four lines don't do anything.
   vector<float>* localx_ ,rhlocalx_;
   int i;
   //TTree *mytree = new TTree("ntuples","mytree");
   //TTree *tree = new TTree("ntuples","tree");
   //You used the same pointer, declaring it twice, to get the histogram pointer.
   TH1D *h_localx_rhlocalx = (TH1D*)file->Get("angle_vs_amplitude_IB1"); 
   TH1D *h_localx_rhlocalx = (TH1D*)f->Get("angle_vs_amplitude_IB1");

   //These two lines are commented out as well.
   //mytree->Branch("angle_vs_amplitude_IB1",&angle_vs_amplitude_IB1);
   ///tree->Branch("angle_vs_amplitude_IB1",&angle_vs_amplitude_IB1);

   TCanvas * c11 = new TCanvas();
   c11->cd();     
   //You are missing the call to draw the histograms on the canvas here.

   //You can use std::to_string here to save some space.
   stringstream ss1;  
   ss1<< i;     
   // string ss = "lorentz_angle_diff_profile_"+ss1.str()+".pdf";
   string ss = "overlaping_profile_"+ss1.str()+".pdf";
   c11->Print(ss.c_str(),".pdf") ;
   //Where does this come from? `angle_vs_amplitude_IB1` is not declared anywhere.
   angle_vs_amplitude_IB1->Draw();

//The function just trails off, where is the close or return value?

```

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 4, 2017, 5:12pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/5 "2017-12-04T17:12:10Z")

</div>

thanks for your reply…actually i want to overlap two different histogram of two different root files. both histograms have same name and position in string i tried alot but it give me no error with empty output.

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 4, 2017, 5:17pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/6 "2017-12-04T17:17:32Z")

</div>

how i will define the draw function and how i can give command of overlap two histograms. actually both histogram extracted from different files have same name and position in string . if you want then i can attach these root files here.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [December 4, 2017, 5:21pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/7 "2017-12-04T17:21:04Z")

</div>

The typical procedure is below:

```auto
   ...
   //We get the pointers to the two histograms from different files.
   //Pointer names do not need to match histogram names.
   TH1D *h1 = (TH1D*)file->Get("angle_vs_amplitude_IB1"); 
   TH1D *h2 = (TH1D*)f->Get("angle_vs_amplitude_IB1");

   TCanvas * c11 = new TCanvas();
   c11->cd();
   //After creating the canvas and selecting it we can draw the histograms. 
   h1->Draw();
   //Use the option "SAME" to overlap the histograms.
   h2->Draw("SAME")
   ...

```

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 4, 2017, 5:57pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/8 "2017-12-04T17:57:06Z")

</div>

illegal pointer to class object h1 0x0 1934.  
\*\*\* Interpreter error recovered \*\*\*

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 4, 2017, 5:57pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/9 "2017-12-04T17:57:50Z")

</div>

it give me this error after making correction.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [December 4, 2017, 8:52pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/10 "2017-12-04T20:52:19Z")

</div>

Yes you should check if the file contains those histograms.

```auto
if (!h1 || !h2) {
   std::cerr << "ERROR: Unable to find histogram(s)!\n";
   std::cerr << "A listing of the files may be helpful:\n"
   f->ls();
   file->ls();
   return;
}
```

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 5, 2017, 11:03am UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/11 "2017-12-05T11:03:21Z")

</div>

thanks for your time. but its not working yet. i put these two root files in my public area of lxplus . work/i/isohail/public . sohail1\_OT\_2017, sohail2\_OT\_2017 , kindly check them bcz still am unable to understand why it give me this error  
with some include files NewTProfileMacro(void)  
{  
TFile \*file = new TFile(“sohail1\_OT\_2017.root”);  
TFile \*f = new TFile(“sohail2\_OT\_2017.root”);  
if(!file) {  
std::cout\<\<“FILE CAN’T BE OPENED”\<\<std::endl;  
}

file-\>cd(“prod/SensorType;1”);  
f-\>cd(“prod/SensorType;1”);  
float localx\_,rhlocalx\_,angle\_vs\_amplitude\_IB1;  
int i;  
TH1D _h1 = (TH1D_)file-\>Get(“angle\_vs\_amplitude\_IB1”); TH1D _h2 = (TH1D_)f-\>Get(“angle\_vs\_amplitude\_IB1”);  
TCanvas \* c11 = new TCanvas();  
c11-\>cd();

```
                  stringstream ss1;
            ss1<< i;
             string ss = "overlaping_profile_"+ss1.str()+".pdf";
        c11->Print(ss.c_str(),".pdf") ;
       h1->Draw();
       h2->Draw("same");
    if(!h1 || !h2) {

```

std::cerr \<\< “ERROR: Unable to find histogram(s)!\n”;  
std::cerr \<\< “A listing of the files may be helpful:\n”

f-\>ls();  
file-\>ls();  
return;  
}

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [December 5, 2017, 2:39pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/12 "2017-12-05T14:39:13Z")

</div>

> [@sohail](#):
>
> i put these two root files in my public area of lxplus . work/i/isohail/public . sohail1\_OT\_2017, sohail2\_OT\_2017 , kindly check them bcz still am unable to understand why it give me this error

Many of us are not at CERN. I know very little about this “lxplus”, but have inferred that they are computers at CERN and as such I doubt I have access to them.

> [@sohail](#):
>
> thanks for your time. but its not working yet.

How is it not working? Do you get an error? Are you expecting a different result?

You have included your code inline which makes it difficult to read. Could you follow the suggestions in the following post and edit your post accordingly? Also, it appears that your code is still not following the normal tabbing where items of the same scope are at the same indentation, this also make it difficult to read and can help identify mistakes.

> [@Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719/2):
>
> Remark on the way to post code snippets: When you post pieces of code on the forum enclose it with ``` . That makes your post much more readable (I systematically edit your posts that way) . So do: ``` // this is a C++ piece of code. ``` And it will display as: // this is a C++ piece of code.

You should compile this script until you have improved your programming skills so that your errors are caught (you have a line with a missing semicolon, there are missing statements, missing curly brackets…). Use the following command to compile your scripts and get messages about errors.

```auto
root [0] .L script.C+

```

Please read through all my comments in your script above as you still have not addressed all of these. Finally, some of your code is out of order. Checking if the histograms are valid after trying to do something with them seems counterproductive. Printing the canvas before drawing on it will also leave you with a blank canvas.

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 5, 2017, 3:51pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/13 "2017-12-05T15:51:29Z")

</div>

thanku so much for your time but it give me no errors but it also give me no output .

---

<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:** [December 5, 2017, 4:06pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/14 "2017-12-05T16:06:09Z")

</div>

In your afs public folder I see:

```auto
$ ls /afs/cern.ch/user/i/isohail/public
ATree.cc Invarientmass1.cc Invarientmass2.cc iqra.cc MASS.cc M_en.cc  
mybranches.root ReadTree.cc TLorentz.cc
$ 

```

`mybranches.root` is your data file ?

---

<div class="post-metadata">

**Author:** ![sohail](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/6de8d8/32.png) [@sohail](https://root-forum.cern.ch/u/sohail)\
**Post date:** [December 6, 2017, 5:06pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/15 "2017-12-06T17:06:58Z")

</div>

Thanku ksmith for your help now its working.

---

<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:** [December 20, 2017, 5:07pm UTC](https://root-forum.cern.ch/t/how-to-overlap-two-different-histograms-of-two-different-root-files-but-its-not-working/27170/16 "2017-12-20T17:07:06Z")

</div>

Closed on December 20, 2017, 5:07pm UTC

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