Filling variable histogram name

Dear Experts,

I have a root tree and I made a class from the tree getting file.C, file.h
In the file.C I’m reading input root files and from each file I get the tree and try to fill histograms with variable name depending in some cuts. For every cut I want to fill histogram with different name. I get error

warning: invalid memory pointer passed to a callee:
if(f_pfmet >j )h_bkg_num[j]->Fill(f_pfmet,f_weight);

here is my code

#define Roc_vrs_Met_cxx
#include "Roc_vrs_Met.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TLorentzVector.h>
#include <TNtuple.h>
#include <TSpline.h>
#include <TMath.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdlib>
#include <stdio.h>

using namespace std;

void Roc_vrs_Met::Loop()
{

   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();

   Long64_t nbytes = 0, nb = 0;
 
 
 //---
 //Reading bkg files
  
  std::vector<string> Vbkg;
  std::vector<string> Vsig;
  std::ifstream infile;
  infile.open("filelist_bkg_signal.txt");
  string inputfilename;

  //creating the vector with roottuples  
  while(std::getline(infile,inputfilename)){
    Vbkg.push_back(inputfilename);
     }
  infile.close();
   
    //Histos
   
TH1F *h_signal_den = new TH1F("","",1000, 0, 1000);
TH1F *h_bkg_den  =   new TH1F("","",1000, 0, 1000);

TH1F *h_signal_num[10];
TH1F *h_bkg_num[10];
char *histname = new char[10];
char *histname2 = new char[10];
int nbins=1000,nhistos=200;
float xmin=0,xmax=1000;

for (int i=20; i<=nhistos; i+=20) {
sprintf(histname,"h_signal_num%d",i);
sprintf(histname2,"h_bkg_num%d",i);
 h_signal_num[i] = new TH1F(histname,"",nbins,xmin,xmax); 
 h_bkg_num[i] = new TH1F(histname2,"",nbins,xmin,xmax); 
}

 Float_t f_weight=-999, f_Z2mass=-999, f_pfmet=-999;

 //loop  
   
  for(unsigned int ifile=0; ifile<(unsigned int)Vbkg.size(); ifile++){

     char dataset[328];
     sprintf(dataset,"%s",Vbkg.at(ifile).c_str());
     cout << "Root-ple= " << dataset << endl;
     TFile *file = TFile::Open(dataset);
    cout<<"file opeed"<<endl;
    TTree *tree = (TTree*)file->Get("ReducedTree");
    unsigned int ntree = tree->GetEntries();
    cout<<"tree opend"<<endl;
    
 tree->SetBranchAddress("f_pfmet",&f_pfmet);
 tree->SetBranchAddress("f_weight",&f_weight);
 tree->SetBranchAddress("f_Z2mass",&f_Z2mass);
 

 //for(unsigned int iev=0; iev<ntree; ++iev){
 for(unsigned int iev=0; iev<10; ++iev){
    tree->GetEntry(iev);

cout<<"entry = "<<iev<<" met = "<<f_pfmet<<endl;

for(int j=20; j<=200; j+=20){
if(f_pfmet >j )h_bkg_num[j]->Fill(f_pfmet,f_weight);    
   }      
 }//iev   
}//bkg files

 TFile *out = new TFile("out.root","RECREATE");
 out->cd(); 
  for(int j=50; j<200; j+=50){
h_bkg_num[j]->Write();
} 
out->Close();
 
cout<<"Done" <<endl;      
   
}

I appreciate if someone help me to solve this problem.

Valid indices for your “h_signal_num” and “h_bkg_num” arrays are from 0 to 9 (and your “for” loops run from 20 or 50 to 200).

Also, use: char histname[20], histname2[20];

Thanks a lot :smile:

Sorry me again

If I include another set of histograms I have the same name of histograms in the output root file. I just divided the input files to signal and background
the updated code

#define Roc_vrs_Met_cxx
#include "Roc_vrs_Met.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TLorentzVector.h>
#include <TNtuple.h>
#include <TSpline.h>
#include <TMath.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdlib>
#include <stdio.h>

using namespace std;

