I’m beginning to study ROOT for an undergraduate research project. I’m trying to take a .dat file with 1200 values and make a TH1F with these 1200 values filling each width=1 bin from 1 to 1200.
I’m having some trouble getting it right. Right now, I don’t know how to work around making another Branch for the integers 1-1200 and then simply plotting the data points. I want to have a TH1F object, and not a TGraph as presently. I believe a TH1F would allow me to use SetTitle("") and other formatting options better–am I correct?
There must be a better way to do it!
#include "Riostream.h"
void basic2 () {
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("basic2.C","");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%sadc.dat",dir.Data()));
Float_t x,adc;
Int_t nlines = 0;
Int_t channels;
TFile *f = new TFile("basic2.root","RECREATE");
TTree *data1 = new TTree("data1","Data from ADC2249");
data1->Branch("adc",&adc,"adc/F");
data1->Branch("channels",&channels,"channel/I");
TH2F *h1 = new TH2F("h1","counts per channel",1200,0,1200,20,0,36000);
while(1) {
in >> x;
if (!in.good()) break;
adc = x;
channels = 1+nlines;
data1->Fill();
nlines++;
}
printf(" found %d points\n",nlines);
in.close();
f->Write();
data1->Draw("adc:channels","","",300,0);
TGraph *adcgraph = (TGraph*)gPad->GetPrimitive("Graph");
adcgraph->Draw("");
adcgraph->GetXaxis()->SetTitle("ADC2249 Channel Number (1-1200)");
adcgraph->GetYaxis()->SetTitle("Events per channel");
adcgraph->Draw("c");
}