Multiple root files to be plotted in one histogram

Help me fixing these errors, am trying to plot the 3 root files on 1 graph
but here is the error and warning am getting!!

Comparison $ root FConvo.C
   ------------------------------------------------------------------
  | Welcome to ROOT 6.30/04                        https://root.cern |
  | (c) 1995-2024, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Jan 31 2024, 08:17:06                      |
  | From heads/master@tags/v6-30-04                                  |
  | With Apple clang version 15.0.0 (clang-1500.3.9.4)               |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] 
Processing FConvo.C...
In file included from input_line_8:1:
/Users/Desktop/Comparison/FConvo.C:11:4: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
   sprintf(fname1,"result.root");
   ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:180:1: note: 'sprintf' has been explicitly marked deprecated here
__deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.")
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:218:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
In file included from input_line_8:1:
/Users/Desktop/Comparison/FConvo.C:12:4: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
   sprintf(fname2,"result1.root");
   ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:180:1: note: 'sprintf' has been explicitly marked deprecated here
__deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.")
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:218:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
In file included from input_line_8:1:
/Users/Desktop/Comparison/FConvo.C:13:4: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
   sprintf(fname3,"result2.root");
   ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:180:1: note: 'sprintf' has been explicitly marked deprecated here
__deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.")
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:218:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
In file included from input_line_8:1:
/Users/Desktop/Comparison/FConvo.C:29:4: error: use of undeclared identifier 'h1'
   h1 = (TH1D*)file1->Get("ND_eDep");
   ^
/Users/Desktop/Comparison/FConvo.C:30:4: error: use of undeclared identifier 'h2'
   h2 = (TH2D*)file2->Get("ND_eDep");
   ^
/Users/Desktop/Comparison/FConvo.C:31:4: error: use of undeclared identifier 'h3'
   h3 = (TH2D*)file3->Get("ND_eDep");
   ^