void Roc_vrs_Met::Loop()
{
   if (fChain == 0) return;
   Long64_t nentries = fChain->GetEntriesFast();
   Long64_t nbytes = 0, nb = 0;
  
 //---
 //Reading bkg files
  
  std::vector<string> Vbkg;
  std::vector<string> Vsig;
  std::ifstream infile;
  infile.open("filelist_bkg_signal.txt");
  string inputfilename;

  //creating the vector with roottuples  
  while(std::getline(infile,inputfilename)){
    Vbkg.push_back(inputfilename);
     }
  infile.close();
  
    //Histos
   
TH1F *h_signal_den = new TH1F("h_signal_den","",1000, 0, 1000);
TH1F *h_bkg_den  =   new TH1F("h_bkg_den ","",1000, 0, 1000);


TH1F *h_signal_num[20];
TH1F *h_bkg_num[20];
char *histname = new char[200];
char *histname2 = new char[200];
int nbins=1000,nhistos=100;
float xmin=0,xmax=1000;

for (int i=10; i<=nhistos; i+=10) {
sprintf(histname,"h_signal_num%d",i);
sprintf(histname2,"h_bkg_num%d",i);
 h_signal_num[i] = new TH1F(histname,"",nbins,xmin,xmax); 
 h_bkg_num[i] = new TH1F(histname2,"",nbins,xmin,xmax); 
}

 Float_t f_weight=-999, f_Z2mass=-999, f_pfmet=-999;

 //loop  
   
  for(unsigned int ifile=0; ifile<(unsigned int)Vbkg.size(); ifile++){

char dataset[328];
sprintf(dataset,"%s",Vbkg.at(ifile).c_str());
cout << "Root-ple= " << dataset << endl;
TFile *file = TFile::Open(dataset);
cout<<"file opeed"<<endl;
TTree *tree = (TTree*)file->Get("HZZ4LeptonsAnalysisReduced");
unsigned int ntree = tree->GetEntries();
cout<<"tree opend"<<endl;
    
 tree->SetBranchAddress("f_pfmet",&f_pfmet);
 tree->SetBranchAddress("f_weight",&f_weight);
 tree->SetBranchAddress("f_Z2mass",&f_Z2mass);
 

 if (Vbkg.at(ifile).find("2HDMplusa_MH3_500")<200){
cout<<">>>> signal file "<<endl;

for(unsigned int iev=0; iev<10; ++iev){
    tree->GetEntry(iev);

cout<<"entry = "<<iev<<" met = "<<f_pfmet<<endl;

h_signal_den->Fill(f_pfmet,f_weight);

for(int j=10; j<=100; j+=10){
if(f_pfmet >j )h_signal_num[j]->Fill(f_pfmet,f_weight);    
   }      
 }//iev 
}//signal file 
else {

cout<<"bkg file >>>>>>"<<endl;
 //for(unsigned int iev=0; iev<ntree; ++iev){
 for(unsigned int iev=0; iev<10; ++iev){
    tree->GetEntry(iev);

cout<<"entry = "<<iev<<" met = "<<f_pfmet<<endl;

h_bkg_den->Fill(f_pfmet,f_weight);

for(int j=10; j<=100; j+=10){
if(f_pfmet >j )h_bkg_num[j]->Fill(f_pfmet,f_weight);    
   }      
 }//iev 
 }  
}//bkg files


 TFile *out = new TFile("out.root","RECREATE");
 out->cd(); 
 
 h_bkg_den->Write();
 h_signal_den->Write();
for(int j=10; j<=100; j+=10){
h_bkg_num[j]->Write();
h_signal_num[j]->Write();
} 
out->Close();
 
cout<<"Done" <<endl;      
   
}

the output root file has all the histograms with signal name and the bkg histograms not there, the list of root file histograms is
TFile** out.root
TFile* out.root
KEY: TH1F bkg_den;1
KEY: TH1F h_signal_den;1
KEY: TH1F h_signal_num30;2
KEY: TH1F h_signal_num30;1
KEY: TH1F h_signal_num10;1
KEY: TH1F h_signal_num40;2
KEY: TH1F h_signal_num40;1
KEY: TH1F h_signal_num20;1
KEY: TH1F h_signal_num50;2
KEY: TH1F h_signal_num50;1
KEY: TH1F h_signal_num60;2
KEY: TH1F h_signal_num60;1
KEY: TH1F h_signal_num70;2
KEY: TH1F h_signal_num70;1
KEY: TH1F h_signal_num80;2
KEY: TH1F h_signal_num80;1
KEY: TH1F h_signal_num90;2
KEY: TH1F h_signal_num90;1
KEY: TH1F h_signal_num100;2
KEY: TH1F h_signal_num100;1
KEY: TH1F h_bkg_num90;1
KEY: TH1F h_bkg_num100;1

Sorry me again.
If I include another set of histograms I have the same name of histograms in the output root file.It seems to be overwritten. I just divided the input files to signal and background
the updated code

#define Roc_vrs_Met_cxx
#include "Roc_vrs_Met.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <TLorentzVector.h>
#include <TNtuple.h>
#include <TSpline.h>
#include <TMath.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdlib>
#include <stdio.h>

using namespace std;

