Basic histogram using previously binned data

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");

}

adc.dat (3.04 KB)
basic.C (873 Bytes)

[code]void basic(void)
{
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll("/./", “/”);
dir.Remove(dir.Last(’/’)); // strip the "macro name"
if (dir.Length() > 0) dir.Append(’/’);

ifstream in;
in.open(TString::Format("%sadc.dat", dir.Data()));

Int_t channel = 0;
Float_t adc;

TFile *f = new TFile(“basic.root”, “RECREATE”);

TTree *data1 = new TTree(“data1”, “Data from ADC2249”);
data1->Branch(“channel”, &channel);
data1->Branch(“adc”, &adc);

while(1) {
in >> adc;
if (!in.good()) break;
channel++;
data1->Fill();
}

in.close();

printf(" found %d points\n", channel);

TH1F *h1 = new TH1F(“h1”, “adc counts versus channel number”,
channel, 0.5, (channel + 0.5));

for (Int_t i = 0; i < data1->GetEntries(); i++) {
data1->GetEntry(i);
h1->Fill(channel, adc);
}

h1->Draw();

f->Write();

data1->ResetBranchAddresses();

return;
}[/code]