Error: illegal pointer to class object

Sorry the code is a bit long. I tried all day trying to figure out if there is a way to fix it but I just can’t seem to find a way. The input files is just a tag_1_pythia.root files for different mass values resonances. The files are too big to be attached.

The Error:

Error: illegal pointer to class object HistPT[k] 0x0 1369 Analysis.C:211:
*** Interpreter error recovered ***

The problem seems to be created when the Selection function is called a second time at mass 500.
Please let me know if anyone catches anything. I have stared at the code for quite a while now.

[code]#define Analysis_cxx
#include “Analysis.h”
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include
#include
#include “TLorentzVector.h”
#include “TSystem.h”
#include “TRandom.h”
#include “TRandom3.h”

#include
#include
#include <TROOT.h>
#include <TRint.h>
#include <TCanvas.h>
#include <TH1.h>
#include <TH2.h>
#include <TH1D.h>
#include <TF1.h>
#include <TGraph.h>
#include
#include
#include
#include <TFile.h>
#include “TGraphErrors.h”

using namespace std;

// In a ROOT session, you can do:
// Root > .L libExRootAnalysis.so
// Root > .L Analysis.C
// Root > Analysis t
// Root > t.GetEntry(12); // Fill t data members with entry number 12
// Root > t.Show(); // Show values of entry 12
// Root > t.Show(16); // Read and show values of entry 16
// Root > t.Loop(); // Loop on all entries
//

// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
// fChain->SetBranchStatus("*",0); // disable all branches
// fChain->SetBranchStatus(“branchname”,1); // activate branchname
// METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
//by b_branchname->GetEntry(ientry); //read only this branch
// gSystem->Load(“libExRootAnalysis.so”);

//Declaration of global variables
TFile *Result[20];
TH1D *Hist[20];
TH1D *Hist1[20];
TH1D *Hist2[20];
TH1D *Hist3[20];

TFile *ResultPT[20];
TH1D *HistPT[20];
TH1D *HistPTSelect[20];
TH1D *HistPTSelectSmeared[20];
TH1D *HistPTSelectSmearedTruncated[20];

TFile *ResultY[20];
TH1D *HistY[20];
TH1D *HistYSelect[20];
TH1D *HistYSelectSmeared[20];
TH1D *HistYSelectSmearedTruncated[20];

TFile *ResultYStar[20];
TH1D *HistYStar[20];
TH1D *HistYStarSelectSmearedTruncated[20];

TFile *ResultPTSub[20];
TH1D *HistPTSub[20];
TH1D *HistPTSelectSub[20];
TH1D *HistPTSelectSmearedSub[20];
TH1D *HistPTSelectSmearedTruncatedSub[20];

TFile *ResultYSub[20];
TH1D *HistYSub[20];
TH1D *HistYSelectSub[20];
TH1D *HistYSelectSmearedSub[20];
TH1D *HistYSelectSmearedTruncatedSub[20];

//TFile *ResultYStarSub[20];
//TH1D *HistYStarSub[20];
//TH1D *HistYStarSelectSmearedTruncatedSub[20];

//intemediate check:
TFile *ResultPTY[20];
TH1D *HistPTYPT[20];
TH1D *HistPTYY[20];

TFile *ResultPTYsub[20];
TH1D *HistPTYPTsub[20];
TH1D *HistPTYYsub[20];

double Acceptance[20];
double CrossSection[20];

//double Analysis::Selection(int M,int k);

