Simple array of histograms to file

Hello,

I’ve searched the archives and root website, but I can’t find a simple way to fill an array of histograms. I’m converting a paw script to root. I get no compile/runtime errors, but the script runs endlessly and, during which, creates a root file of 288bytes. I’m sure this is a simple problem with my lack of C++/Root experience. I’ll post the script in its entirety, because it should be so simple for anyone on this forum, but I can easily make a smaller script if this is just two long.

The idea is very simple, read in an ascii file and create a root file full of histograms:

Thank you,
James

{
// template for the following was found at:
//
gROOT->Reset();
#include “Riostream.h”;
#include “functions.c”;

ifstream in;

in.open("/data/babar/rotondo/DataEMiss/b0_nuovo.dat");

int hshift1=3000;

int evtLept, EventKind, nTrack, nKa, nLepSam, Lks, Lkao, KndSl;
Float_t PLepRaw, the1, the2, llTheta, llMass, nuMassSig;
Float_t nuMassTag, mtot, etot, ptot, thtot;

Stat_t Weight;

int SigSemilep, SigKind, TagKind, TagType;
bool isSigMu, isSigEl;

int nplep;

Int_t nlines = 100;
TFile *f = new TFile(“output.root”,“RECREATE”);

// ----------------------------
// Book histograms
// ----------------------------

// gc
Float_t expqq,qqelmb,qqelsb,qqmumb,qqmusb;
Float_t expqqrr,qqelmbe,qqelsbe,qqmumbe,qqmusbe;

// wc
Float_t wcqq,wcelmb,wcelsb,wcmumb,wcmusb;

Float_t wcqqrr,wcelmbe,wcelsbe,wcmumbe,wcmusbe;

int nbin,i,j,k,H,l;
const nbin=50;

char hname[120];
char htitle[120];
Int_t H;
TH1F *h[100000];

h[876] = new TH1F(“876”, “#nu Mass GC”,12,-10.,2.0);
h[877] = new TH1F(“877”, “#nu Mass WC”,12,-10.,2.0);
h[776] = new TH1F(“776”, “#nu Mass GC”,12,-10.,2.0);
h[777] = new TH1F(“777”, “#nu Mass WC”,12,-10.,2.0);

h[8760] = new TH1F(“8760”, “#nu Mass GC”,12,-10.,2.0);
h[8770] = new TH1F(“8770”, “#nu Mass WC”,12,-10.,2.0);
h[7760] = new TH1F(“7760”, “#nu Mass GC”,12,-10.,2.0);
h[7770] = new TH1F(“7770”, “#nu Mass WC”,12,-10.,2.0);

for(Int_t i=0;i<=8;i++){
sprintf(hname,“h%d”,700+i);
h[700+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,710+i);
h[710+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,900+i);
h[900+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,910+i);
h[910+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,750+i);
h[750+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,760+i);
h[760+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,950+i);
h[950+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,960+i);
h[960+i] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
}

for(Int_t i=1;i<=4;i++){
for(Int_t j=0;j<=7;j++){
sprintf(hname,“h%d”,i1000+610+j);
h[i*1000+610+j] = new TH1F(hname,“PLep Data GC”,nbin,1.0,3.5);
sprintf(hname,“h%d”,i
1000+600+j);
h[i*1000+600+j] = new TH1F(hname,“PLep Data WC”,nbin,1.0,3.5);
sprintf(hname,“h%d”,i1000+710+j);
h[i*1000+710+j] = new TH1F(hname,“PLep Data GC (noveto)”,nbin,1.0,3.5);
sprintf(hname,“h%d”,i
1000+700+j);
h[i*1000+700+j] = new TH1F(hname,“PLep Data WC (noveto)”,nbin,1.0,3.5);
}
}

for(Int_t i=1;i<=4;i++) {
for(Int_t j=0;j<=9;j++) {
for(Int_t k=0;k<=9;k++) {
for(Int_t l=1;l<=2;l++) {
H=i10000+l1000+j100+k10;
sprintf(hname,“h%d”,H);
h[H] = new TH1F(hname, “#nu Mass GC”,12,-10.,2.0);
sprintf(hname,“h%d”,H+1);
h[H+1] = new TH1F(hname, “#nu Mass GC NoVeto”,12,-10.,2.0);
sprintf(hname,“h%d”,H+5);
h[H+5] = new TH1F(hname, “#nu Mass WC”,12,-10.,2.0);
sprintf(hname,“h%d”,H+6);
h[H+6] = new TH1F(hname, “#nu Mass WC NoVeto”,12,-10.,2.0);
sprintf(hname,“h%d”,H+2);
h[H+2] = new TH1F(hname, “PLep GC Mass”,nbin,1.0,3.5);
sprintf(hname,“h%d”,H+3);
h[H+3] = new TH1F(hname, “PLep GC NoVeto”,nbin,1.0,3.5);
sprintf(hname,“h%d”,H+4);
h[H+4] = new TH1F(hname, “PLep GC Side”,nbin,1.0,3.5);
sprintf(hname,“h%d”,H+7);
h[H+7] = new TH1F(hname, “PLep WC Mass”,nbin,1.0,3.5);
sprintf(hname,“h%d”,H+8);
h[H+8] = new TH1F(hname, “PLep WC NoVeto”,nbin,1.0,3.5);
sprintf(hname,“h%d”,H+9);
h[H+9] = new TH1F(hname, “PLep WC Side”,nbin,1.0,3.5);

      sprintf(hname,"h%d",H+5002);
      h[H+5002] = new TH1F(hname, "PLep GC Mass",nbin,1.0,3.5);
      sprintf(hname,"h%d",H+5007);
      h[H+5007] = new TH1F(hname, "PLep WC Mass",nbin,1.0,3.5);

      sprintf(hname,"h%d",H+5000);
      h[H+5000] = new TH1F(hname, "Mnu GC Mass",12,-10.,2.0);
      sprintf(hname,"h%d",H+5005);
      h[H+5005] = new TH1F(hname, "Mnu WC Mass",12,-10.,2.0);

      sprintf(hname,"h%d",H+2000);
      h[H+2000] = new TH1F(hname, "Mnu GC Mass",12,-10.,2.0);
      sprintf(hname,"h%d",H+2005);
      h[H+2005] = new TH1F(hname, "Mnu WC Mass",12,-10.,2.0);

      sprintf(hname,"h%d",H+5003);
      h[H+5003] = new TH1F(hname, "PLep GC Mass",nbin,1.0,3.5);
      sprintf(hname,"h%d",H+5008);
      h[H+5008] = new TH1F(hname, "PLep WC Mass",nbin,1.0,3.5);
    }
  }
}

}
sprintf(hname, “h%d”, 1031);
h[1031] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 1032);
h[1032] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 1041);
h[1041] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 1042);
h[1042] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 2031);
h[2031] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 2032);
h[2032] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 2041);
h[2041] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);
sprintf(hname, “h%d”, 2042);
h[2042] = new TH1F(hname,“mnu p.gt.2.8”,12,-10.,2.0);

sprintf(hname, “h%d”, 61);
h[61] = new TH1F(hname, “PLep Data GC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 66);
h[66] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 161);
h[161] = new TH1F(hname, “PLep Data GC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 166);
h[166] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);

sprintf(hname, “h%d”, 62);
h[62] = new TH1F(hname, “PLep Data GC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 67);
h[67] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 162);
h[162] = new TH1F(hname, “PLep Data GC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 167);
h[167] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);

sprintf(hname, “h%d”, 551);
h[551] = new TH1F(hname, “nu Mass Sig El”,12,-10.,2.0);
sprintf(hname, “h%d”, 552);
h[552] = new TH1F(hname, “nu Mass Sig Mu”,12,-10.,2.0);
sprintf(hname, “h%d”, 556);
h[556] = new TH1F(hname, "PLep Sig El ",nbin,1.0,3.5);
sprintf(hname, “h%d”, 557);
h[557] = new TH1F(hname, "PLep Sig Mu ",nbin,1.0,3.5) ;

sprintf(hname, “h%d”, 1000+510);
h[1000+510] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 1000+500);
h[1000+500] = new TH1F(hname, “PLep Data”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 2000+510);
h[2000+510] = new TH1F(hname, “PLep Data WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 2000+500);
h[2000+500] = new TH1F(hname, “PLep Data”,nbin,1.0,3.5);

sprintf(hname, “h%d”, 3000+511);
h[3000+511] = new TH1F(hname, “PLep Sig B0 WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 3000+501);
h[3000+501] = new TH1F(hname, "PLep Sig B0 ",nbin,1.0,3.5);

sprintf(hname, “h%d”, 4000+511);
h[4000+511] = new TH1F(hname, “PLep Sig B+ WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 4000+501);
h[4000+501] = new TH1F(hname, "PLep Sig B+ ",nbin,1.0,3.5);

sprintf(hname, “h%d”, 3000+512);
h[3000+512] = new TH1F(hname, “PLep BB B0 WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 3000+502);
h[3000+502] = new TH1F(hname, "PLep BB B0 ",nbin,1.0,3.5);

sprintf(hname, “h%d”, 4000+512);
h[4000+512] = new TH1F(hname, “PLep BB B+ WC”,nbin,1.0,3.5);
sprintf(hname, “h%d”, 4000+502);
h[4000+502] = new TH1F(hname, "PLep BB B+ ",nbin,1.0,3.5);

while (1) {
in >> evtLept >> PLepRaw >> the1 >> the2 >> EventKind >> nTrack >> Weight >> llTheta >> llMass >> nKa >> nLepSam >> n
uMassSig >> nuMassTag >> Lks >> Lkao >> KndSl >> mtot >> etot >> ptot >> thtot;
if (!in.good()) break;

 Float_t PLep = fabs(PLepRaw);


 //Define cuts:

bool cut1=false;
bool cut2=false;
bool cut3=false;
bool cut4=false;
bool cut5=false;
bool cut6=false;
bool cut9=false;

//Load functions
//.L functions.c

// ----------------------------------------------------------
SigKind= SigSemilep(KndSl);
TagKind= TagType(EventKind);
// ----------------------------------------------------------

 if((fabs(evtLept)==11) && (fabs(nTrack)>=6)){
   if(fabs(the1-the2)>0.05){
     if(the1 >0.  && the2>0.  && thtot>0.2 &&
        thtot<2.8 && ptot>1.0 && ptot <3.<img src="/uploads/default/original/1X/fe5f42ae79e389360ae9d969db4dcc4bca03d384.gif" width="15" height="17" alt="8)" title="Cool"/>{
       cut1=true;
     }
   }
 }
 else((fabs(evtLept)>11) && (fabs(nTrack)>=5)) {
   if(the1>0.  && the2>0.  &&
      ptot>0.5 && ptot<3.8 ) {
       cut1=true;
   }
 }


 if(fabs(evtLept)==11) {
   if(llMass>0.5 && llMass<4.0) {
     cut2=true;
   }
 }
 else(llMass>0.5 && llMass<4.2) {
   cut2=true;
 }

 if(llTheta>0.4 && llTheta<2.<img src="/uploads/default/original/1X/fe5f42ae79e389360ae9d969db4dcc4bca03d384.gif" width="15" height="17" alt="8)" title="Cool"/> {
   cut3=true;
 }

 if(fabs(evtLept)==11 || fabs(evtLept)==22) {
   if(llMass-3.1>0.05 || llMass-3.1 < -0.10) {
     cut4=true;
   }
 }
 else{
   cut4=true;
 }

 if(nuMassSig<-4.0&&(Lks+Lkao)<1) cut5=true;


 if(nTrack>0 && nKa==0 && nLepSam<2) cut6=true; //unmixed
 if(nTrack<0 && nKa<1 && nLepSam<1) cut6=true; // mixed


 if(PLep>2.30&&PLep<2.60) cut9=true;


 //----------------------------------------------------------------

 if(hshift1==3000) {
   if(isSigEl) H=551;
   if(isSigMu) H=552;
   if(SigKind==1 && TagKind==1) {
     h[H+5]->Fill(PLep, Weight);
   }
   if(cut9){
     if(SigKind==1 && TagKind==1) {
       h[H]->Fill(nuMassTag,Weight);
         }
   }
 }

 // -----------------------------------------------------------


 bool isVub=false;
 bool isNotVub=false;

 bool isGC=false;
 bool isWC=false;

 bool isMixed=false;
 bool isUnmixed=false;

 bool isSigMu=false;
 bool isSigEl=false;

 if(SigKind==1) {
   isVub=true;
   isNotVub=false;
 }
 else {
   isVub=false;
   isNotVub=true;
 }

 if(nTrack>0) {
   isMixed=false;
   isUnmixed=false;
 }
 else {
   isMixed=true;
   isUnmixed=false;
 }

 if(fabs(evtLept)==21 || fabs(evtLept)==22)  {
   isSigMu=true;
   isSigEl=false;
 }
 else {
   isSigMu=false;
   isSigEl=true;
 }

 if(evtLept>0)  {
   isGC=true;
   isWC=false;
 }
 else {
   isGC=false;
   isWC=true;
 }

 // -----------------------------------------------------------
 if(isSigEl) H=hshift1*10+1000+TagKind*100+SigKind*10;
 if(isSigMu) H=hshift1*10+2000+TagKind*100+SigKind*10;

 if(cut9)  {
   if(cut1 && cut2 && cut3 && cut4)  {
     if(cut5 && cut6)  {
       if(evtLept>0)  {
         if(nuMassTag>-3.0 && nuMassTag<2.0) {
           int nmnu=nmnu+1;
         }
         h[H]->Fill(nuMassTag,Weight);
       }
       if(evtLept<0) {
         h[H+5]->Fill(nuMassTag,Weight);
       }
     }
     else {
       if(evtLept>0) {
         h[H+1]->Fill(nuMassTag,Weight);
       }
       if(evtLept<0) {
         h[H+6]->Fill(nuMassTag,Weight);
       }
     }
   }
 }

// Nu Mass no-cuts (only PLep)
// if(cut9) {
if(cut9 && cut2 && cut1 && cut3 && cut4) {
if(evtLept>0) {
h[H+5000]->Fill(nuMassTag,Weight);
}
if(evtLept<0) {
h[H+5+5000]->Fill(nuMassTag,Weight);
}
}

// Nu Mass no-cuts (PLep+continuum rejection)
// if(cut9 && cut2 && cut1 && cut3 && cut4) {
if(cut9) {
if(evtLept>0) {
h[H+2000]->Fill(nuMassTag,Weight);
}
if(evtLept<0) {
h[H+5+2000]->Fill(nuMassTag,Weight);
}
}

// studi WC-GC *** nuovi
if(hshift1<3000 && PLep>2.8 && PLep<3.5) {
if(isGC) h[hshift1+31]->Fill(nuMassTag,Weight);
if(isWC) h[hshift1+32]->fill(nuMassTag,Weight);
}
if(hshift1<3000 && PLep>2.8 && PLep<3.5) {
if(cut1 && cut2 && cut3 && cut4 && cut5 && cut6) {
if(isGC) h[hshift1+41]->Fill(nuMassTag,Weight);
if(isWC) h[hshift1+42]->Fill(nuMassTag,Weight);
}
}

/* — Codice Istogrammi
1.0 - 3.5 2.7 - 3.5
1(lep)002 GC MB 1(lep+5)002
1(lep)003 GC SB 1(lep+5)003
1(lep)007 WC MB 1(lep+5)007
1(lep)008 WC SB 1(lep+5)008

PLep mass-side
if(isMCB0 && SigKind==3) PLep=PLep*(0.95+(PLep-1.0)*.02)
*/

     if(nuMassTag>-3.0 && nuMassTag<2.0) {
       if(cut1 && cut2 && cut3 && cut4) {
         if(cut5 && cut6) {
           if(evtLept>0) {
             if(cut9) nplep=nplep+1;
             h[H+2]->Fill(PLep,Weight);
           }
           if(evtLept<0) {
             h[H+7]->Fill(PLep,Weight);
           }
         }
         if(cut5 && cut6 && fabs(PLep)>2.7) {
           if(evtLept>0) {
             h[H+5000+2]->Fill(PLep,Weight);
           }
           if(evtLept<0) {
             h[H+5000+7]->Fill(PLep,Weight);
           }
         }
       }
     }


     // PLep side-band
     if(nuMassTag<-4.0) {
       if(cut1 && cut2 && cut3 && cut4) {
         if(cut5 && cut6) {
           if(evtLept>0)
             {
             h[H+3]->Fill(PLep,Weight);
             }
           if(evtLept<0)
             {
             h[H+8]->Fill(PLep,Weight);
             }
         }
         if(cut5 && cut6 && fabs(PLep)>2.7) {
             if(evtLept>0)
               {
                 h[H+5000+3]->Fill(PLep,Weight);
               }
         }
         if(evtLept<0) {
           h[H+5000+8]->Fill(PLep, Weight);
         }
       }
     }

}

/* PLep mass-band nocuts
if(nuMassTag>-3.0) {
if(evtLept>0) {
h[H+2+5000]->Fill(PLep,Weight);
}
if(evtLept<0) {
h[H+7+5000]->Fill(PLep,Weight);
}
}

*/

in.close();

f->Write();
}

You have many problems in your C++ code, eg
int nbin is redeclared
int H is redeclared
variable Cool is not defined and you have a missing parenthesis at
sprintf(hname,“h%d”,H+Cool); //<=================

To make progress with your code, you should post the data file “b0_nuovo.dat”

You should not use the thread “CINT” to report this type of problem,
but the thread “Support”

Rene

Thanks for your reply. I’ll look into these problems and move the thread.

James

Brun,

If I can ask you one more quick thing. How do you debug your root scripts? Do these compile in any fashon outside of root? Are you using a particular editor?

Any tips you can give to a root/C++ neophyte would be of great help.

Thanks again,

James

The best way to debug your script (if you suspect a problem with C++)
is to write your script as a C++ compliant script and use ACLIC.
In your case, I changes the top of your script to look like:

[code]//file james.C
#include “TFile.h”
#include “TH1.h”
#include “Riostream.h”

void james() {
// template for the following was found at:
//
//gROOT->Reset();[/code]

then in an interactive ROOT session, I did

root > .L james.C+ root > james()
Rene