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

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();

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 …

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

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.

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.

The typical procedure is below:

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

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

it give me this error after making correction.

Yes you should check if the file contains those histograms.

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;
}

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;
}

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.

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.

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.

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.

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

In your afs public folder I see:

$ 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 ?

Thanku ksmith for your help now its working.

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