Two Graphs, one empty other one full


ROOT Version: 14.06.01
Platform: Not Provided
Compiler: Not Provided
_

I am trying to graph a histogram and a tgraph based on a data set. If a point in the data set has a value > 0, then the histogram will have a value of 1 for that x point, and 0 otherwise. The graph is just plotting the data itself. For some reason, the Tgraph works perfectly but the TH1D is not being graphed. When I write both graphs into a new Root file, I can graph the TH1D with no problem.

Here is my code:

#include <vector>
#include <iostream>
#include <fstream>

using std::cin;
using std::cout;
using std::endl;

//some ROOT includes
#include "TInterpreter.h"
#include "TROOT.h"
#include "TH1D.h"
#include "TH1F.h"
#include "TH2S.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TVectorD.h"
#include "TGraph.h"
#include "TH1D.h"

void activityGraphs ()
{
    std::vector<Double_t> channel, activity; 
 
    TFile* file = TFile::Open("/Applications/root_v6.14.02/output_run3.root");
    TH2F* readThis = 0;
    TH1D binaryHist("wires_status","channels status",8256,0,8256); 
    file->GetObject("avgHitAmpHist",readThis);
    int yBin = readThis->GetYaxis()->GetNbins();
    int xBin = readThis->GetXaxis()->GetNbins();

    TFile f_output("/Applications/deadChannelsTrigger.root","RECREATE");

    for(int i =0; i<xBin;i++)
    {
        auto data = readThis->GetBinContent(i,(int)yBin);
        channel.push_back((Double_t)i);
        activity.push_back((Double_t)data);
        if (data != 0)
	{
	   binaryHist.Fill((double)i,1);
	}
	else
	{
	binaryHist.Fill((double)i,0);
	}
    }
    auto graph3 = new TCanvas("Channel Activity","Channel Activity");
    auto channelAct= new TGraph(xBin, channel.data(), activity.data());

    graph3->Divide(1,2);
    f_output.cd();
    graph3->cd(1);
    binaryHist.Draw("hist");
    graph3->Draw();
 
    graph3->cd(2);
    binaryHist.Write();
    graph3->Draw();
    channelAct->SetFillColor(1);
    channelAct->Draw("AB");
    channelAct->Write();
    f_output.Close();

    }

Your histogram gets deleted when your macro finishes.
If you want to “preserve” it, you would need to create it on the heap (i.e. using “new”).

1 Like

Thank you, I tried using a pointer instead and it wouldn’t work, but that fixed it

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