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

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 for.)

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.)

//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?
1 Like