double Analysis::Selection(int M,int k)
{

/*Hist[k]= new TH1D(“Mass Resonance”,“Mass Resonance”,1000,-1000,5000);
Hist1[k]= new TH1D(“Mass Resonance+Selection”,“Mass Resonance”,1000,-1000,5000);
Hist2[k]= new TH1D(“Mass Resonance+Selection+1smearing effect”,“Mass Resonance+smearing effect”,1000,-3000,2000);
Hist3[k]= new TH1D(“Mass Resonance+Selection+smearing effect+truncated”,“Mass Resonance+smearing effect+truncated”,1000,-1000,5000);
*/

Hist1[k]->GetXaxis()->SetTitle("Mass(GeV");
Hist1[k]->GetYaxis()->SetTitle("Events");
Hist2[k]->GetXaxis()->SetTitle("Mass(GeV");
Hist2[k]->GetYaxis()->SetTitle("Events");
Hist3[k]->GetXaxis()->SetTitle("Mass(GeV");

// Hist1[k].SetNameTitle(

//    Hist3->GetYaxis-()>SetTitle("Events");
std::filebuf outfile;
outfile.open("Smearing.txt",std::ios::out);
ostream os (&outfile);
TRandom r;

//Loading the graph
TFile *pdata = TFile::Open("CrossSection.root");
TGraph *mvc =(TGraph*)pdata->Get("mvc");
// vector<int> qIdx;
Long64_t nentries = fChain->GetEntriesFast();
vector<int> qIdx;

Long64_t nbytes = 0, nb = 0;
TFile *Selection=new TFile(“SelectionHistogram.root”,“RECREATE”);

for (Long64_t jentry=0; jentry<1000;jentry++)
//nentries
{
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
vector qIdx; //qIdx is the list to store the i
// cout<<"qIdx.at(i): ";

   for (int i=0; i<kMaxParticle; i++)
   {

       if (abs(Particle_PID[i])<7 || Particle_PID[i]==21)
        {
           if (Particle_Status[i]==1)
            //particle status =1 are final state particles
            {
                qIdx.push_back(i);
               // cout<<qIdx.at(i)<<"  ";
            }
        }
   }
    cout<<"Part one"<<endl;
    
      // cout << qIdx.size()<<endl;
    
    //Removing events that have less than 2 particles
    cout <<"Size of qIdx"<<qIdx.size()<<endl;
    if  (qIdx.size() <2)
    {
        continue; //break out of the for loop for the entries
    }

    cout<<"Part two"<<endl;


        int leadingidOri, subleadingidOri;
        leadingidOri=qIdx.at(1);
        subleadingidOri=qIdx.at(0);
    
      //  Particle_PT[leadingid]=0;
         vector<int> subqIdx;
    
    
    //Create the subleadingOriginal and leading IDOrginal for each event before any selections
    for (int i=0; i<qIdx.size(); i++)
    {
        if (Particle_PT[qIdx.at(i)]>Particle_PT[leadingidOri])
        {
            subleadingidOri=leadingidOri;
            leadingidOri=qIdx.at(i);
        }
    }
    
    //After the leading and subleading particle is found, make the lorentz vector objects.
    
    TLorentzVector j1Ori;
    TLorentzVector j2Ori;
    
    j1Ori.SetPtEtaPhiE(Particle_PT[leadingidOri],Particle_Eta[leadingidOri],Particle_Phi[leadingidOri],Particle_E[leadingidOri]);
    j2Ori.SetPtEtaPhiE(Particle_PT[subleadingidOri],Particle_Eta[subleadingidOri],Particle_Phi[subleadingidOri],Particle_E[subleadingidOri]);
    
    //Actual event construction
    TLorentzVector resonanceOri = j1Ori+j2Ori;
    
    cout<<"Part five"<<endl;
    
    cout<<j1Ori.M()<<"  "<<j2Ori.M()<<"   "<<resonanceOri.M()<<endl;
    
    cout<<"Part five1"<<endl;
    
    Hist[k]->Fill(resonanceOri.M());
    
    HistPT[k]->Fill(Particle_PT[leadingidOri]);
    HistY[k]->Fill(Particle_Rapidity[leadingidOri]);
    HistYStar[k]->Fill(abs(Particle_Rapidity[leadingidOri]-Particle_Rapidity[subleadingidOri])*0.5);
    
    HistPTSub[k]->Fill(Particle_PT[subleadingidOri]);
    HistYSub[k]->Fill(Particle_Rapidity[subleadingidOri]);
    
    //Applying the Event Selection Criteria(Only for Particle PT)
   // int leadingidPT, subleadingidPT;
   // leadingidPT=qIdx.at(1);
   // subleadingidPT=qIdx.at(0);
    

        
        if (Particle_PT[leadingidOri]>50)
        {
            HistPTSelect[k]->Fill(Particle_PT[leadingidOri]);
        }


        if (Particle_PT[subleadingidOri]>50)
        {
            HistPTSelectSub[k]->Fill(Particle_PT[subleadingidOri]);
        }


    //Applying Selection Criteria(Only for Rapidity)

        
        if (Particle_Rapidity[leadingidOri]< 2.8 && Particle_Rapidity[leadingidOri]>-2.8)
        {        HistYSelect[k]->Fill(Particle_Rapidity[leadingidOri]);
      //  else {subqIdx.push_back(i);}
        }
    
        if (Particle_Rapidity[subleadingidOri]< 2.8 && Particle_Rapidity[subleadingidOri]>-2.8)
        {
                HistYSelectSub[k]->Fill(Particle_Rapidity[subleadingidOri]);
        }
    
    
    if (Particle_Rapidity[leadingidOri]< 2.8 && Particle_Rapidity[leadingidOri]>-2.8 && Particle_PT[leadingidOri]>50)
    {
        HistPTYPT[k]->Fill(Particle_PT[leadingidOri]);
        HistPTYY[k]->Fill(Particle_Rapidity[leadingidOri]);
    }
    
    if (Particle_Rapidity[subleadingidOri]< 2.8 && Particle_Rapidity[subleadingidOri]>-2.8 && Particle_PT[subleadingidOri]>50)
    {
        HistPTYPTsub[k]->Fill(Particle_PT[subleadingidOri]);
        HistPTYYsub[k]->Fill(Particle_Rapidity[subleadingidOri]);
    }
    
    
    //Applying the selection criteria rapidity + PT
    
    int leadingid, subleadingid;
     leadingid=qIdx.at(1);
     subleadingid=qIdx.at(0);
    
    

        for (int i=0; i<qIdx.size(); i++)
            {
               //printing out the particle PID
               /*
               cout<<"Entry number: "<<jentry<<"Particle: "<<qIdx.at(i)<<"Particle PID" <<Particle_PID[qIdx.at(i)]<<endl;
               */
               
               //Applying selections Criteria 1 and 2
                cout<<"Entry number: "<<jentry<<"qIdx: "<<i<<"Particle PT: "<< Particle_PT[qIdx.at(i)]<<"Particle rapidity: "<<Particle_Rapidity[qIdx.at(i)]<<endl;
                
                if (Particle_PT[qIdx.at(i)]<=50 || Particle_Rapidity[qIdx.at(i)]>= 2.8 || Particle_Rapidity[qIdx.at(i)]<=-2.8)
                {continue;}
                else {subqIdx.push_back(i);}
                
                //Finding subleading and leading jets within the particles that fullfills selection 1 and 2
                if (Particle_PT[qIdx.at(i)]>Particle_PT[leadingid])
                {
                        subleadingid=leadingid;
                        leadingid=qIdx.at(i);
                }

            }
    

    
    cout<<"Part three"<<endl;

        //Checking if there are still at least 2 jets in the subqIdx vector after selection 1+2
    cout <<"Size of subqIdx: "<<subqIdx.size()<<endl;
    
 /*   for (int i=0, i<subqIdx.size(), i++)
    {
        cout<<"entry number: "<<jentry<<" #in subqIdx: "<<i<<" Particle PT: "<<Particle_PT[i]<<" Particle Rapidity: "<<Particle_Rapidity[i]<<endl;
    }
   */
    if (subqIdx.size()<2)
        {
            continue;
        }
    
    cout<<"Part four"<<endl;

        //Apply selection criteria 3 on subqIdx particles
        if (abs(Particle_Rapidity[leadingid]-Particle_Rapidity[subleadingid])*0.5>=0.6)
        {
            continue;//Breaking out of the entry loop, discarding the whole event.
        }
    
    
    
    
    
    TLorentzVector j1;
    TLorentzVector j2;

            j1.SetPtEtaPhiE(Particle_PT[leadingid],Particle_Eta[leadingid],Particle_Phi[leadingid],Particle_E[leadingid]);
            j2.SetPtEtaPhiE(Particle_PT[subleadingid],Particle_Eta[subleadingid],Particle_Phi[subleadingid],Particle_E[subleadingid]);

    //Actual event construction
        TLorentzVector resonance = j1+j2;
    cout<<"Part five"<<endl;

        cout<<j1.M()<<"  "<<j2.M()<<"   "<<resonance.M()<<endl;
   
    cout<<"Part five1"<<endl;

    
    
    Hist1[k]->Fill(resonance.M());



   //Smearing step 2
        //Declaration
    double S, mass;
    double sigma1;
    //double gaus_sig;

    
    cout<<"Part five2"<<endl;

    cout <<mvc->Eval(resonance.M())<<endl;
    
    sigma1=mvc->Eval(resonance.M());
    cout<<"Sigma1: "<<sigma1<<endl;
    cout<<"mass: "<<resonance.M()<<endl;
    cout<<"Part five2"<<endl;

 //   cout<<"sigma: "sigma<<endl;
 //   cout<<"Part five3"<<endl;
    mass = resonance.M();
    double gaus_sig=r.Gaus(0,sigma1*mass);
    cout<<"R.Gaus sig=1: "<<r.Gaus(0,1)<<"r.gaus sig=sigma1*mass: "<<r.Gaus(0,sigma1*mass)<<endl;
    S=mass+gaus_sig;
    
    os<<"S: "<<S<<"Mass: "<<mass<<"Gaus Sigma: "<<gaus_sig<<"Sigma: "<<sigma1*mass<<endl;
    
 //   assert(gaus_sig < 0);
    Hist2[k]->Fill(S);
    HistPTSelectSmeared[k]->Fill(Particle_PT[leadingid]);
    HistYSelectSmeared[k]->Fill(Particle_Rapidity[leadingid]);
    
    HistPTSelectSmearedSub[k]->Fill(Particle_PT[leadingid]);
    HistYSelectSmearedSub[k]->Fill(Particle_Rapidity[leadingid]);
    
    cout<<"Part six"<<endl;

    



// truncation: Removing points Mq* below and above a certain point

if (S>=0.8*M && S<=1.2*M)
{   Hist3[k]->Fill(S);
    HistPTSelectSmearedTruncated[k]->Fill(Particle_PT[leadingid]);
    HistYSelectSmearedTruncated[k]->Fill(Particle_Rapidity[leadingid]);
    HistYStarSelectSmearedTruncated[k]->Fill(abs(Particle_Rapidity[leadingid]-Particle_Rapidity[subleadingid])*0.5);
    
    HistPTSelectSmearedTruncatedSub[k]->Fill(Particle_PT[subleadingid]);
    HistYSelectSmearedTruncatedSub[k]->Fill(Particle_Rapidity[subleadingid]);
}

}

/*
//Drawing everything
TCanvas *c= new TCanvas();
Hist1[k]->Draw();
TCanvas *c1 =new TCanvas();
Hist2[k]->Draw();
TCanvas c2 = new TCanvas();
Hist3[k]->Draw();
/

outfile.close();
Hist[k]->Write();
Hist1[k]->Write();
Hist2[k]->Write();
Hist3[k]->Write();
Result[k]->Write();


HistPT[k]->Write();
HistPTSelect[k]->Write();
HistPTSelectSmeared[k]->Write();
HistPTSelectSmearedTruncated[k]->Write();
ResultPT[k]->Write();

HistY[k]->Write();
HistYSelect[k]->Write();
HistYSelectSmeared[k]->Write();
HistYSelectSmearedTruncated[k]->Write();
ResultY[k]->Write();

HistPTYPT[k]->Write();
HistPTYY[k]->Write();
ResultPTY[k]->Write();

HistPTYPTsub[k]->Write();
HistPTYYsub[k]->Write();
ResultPTYsub[k]->Write();

HistPTSub[k]->Write();
HistPTSelectSub[k]->Write();
HistPTSelectSmearedSub[k]->Write();
HistPTSelectSmearedTruncatedSub[k]->Write();
ResultPTSub[k]->Write();

HistYSub[k]->Write();
HistYSelectSub[k]->Write();
HistYSelectSmearedSub[k]->Write();
HistYSelectSmearedTruncatedSub[k]->Write();
ResultYSub[k]->Write();

HistYStar[k]->Write();
HistYStarSelectSmearedTruncated[k]->Write();
ResultYStar[k]->Write();

// Result[k]->Close();

//Selection->Write();

/*
c->Modified(); c->Update();
c1->Modified(); c1->Update();
c2->Modified(); c2->Update();

*/

double Mean=Hist3[k]->GetMean(1);
Acceptance[k]= Hist3[k]->GetEntries()/10000;

return Mean;

}

