Create(and fill) histograms and ntuple using a for loop

I am trying to create an ntuple in the most automated possible way. The idea is the following. I have an ascii file to be processed. The columns of this file will define how many histos will be created and filled. I need one TH1D histogram per column and 1 TH2D histogram per 2 columns. I am processing the file in a script, that I am executing inside root.

The file to be processed has the format

Kmax Event File - Text Format 1 4 1000 65 4121 9426 12312 56 4118 8882 12307 1273 4188 8217 12309 1291 4204 8233 12308 1329 4170 8225 12303 1341 4135 8207 12306 63 4108 8904 12300 60 4106 8897 12307 731 4108 8192 12306 ... ÿÿÿÿÿÿÿÿ

The script, named evnt2dat is the following

[code]#!/bin/bash

if test $1 ; then
if [ -f $1.evnt ] ; then
rm -f $1.dat
sed -n ‘2p’ $1.evnt | (read v1 v2 v3
for filename in $1*.evnt ; do
echo -e “Processing file $filename"
sed ‘$d’ < $filename > $1_tmp
sed -i ‘/Kmax/d’ $1_tmp
sed -i ‘/^’”$v1"’ ‘"$v2"’ /d’ $1_tmp
cat $1_tmp >> $1.dat
done
v3=wc -l $1.dat | awk '{print $1}'
echo -e “$v1 $v2 $v3” > .$1.dat
rm -f $1_tmp)
else
echo -e "\a!!!"
echo -e " Event file $1.evnt doesn’t exist !!!"
echo -e "!!!"
fi
else
echo -e "\a!!!"
echo -e "!!! Give name for event files !!!"
echo -e "!!!"
fi
awk -v channels=4096 ‘NR<=2{next}{for (i=1;i<=NF;i++) $i=$i-(i-1)*channels} END{detectors=NF;}1’ $1.dat >$1_Processed.dat
rm -f $1.dat
exit 0[/code]

and the processed file is

65 25 1234 24 56 22 690 19 1273 92 25 21 1291 108 41 20 1329 74 33 15 1341 39 15 18 63 12 712 12 60 10 705 19 731 12 0 18

What I want to do is create an ntuple and histograms, in a way I described at the beginning of my post. A macro that does the job is the following

[code]#include "Riostream.h"
void ntuple(char * file_c) {

TString file(file_c);
gSystem->Exec(TString::Format("./evnt2dat %s",file.Data()));
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“ntuple.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(TString::Format("%s%s_Processed.dat",dir.Data(),file.Data()));

Float_t de1,e1,de2,e2;
Int_t events = 0;
Int_t channels=4096;
TFile *f = new TFile(“ntuple.root”,“RECREATE”);
//Create the histograms
TH1F *histo_de1 = new TH1F(“histo_de1”,"#Delta E_{1}",channels,1,channels);
TH1F *histo_e1 = new TH1F(“histo_e1”,“E_{1}”,channels,1,channels);
TH1F *histo_de2 = new TH1F(“histo_de2”,"#Delta E_{2}",channels,1,channels);
TH1F *histo_e2 = new TH1F(“histo_e2”,“E_{2}”,channels,1,channels);
TH2F *dee1 = new TH2F(“dee1”, “#Delta E_{1} VS E_{1}”,channels,1,channels,channels,1,channels);
TH2F *dee2 = new TH2F(“dee2”, “#Delta E_{2} VS E_{2}”,channels,1,channels,channels,1,channels);
/An attempt to creat the histos in an automated way
//Create the e histos
std::vector my_e_hists;
for(Int_t i=1;i<3;i++)
{
TString name = TString::Format(“histo_e%d”,i);
TH1F temp(name,name,channels,1,channels);
my_e_hists.push_back(temp);
}
//Create the de histos
std::vector my_de_hists;
for(Int_t j=1;j<3;j++)
{
TString name = TString::Format(“histo_de%d”,i);
TH1F temp(name,name,channels,1,channels);
my_de_hists.push_back(temp);
}
/

//Create the ntuple
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“de1:e1:de2:e2”);

//Fill the histos and the ntuple
while (1) {
in >> de1 >> e1 >> de2 >> e2;
if (!in.good()) break;
histo_de1->Fill(de1);
histo_e1->Fill(e1);
histo_de2->Fill(de2);
histo_e2->Fill(e2);
dee->Fill(de1, e1);
/* for(Int_t k=1;k<3;k++)
{
histo_e$k->Fill(e_k);
}
for(Int_t l=1;l<3;l++)
{
histo_e$l->Fill(de_l);
}*/
ntuple->Fill(de1,e1,de2,e2);
events++;
}
printf(" found %d events\n",events);

in.close();

f->Write();
histo_de1->Draw();
//dee->Draw(“COLZ”);
//Need for loops to draw them…
}[/code]

Is it possible to create the ntuple, fill it, create the histograms, fill them and draw them in an automated way?

Thank you very much in advance!