/Users/Desktop/Comparison/FConvo.C:39:21: error: use of undeclared identifier 'h1'
     Double_t cent= h1->GetBinCenter(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:40:21: error: use of undeclared identifier 'h1'
     Int_t counts = h1->GetBinContent(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:44:30: error: use of undeclared identifier 'myRand'
           Double_t var_rand=myRand->Gaus(cent,bin_sigma);
                             ^
/Users/Desktop/Comparison/FConvo.C:55:21: error: use of undeclared identifier 'h2'
     Double_t cent= h2->GetBinCenter(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:56:21: error: use of undeclared identifier 'h2'
     Int_t counts = h2->GetBinContent(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:60:30: error: use of undeclared identifier 'myRand'
           Double_t var_rand=myRand->Gaus(cent,bin_sigma);
                             ^
/Users/Desktop/Comparison/FConvo.C:71:21: error: use of undeclared identifier 'h3'
     Double_t cent= h3->GetBinCenter(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:72:21: error: use of undeclared identifier 'h3'
     Int_t counts = h3->GetBinContent(k);
                    ^
/Users/Desktop/Comparison/FConvo.C:76:24: error: use of undeclared identifier 'myRand'
     Double_t var_rand=myRand->Gaus(cent,bin_sigma);
                       ^
/Users/Desktop/Comparison/FConvo.C:89:16: error: use of undeclared identifier 'h1'
 leg->AddEntry(h1,"Material1","L");
               ^
/Users/Desktop/Comparison/FConvo.C:90:16: error: use of undeclared identifier 'h2'
 leg->AddEntry(h2,"Material2","L");
               ^
/Users/Desktop/Comparison/FConvo.C:91:16: error: use of undeclared identifier 'h2'
 leg->AddEntry(h2,"Material3","L"); 
               ^
root [1] 

The code is this
FConvo.C (2.1 KB)

void FConvo() {
   double var_rand = 0, FWHM = 0, bin_sigma,cont;
   auto myRand = new TRandom();

   TFile *file1 = new TFile("result.root");
   TFile *file2 = new TFile("result1.root");
   TFile *file3 = new TFile("result2.root");

   Int_t binN=500;

   auto NearDet_edep1 = new TH1D("NearDet_edep1",";Energy (MeV);Counts keV",binN,0,600.0);
   auto NearDet_edep2 = new TH1D("NearDet_edep2",";Energy (MeV);Counts keV",binN,0,600.0);
   auto NearDet_edep3 = new TH1D("NearDet_edep3",";Energy (MeV);Counts keV",binN,0,600.0);

   TH1D *h1 = (TH1D*)file1->Get("ND_eDep");
   TH2D *h2 = (TH2D*)file2->Get("ND_eDep");
   TH2D *h3 = (TH2D*)file3->Get("ND_eDep");

   for (int k = 0; k<binN; k++) {
      FWHM = 46/1000.0; // in keV
      bin_sigma = FWHM /2.35;
      Double_t cent= h1->GetBinCenter(k);
      Int_t counts = h1->GetBinContent(k);
      for (int l=0; l<counts; l++) {
         Double_t var_rand=myRand->Gaus(cent,bin_sigma);
         NearDet_edep1->Fill(var_rand);
      }
   }

   for (int k = 0; k<binN; k++) {
      FWHM = 46/1000.0; // in keV
      bin_sigma = FWHM /2.35;
      Double_t cent= h2->GetBinCenter(k);
      Int_t counts = h2->GetBinContent(k);
      for (int l=0; l<counts; l++) {
         Double_t var_rand=myRand->Gaus(cent,bin_sigma);
         NearDet_edep2->Fill(var_rand);
      }
   }


  for (int k = 0; k<binN; k++) {
      FWHM = 46/1000.0; // in keV
      bin_sigma = FWHM /2.35;
      Double_t cent= h3->GetBinCenter(k);
      Int_t counts = h3->GetBinContent(k);
      for (int l=0; l<counts; l++) {
         Double_t var_rand=myRand->Gaus(cent,bin_sigma);
         NearDet_edep3->Fill(var_rand);
      }
   }

   auto c1 = new TCanvas("c1", "",1800,1400);
   NearDet_edep1->Draw(); NearDet_edep1->SetStats(0);
   NearDet_edep2->Draw("SAME"); NearDet_edep2->SetStats(0);
   NearDet_edep3->Draw("SAME"); NearDet_edep3->SetStats(0);

   auto leg = new TLegend(0.55,0.7,0.85,0.85);
   leg->AddEntry(h1,"Material1","L");
   leg->AddEntry(h2,"Material2","L");
   leg->AddEntry(h2,"Material3","L");
   leg->Draw();
}

Your error means the histogram h1 is not defined. Very likely the histogram ND_eDep is not in the file result.root

Yes I tried to fix it, and it worked
but no visible visualisation of combined root files in one histogram

To debug further we will need the root files.

The root files are as follows
result.root (34.5 KB)
result1.root (34.7 KB)
result2.root (34.3 KB)

Your files contain each a single TTree called ntuple1. There is no TH1D.

How can I solve the issue, so that the histograms in each root files is going to be present for comparison!!
This is the current code
FConvo.C (2.0 KB)

You should check the code which created the ROOT files. You cannot do anything from your analysis macro. The files do not contain any TH1D and you try to access some… The only object in your root file is a TTree with one variable. Inspect the file with TBrowser (or use rootls) and you will understand what I mean.

Yes, I understand
Actually for simulation am using geant4 this is the code for obtaining root file
G4AnalysisManager* mgr = G4AnalysisManager::Instance();

mgr->SetVerboseLevel(0);
mgr->SetNtupleMerging(true);

mgr->CreateNtuple(“Ntuple1”, “GS20”);
mgr->CreateNtupleDColumn(“EdepCryst1”);
mgr->FinishNtuple();

G4String fileName = “result.root”;
mgr->OpenFile(fileName);

the code look like this!

You should ask the Geant4 Forum.

Thanks for the insights
I was able to change TH1D, for TTree and it worked!