//end of calculation of acceptance

double Analysis::Calculation(double mass1, double sigma)
{
//Adapted from Cross-Section Select program
//function that calculated and print out sigG/MassG( it does not return)
//Returns the cross section value

double sigGmassG;
double sigMassinput;

double massGseven[100] = {300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3200,3400,3600,3800,4000};
double massGten[100] = {350,400,450,500,550,600,650,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3200,3400,3600,3800};
double massGfifteen[100] = {400,450,500,550,600,650,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2100,2200,2300,2400,2500,2600,2700,2800,2900,3000,3200,3400};

double ptseven[100]={380,250,77,63,39,17,7.6,4.7,2.8,1.1,0.67,0.47,0.28,0.2,0.21,0.22,0.2,0.14,0.096,0.068,0.061,0.064,0.069,0.076,0.08,0.081,0.078,0.074,0.068,0.059,0.049,0.038,0.029,0.026,0.025,0.023,0.018,0.014,0.012,0.01,0.0085,0.0066,0.0049,0.0038,0.0031,0.0023,0.0016,0.0012,0.00099,0.00085};
double ptten[100]={210,97,75,41,17,8.3,4.8,2.3,1.2,0.8,0.5,0.34,0.3,0.28,0.24,0.18,0.14,0.11,0.088,0.083,0.088,0.093,0.1,0.11,0.11,0.1,0.093,0.08,0.068,0.057,0.047,0.041,0.036,0.032,0.026,0.021,  0.017,0.014,0.011,0.0087,0.0067,0.0053,0.0042,0.0034,0.0023,0.0017,0.0013,0.0011};
double ptfifteen[100]={120,75,34,15,7.5,3.6,2,1.3,0.88,0.67,0.52,0.39,0.28,0.23,0.18,0.16,0.13,0.14,0.14,0.11,0.12,0.15,0.15,0.14,0.13,0.11,0.085,0.082,0.072,0.062,0.054,0.046,0.041,0.032,0.025,0.019,0.014,0.011,0.0081,0.0065,0.0051,0.0041,0.0034,0.0025,0.0019};

TFile *RealCrossSection = new TFile("RealCrossSection.root","RECREATE");

TGraphErrors *gr1= new TGraphErrors(50,massGseven,ptseven,0,0);
TGraphErrors *gr2= new TGraphErrors(48,massGten,ptten,0,0);
TGraphErrors *gr3= new TGraphErrors(45, massGfifteen,ptfifteen,0,0);

/*
TCanvas *c3 = new TCanvas();
c3->SetLogy();
*/

gr1->SetLineColor(kRed);
gr2->SetLineColor(kBlue);
gr3->SetLineColor(kGreen);

gr1->Draw("ALP");
gr2->Draw("LP");
gr3->Draw("LP");

gr1->Write();
gr2->Write();
gr3->Write();

RealCrossSection->Write();

gDirectory->Append(gr1);

//input signmassinput and Mass

// mass
// double sigMassinput=1;
double sigMassAnalysis[4]={0.07,0.10,0.15};
double sigMassoutput;

//finding the sigmass for this analysis


double MassG ;


for (int i=0; i<50; i++)
{
    if (mass1<massGseven[i] && i!=0 )
    {
        MassG=massGseven[i];
        
    }
    
    if (mass1<massGseven[0])
        
    {
        MassG=massGseven[0];
    }
    
    if (mass1==massGseven[i])
    {
        MassG=massGseven[i];
    }
    if (mass1<massGseven[i] && i!=0 )
    {
        break;
    }
    cout<<" Mass 1: "<<mass1<<" MassGfifth: "<<massGseven[i]<<" Mass G: "<<MassG<<endl;
    
}


sigMassinput=sigma/MassG;

for (int i=0; i<4; i++)
{
    if (sigMassinput<sigMassAnalysis[i] && i!=0 )
    {
        sigMassoutput=sigMassAnalysis[i];
        
    }
    
    if (sigMassinput<sigMassAnalysis[0])
        
    {
        sigMassoutput=sigMassAnalysis[0];
    }
    
    if (sigMassinput==sigMassAnalysis[i])
    {
        sigMassoutput=sigMassAnalysis[i];
    }
    if (sigMassinput<sigMassAnalysis[i] && i!=0 )
    {
        break;
    }
    

    cout<<"Sigmass input: "<<sigMassinput<<" sigMassAnalysis: "<<sigMassAnalysis[i]<<" SigMassoutput: "<<sigMassoutput<<endl;
}

if (sigMassinput>sigMassAnalysis[2])
{
    sigMassoutput=sigMassAnalysis[2];
}
    cout<<"Sigmass input: "<<sigMassinput<<" sigMassAnalysis: "<<sigMassAnalysis[2]<<" SigMassoutput: "<<sigMassoutput<<endl;

if (sigMassinput>sigMassAnalysis[2])
{sigMassoutput=sigMassAnalysis[2];}

//sigGmassG=sigMassoutput/MassG;

// cout<<"SigG/mG: "<<sigGmassG<<endl;

double CrossSection;

if (sigMassoutput==0.07)
{
    CrossSection=gr1->Eval(MassG);
cout <<"Mass: "<<MassG<<" sigma: "<<sigma<<" SigG/MassG: "<<sigMassoutput<<" Cross Section Result: "<<CrossSection<<endl;
    
}

if (sigMassoutput==0.1)
{
        CrossSection=gr2->Eval(MassG);
        cout <<"Mass: "<<MassG<<" sigma: "<<sigma<<" SigG/MassG: "<<sigMassoutput<<" Cross Section Result: "<<CrossSection<<endl;
}

if (sigMassoutput==0.15)
{
    CrossSection=gr3->Eval(MassG);
    cout <<"Mass: "<<MassG<<" sigma: "<<sigma<<" SigG/MassG: "<<sigMassoutput<<" Cross Section Result: "<<CrossSection<<endl;
}
//for different values of sigG/mG, use differnt cross section lines to return cross sections
    
    
return CrossSection;

}

