Issues reading in multiple histograms from TFile

Hi Everyone,

I am attempting to read in multiple TH1D from a root file. They are named in numerical order with a few strings mixed in. My current code will read in the first file, but when I attempt to draw or conduct any operations on any of the other histograms, I get a segmentation break.

Here is the code I am working with.

//You need to enter the following lines in ROOT to use rooFit
//root> gSystem->Load(“libRooFit”) ; 
//root> using namespace RooFit ; 
// Complilation command:
// g++ sitch.cpp `root-config --libs --cflags` -lSpectrum -o stich
#include <stdio.h>
#include <TMath.h>
#include <TCanvas.h>
#include <iostream>
#include <TROOT.h>
#include <TChain.h>
#include <TObject.h>
#include <TRandom.h>
#include <TFile.h>
#include <math.h>
#include <TF1Convolution.h>
#include <TF1.h>
#include <TH1F.h>
#include <TGraph.h>
#include <TVirtualFFT.h>
#include <TH1D.h>
#include <complex.h>
#include "TRandom.h"
#include "TSpectrum.h"
#include "TVirtualFitter.h"
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <string>
#include <sstream>
#include <iomanip>
#include <stdint.h>
#include <ctime>
#include <math.h> 
#include <stdio.h>
#include "TApplication.h"

using namespace std;

double fitGaus(TH1D* histo);

int main(int argc, char **argv)
  const int numofMin = 45;
  TH1D *minRateGam[numofMin];
  TH1D *minRateNeu[numofMin];

  double meanGam[numofMin];
  double meanNeu[numofMin];

  TFile *f1 = new TFile( "/path/to/myFile.root");

  for(int i=0 ;i<numofMin ;i++)
    cout << i << endl;
    stringstream SSminGam;
    TString histminnamegam = SSminGam.str();
    SSminGam << i << "_gamMinRate_ch" << i ;
    histminnamegam = SSminGam.str();
    minRateGam[i] = (TH1D*)f1->Get(histminnamegam);
    meanGam[i] = fitGaus(minRateGam[i]);

    stringstream SSminNeu;
    TString histminnameneu = SSminNeu.str();
    SSminNeu << i << "_neuMinRate_ch" << i ;
    histminnameneu = SSminNeu.str();
    minRateNeu[i] = (TH1D*)f1->Get(histminnameneu);
    meanNeu[i] = fitGaus(minRateNeu[i]);

  TApplication theApp("App", &argc, argv);
  TFile *RootFile;
  RootFile = new TFile("out.root", "RECREATE");
  RootFile->SetCompressionSettings(3); //value betweeon 0 and 9 with 0 being no compression

  TCanvas* c = new TCanvas("c1"," ",200,10,600,400);
  c->Connect("Closed()", "TApplication", &theApp, "Terminate()");


double fitGaus(TH1D* histo)
  //fit double gauss
  Double_t par[3];
  double mean;
  cout<< "check" << endl;
  TF1 *g1 = new TF1("g1","gaus",0.,4000);
  cout<< "fit" << endl;
  mean = par[1];
  return mean;

Any thoughts would be great.

I figured this out. It was a very simple mistake on my part with the naming of the files. Sometimes the answer is right in front of your face but you just can’t see it.