Projection works in terminal, doesn't work in macro

ROOT Version (6.12/06):
Platform, compiler (Fedora 26, gcc7.3):


I’m having an issue writing a set of commands into a macro. I need it to get a matrix from a root file, set a Y projection and then fit a peak on said projection. I figured out what to do by using commands in the terminal, but for some reason the marco only partially works. I get the result of the fit but it doesn’t draw my graph.

Is there something the terminal is doing behind the scenes that I need to manually implement in a macro?

I would be really grateful for any advice on the matter,
histfitraw.C (1.1 KB)

#include <TFile.h>
#include <TF1.h>
#include <iostream>
#include <string>
#include <sstream>
#include <TDirectory.h>
#include <TObject.h>
#include <TH1.h>

using namespace std;

int histfitraw (){

  char HistRing[20], FileNa[20], resp[20];
  double ampl, area, P2, P3;
  double Pi = 3.14159265358979;

//set parameters for peaks: 

  double uCent = 713.6; double uSigma = 2.3;
  double gLow = 972;   double gHigh = 978;
  double rLow = 650; double rHigh = 750; 

// define fit function 

  TF1 *f = new TF1("f","gaus(0)+pol0(3)",620,680);
  TCanvas *c1 = new TCanvas("c1");

// read gamma-gamma matrix from file and get projection.

      TFile file("matrices_10um.root");
      TH2F *m;
      file.GetObject("mGammaGamma_ring1_ring2_pid100", m);
      TH1D *py = m -> TH2::ProjectionY("py",gLow,gHigh);

// draw projection and fit function.
      py -> GetXaxis() -> SetRangeUser(rLow,rHigh);
      py -> Draw();

      f -> FixParameter(1, uCent);
      f -> FixParameter(2, uSigma);
      py -> Fit("f","B");

      ampl = f -> GetParameter(0);
      area = ampl*uSigma*sqrt(2*Pi);

   return 0; 

Thank you for your swift reply, works perfectly now!

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