void Analysis::Loop(int M,int k)
{

if (fChain == 0) return;
double inputmass;
double massAfterSelection;
double CrossSection;
double sigma;
inputmass=1000;
ofstream infile;

//Finding the average mass after event selection:
cout<<"Mass of the Generated Events: "<<M<<endl;

massAfterSelection=Analysis::Selection(M,k);

cout<<"Mass after Selection: "<<massAfterSelection<<endl;

//Finding the SigG/MassG
sigma=(1.2*inputmass-0.8*inputmass)/5;
CrossSection=Analysis::Calculation(massAfterSelection, sigma);
cout<<"Cross Section: "<<CrossSection<<endl;

cout<<"Check";
infile.open("crosssectionvsmass2.txt");
infile<<"Cross Section: "<<CrossSection<<endl;

}

void GenerateAnalysis(void)
{
ofstream infile;
infile.open(“crosssectionvsmass.txt”);

string Dr="/Users/yvonne/Documents/MadGraph/MG5_aMC_v2_2_3/PROC_New_Resonances_UFO_0/Events/mass";
string MassNumber[20]={"250","500","1000","1500","3000","3500","4000"};

//,"4500","5000","5500", "6000","6500","7000"
string FileName="/tag_2_pythia_lhe_events.root";
string AbsDirectory;
string AnalysisName;
//  int i=1;
gSystem->Load("Analysis.C");

    int mass[20]={250,500,1000,1500,3000,3500,4000};
//,4500,5000,5500, 6000,6500,7000
//   gSystem->CompileMacro("Analysis.C","k");
Hist[1]= new TH1D("Mass Resonance+Selection","Mass Resonance",1000,-1000,5000);


for (int i=0; i<1; i++)
{
    Result[i]= new TFile(Form("Result Mass %d", mass[i]), "RECREATE");
    Hist[i]= new TH1D(Form("Mass Resonance Mass %d",mass[i]),Form("Mass Resonance Mass %d",mass[i]),1000,-1000,5000);
    Hist1[i]= new TH1D(Form("Mass Resonance+Selection Mass %d",mass[i]),Form("Mass Resonance+Selection Mass %d",mass[i]),1000,-1000,5000);
    Hist2[i]= new TH1D(Form("Mass Resonance Mass+Selection+smearing effect %d",mass[i]),Form("Mass Resonance Mass+Selection+smearing effect %d",mass[i]),1000,-1000,5000);
    Hist3[i]= new TH1D(Form("Mass Resonance+Selection+smearing effect+truncated Mass %d",mass[i]),Form("Mass Resonance+smearing effect+truncated Mass %d",mass[i]),1000,-1000,5000);
    
    
    //Leading Jet Histograms
    ResultPT[i]= new TFile(Form("Result PT Leading Jet, Mass %d.root", mass[i]), "RECREATE");
    HistPT[i]= new TH1D(Form("Leading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),Form("Leading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),1000,0,1200);
    HistPTSelect[i] = new TH1D(Form("Leading Jet: Transverse Momentum After Selection Mass%d",mass[i]),Form("Leading Jet: Transverse Momentum After Selection Mass%d",mass[i]),1000,0,1200);
    HistPTSelectSmeared[i]= new TH1D(Form("Leading Jet: Transverse Momentum After Selection + Smearing Mass%d",mass[i]),Form("Leading Jet: Transverse Momentum After Selection + Smearing Mass%d",mass[i]),1000,0,1200);
    HistPTSelectSmearedTruncated[i]= new TH1D(Form("Leading Jet: Transverse Momentum After Selection+ Smearing+ Truncated Mass%d",mass[i]),Form("Leading Jet: Transverse Momentum After Selection+ Smearing+ Truncated Mass%d",mass[i]),1000,0,1200);
    
    ResultY[i]= new TFile(Form("Result Rapidity Leading Jet, Mass %d", mass[i]), "RECREATE");
    HistY[i]=new TH1D(Form("Leading Jet: Rapidity Before Selection Mass%d", mass[i]),Form("Leading Jet: Rapidity Before Selection Mass%d", mass[i]),1000,-10,10);
    HistYSelect[i]=new TH1D(Form("Leading Jet: Rapidity After Selection Mass%d",mass[i]),Form("Leading Jet: Rapidity After Selection Mass%d",mass[i]),1000,-10,10);
    HistYSelectSmeared[i]=new TH1D(Form("Leading Jet: Rapidity After Selection+ Smearing Mass%d",mass[i]),Form("Leading Jet: Rapidity After Selection+ Smearing Mass%d",mass[i]),1000,-10,10);
    HistYSelectSmearedTruncated[i]=new TH1D(Form("Leading Jet: Rapidity After Selection+ Smearing+ Truncated Mass%d",mass[i]),Form("Leading Jet: Rapidity After Selection+ Smearing+ Truncated Mass%d",mass[i]),1000,-10,10);
    
    ResultYStar[i]= new TFile(Form("Result Absolute Half Difference between the 2 leading Jets, Mass %d", mass[i]),"RECREATE");
    HistYStar[i]=new TH1D(Form("Absolute Half Difference in Rapidity between Leading and subleading jet; Mass%d",mass[i]),Form("Leading Jet: Absolute Half Difference in Rapidity between Leading and subleading jet; Mass%d",mass[i]),1000,-1.2,1.2);
    HistYStarSelectSmearedTruncated[i]=new TH1D(Form("After all Selections, Half Difference in Rapidity between Leading and subleading jet; Mass%d",mass[i]),Form("After all Selections, Half Difference in Rapidity between Leading and subleading jet; Mass%d",mass[i]),1000,-1.2,1.2);
    
    //SubLeading Jet Histograms
    ResultPTSub[i]= new TFile(Form("Result PT SubLeading Jet, Mass %d", mass[i]), "RECREATE");
    HistPTSub[i]= new TH1D(Form("SubLeading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),Form("SubLeading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),1000,0,1200);
    HistPTSelectSub[i] = new TH1D(Form("SubLeading Jet: Transverse Momentum After Selection Mass%d",mass[i]),Form("SubLeading Jet: Transverse Momentum After Selection Mass%d",mass[i]),1000,-0,1200);
    HistPTSelectSmearedSub[i]= new TH1D(Form("SubLeading Jet: Transverse Momentum After Selection + Smearing Mass%d",mass[i]),Form("SubLeading Jet: Transverse Momentum After Selection + Smearing Mass%d",mass[i]),1000,0,1200);
    HistPTSelectSmearedTruncatedSub[i]= new TH1D(Form("SubLeading Jet: Transverse Momentum After Selection+ Smearing+ Truncated Mass%d",mass[i]),Form("SubLeading Jet: Transverse Momentum After Selection+ Smearing+ Truncated Mass%d",mass[i]),1000,0,1200);
    
    ResultYSub[i]= new TFile(Form("Result Rapidity SubLeading Jet, Mass %d", mass[i]), "RECREATE");
    HistYSub[i]=new TH1D(Form("SubLeading Jet: Rapidity Before Selection Mass%d", mass[i]),Form("SubLeading Jet: Rapidity Before Selection Mass%d", mass[i]),1000,-10,10);
    HistYSelectSub[i]=new TH1D(Form("SubLeading Jet: Rapidity After Selection Mass%d",mass[i]),Form("SubLeading Jet: Rapidity After Selection Mass%d",mass[i]),1000,-10,10);
    HistYSelectSmearedSub[i]=new TH1D(Form("SubLeading Jet: Rapidity After Selection+ Smearing Mass%d",mass[i]),Form("SubLeading Jet: Rapidity After Selection+ Smearing Mass%d",mass[i]),1000,-10,10);
    HistYSelectSmearedTruncatedSub[i]=new TH1D(Form("SubLeading Jet: Rapidity After Selection+ Smearing+ Truncated Mass%d",mass[i]),Form("SubLeading Jet: Rapidity After Selection+ Smearing+ Truncated Mass%d",mass[i]),1000,-10,10);
    //Intermediate check:
    
    ResultPTY[i]=new TFile(Form("Result PT & Y Selection, Mass %d", mass[i]), "RECREATE");
    HistPTYPT[i]=new TH1D(Form("PT After Selection on PT and Y; mass %d",mass[i]),Form("PT After Selection on PT and Y; mass %d",mass[i]),1000,0,1200);
    HistPTYY[i]=new TH1D (Form("Rapidity After Selection on PT and Y; mass %d",mass[i]),Form("PT After Selection on PT and Y; mass %d",mass[i]),1000,-1.2,1.2);
                               
    ResultPTYsub[i]=new TFile(Form("Subjet Result PT & Y Selection, Mass %d", mass[i]), "RECREATE");
    HistPTYPTsub[i]=new TH1D(Form("Subjet PT After Selection on PT and Y; mass %d",mass[i]),Form("Subjet PT After Selection on PT and Y; mass %d",mass[i]),1000,0,1200);
    HistPTYYsub[i]=new TH1D (Form("Subjet Rapidity After Selection on PT and Y; mass %d",mass[i]),Form("Subjet PT After Selection on PT and Y; mass %d",mass[i]),1000,-1.2,1.2);
}

//k=0

for (int i =1; i<7; i++)
{
    AbsDirectory=Dr+MassNumber[i]+FileName;
    //    AnalysisName="Analysis"+MassNumber[i];
    cout<<"Abs Directory: "<<AbsDirectory<<endl;
    //      cout<<"Analysis Name: :"<<AnalysisName;
    
    
    //.L Analysis.C;
    TFile *f = new TFile(AbsDirectory.c_str());
    TTree *T;
    
    f ->GetObject("LHEF",T);
    cout<<"T: "<<T;
    
    Analysis t(T);
    //t.Loop();
    t.Loop(mass[i],i);
    
    //Printing to a file
    infile<<"MassNumber: "<<MassNumber[i]<<endl;
    //k=k+1;
    
    cout<<"Test"<<endl;

}
//infile.close();
cout<<"Test"<<endl;

}
[/code]
Analysis.h (10.4 KB)

It seems to me that you have: TH1D *HistPT[20]; but then, in the “GenerateAnalysis”, you actually initialize “HistPT[0]” only: for (int i=0; i<1; i++) { // ... HistPT[i]= new TH1D(Form("Leading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),Form("Leading Jet: Transverse Momentum Before Selection Mass%d", mass[i]),1000,0,1200); // ... } BTW. The “names” of your histograms are wrong. They should be simple identifiers, like ordinary c/c++ variables (without any “special” characters, like “space” or “:” or “+” or “;”). The “titles” of histograms can be fancy, of course.

Thank you!