void Roc_vrs_Met::Loop()
{
if (fChain == 0) return;
   Long64_t nentries = fChain->GetEntriesFast();
   Long64_t nbytes = 0, nb = 0;
  
 //---
 //Reading bkg files
  
std::vector<string> Vbkg;
  std::vector<string> Vsig;
  std::ifstream infile;
  infile.open("filelist_bkg_signal.txt");
  string inputfilename;

  //creating the vector with roottuples  
  while(std::getline(infile,inputfilename)){
    Vbkg.push_back(inputfilename);
     }
  infile.close();
  
    //Histos
TH1F *h_signal_den = new TH1F("h_signal_den","",1000, 0, 1000);
TH1F *h_bkg_den  =   new TH1F("h_bkg_den ","",1000, 0, 1000);


TH1F *h_signal_num[20];
TH1F *h_bkg_num[20];
char *histname = new char[200];
char *histname2 = new char[200];
int nbins=1000,nhistos=100;
float xmin=0,xmax=1000;

for (int i=10; i<=nhistos; i+=10) {
sprintf(histname,"h_signal_num%d",i);
sprintf(histname2,"h_bkg_num%d",i);
h_signal_num[i] = new TH1F(histname,"",nbins,xmin,xmax); 
 h_bkg_num[i] = new TH1F(histname2,"",nbins,xmin,xmax); 
}

 Float_t f_weight=-999, f_Z2mass=-999, f_pfmet=-999;

 //loop  
   
  for(unsigned int ifile=0; ifile<(unsigned int)Vbkg.size(); ifile++){

char dataset[328];
sprintf(dataset,"%s",Vbkg.at(ifile).c_str());
cout << "Root-ple= " << dataset << endl;
TFile *file = TFile::Open(dataset);

cout<<"file opeed"<<endl;
TTree *tree = (TTree*)file->Get("HZZ4LeptonsAnalysisReduced");
unsigned int ntree = tree->GetEntries();
cout<<"tree opend"<<endl;
    
 tree->SetBranchAddress("f_pfmet",&f_pfmet);
 tree->SetBranchAddress("f_weight",&f_weight);
 tree->SetBranchAddress("f_Z2mass",&f_Z2mass);
 

 if (Vbkg.at(ifile).find("2HDMplusa_MH3_500")<200){
cout<<">>>> signal file "<<endl;

for(unsigned int iev=0; iev<10; ++iev){
tree->GetEntry(iev);

cout<<"entry = "<<iev<<" met = "<<f_pfmet<<endl;

h_signal_den->Fill(f_pfmet,f_weight);

for(int j=10; j<=100; j+=10){
if(f_pfmet >j )h_signal_num[j]->Fill(f_pfmet,f_weight);    
   }      
 }//iev 
}//signal file 
else {
cout<<"bkg file >>>>>>"<<endl;
 //for(unsigned int iev=0; iev<ntree; ++iev){
 for(unsigned int iev=0; iev<10; ++iev){
    tree->GetEntry(iev);

cout<<"entry = "<<iev<<" met = "<<f_pfmet<<endl;

h_bkg_den->Fill(f_pfmet,f_weight);

for(int j=10; j<=100; j+=10){
if(f_pfmet >j )h_bkg_num[j]->Fill(f_pfmet,f_weight);    
   }      
 }//iev 
 }  
}//bkg files


 TFile *out = new TFile("out.root","RECREATE");
 out->cd(); 
 
 h_bkg_den->Write();
 h_signal_den->Write();
for(int j=10; j<=100; j+=10){
h_bkg_num[j]->Write();
h_signal_num[j]->Write();
} 
out->Close();
 
cout<<"Done" <<endl;      
   
}

the output root file has all the histograms with signal name and the bkg histograms not there, the list of histograms in the root file is
TFile** out.root
TFile* out.root
KEY: TH1F bkg_den;1
KEY: TH1F h_signal_den;1
KEY: TH1F h_signal_num30;2
KEY: TH1F h_signal_num30;1
KEY: TH1F h_signal_num10;1
KEY: TH1F h_signal_num40;2
KEY: TH1F h_signal_num40;1
KEY: TH1F h_signal_num20;1
KEY: TH1F h_signal_num50;2
KEY: TH1F h_signal_num50;1
KEY: TH1F h_signal_num60;2
KEY: TH1F h_signal_num60;1
KEY: TH1F h_signal_num70;2
KEY: TH1F h_signal_num70;1
KEY: TH1F h_signal_num80;2
KEY: TH1F h_signal_num80;1
KEY: TH1F h_signal_num90;2
KEY: TH1F h_signal_num90;1
KEY: TH1F h_signal_num100;2
KEY: TH1F h_signal_num100;1
KEY: TH1F h_bkg_num90;1
KEY: TH1F h_bkg_num100;1

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