ROOT Version: latest
Platform: M1
Compiler: VSCode
I am having a hard time making a histogram from the root file, every time it displays an empty canvas. But using direct commands like DataTree->Draw(“SampleStream.ch0_data”) display the histogram. I want to make a histogram so that I can fit the data and can include all the necessary information in a single canvas.The root file has one tree one branch, and other leaves. I am trying to plot a histogram from a leaf. Any help and suggestions for this will be greatly appreciated. Thank you.
I have started with this coding,
#include <iostream>
#include <TH2.h>
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include "TH1F.h"
#include <TStyle.h>
#include <TCanvas.h>
#include <fstream>
#include "TSystem.h"
#include "TPaveLabel.h"
void extractRoot() {
// Read the root file
TString fileName = "Int_Run_020-020.root";
TFile* file = TFile::Open(fileName);
// Check if the file is open
if (!file) {
std::cerr << "Error opening file: " << fileName << std::endl;
return;
}
// Get the tree from the file
TTree* tree = dynamic_cast<TTree*>(file->Get("DataTree"));
TBranch* sampleStreamBranch = tree->GetBranch("SampleStream.ch0_sum");
// Check if the branch exists
if (!sampleStreamBranch) {
std::cerr << "Error retrieving SampleStream.ch0_sum branch" << std::endl;
file->Close();
return;
}
// Get the first entry of the ch0_sum branch
sampleStreamBranch->GetEntry(0);
// Defining variables
std::vector<double> tStmp;
std::vector<double> ch0_data;
std::vector<double> ch1_data;
double ch0_sum;
// Set branch addresses.
tree->SetBranchAddress("tStmp", &tStmp);
tree->SetBranchAddress("ch0_data", &ch0_data);
tree->SetBranchAddress("ch1_data", &ch1_data);
tree->SetBranchAddress("ch0_sum", &ch0_sum);
gStyle->SetOptStat(1111); //sets the option for the statistics box in ROOT to display all items.
// Create a histogram
TH1F* histogram = new TH1F("hist", "ch0_sum Histogram", 10, 0, 2);
// Fill the histogram
for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
tree->GetEntry(i);
histogram->Fill(ch0_sum);
}
// Plot the histogram
TCanvas* canvas = new TCanvas("canvas", "Histogram Canvas");
histogram->Draw();
file->Close();
}
int main() {
extractRoot();
return 0;
}