Iterating along histograms

Dear ROOTers:

A cordial greeting for everybody. It is my first time here and i would like to begin the contact with an small problem which is devouring my head this morning. I have a loop along one histogram, i compute a sum over the contents of all bins, i define a magnitude “prueba” just to perform a cut, then i apply the cut and i create one new sum which take into account the cut. My question is if i am performing the loop correctly 'cause i dont obtain the result i expect in the whole code (i get an empty file due to this implementation).

Thanks a lot in advance and the subset of the code is as follow:

Int_t bin1 = h_om_time[iom][ifloor]->FindBin(Tmin);
Int_t bin2 = h_om_time[iom][ifloor]->FindBin(Tnoise);
Int_t nbin = 0;
Double_t sumN=0, sumN2=0;
Double_t BinSize = (Tmax - Tmin)/h_om_time[iom][ifloor]->GetXaxis()->GetNbins();

 Double_t prueba;
 Double_t suma = 0, suma2 = 0; 

for (Int_t ibin = bin1; ibin <= bin2; ibin++) {

Double_t bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
sumN += bincon;
sumN2 += binconbincon;
prueba = 1e6
(sumN/nbin)/BinSize*numberOfEvents);

nbin++;

h_om_noise[iom][ifloor]->Fill(bincon);
if (prueba<=100) {
Double_t bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
suma += bincon;
suma2 += bincon*bincon;
nbin++;

 }

It is hard to understand what you try to do in this piece of code.
-Why do you redeclare and recompte bincon inside the loop?
-The statement “h_om_noise[iom][ifloor]->Fill(bincon);” looks like a bug
(the first argument of Fill is an abscissa, not a weight
-you should post code using the code option or use an attachment.

Rene

Thank you very much Rene for your answer. The piece of code itself could be hard to comprehend, but my whole code is so large. I am going to try to explain once again more easily.

  • The redeclar and recount of bincon is not necessary, it was commented (//), i had a mistake, sorry.
  • The statment h_om_noise[iom][ifloor]->Fill(bincon); works fine into the code but it is not relevant respect to what i want to do in the loop.

I’m just trying to perform a sum, define a cut and then define a new sum which take into account the predefined cut, and i would like to see its results out the loop:

Int_t bin1 = h_om_time[iom][ifloor]->FindBin(Tmin);
Int_t bin2 = h_om_time[iom][ifloor]->FindBin(Tnoise);
Int_t nbin = 0;
Double_t sumN=0, sumN2=0;
Double_t BinSize = (Tmax - Tmin)/h_om_time[iom][ifloor]->GetXaxis()->GetNbins();
Double_t prueba;
Double_t suma = 0, suma2 = 0;

for (Int_t ibin = bin1; ibin <= bin2; ibin++) {

Double_t bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
sumN += bincon; // The sum to define the cut
sumN2 += bincon*bincon;

prueba = 1e6*(sumN/nbin)/(BinSize*numberOfEvents); // Cut i want to perform (NumberOfEvents=constat)
// h_om_noise[iom][ifloor]->Fill(bincon); // iom, ifloor are from an external loop (not showed)

if (prueba<=100) {
suma += bincon; // The sum i want, with the cut
suma2 += bincon*bincon;
fprintf(output,"%i %i %i %3.2f %3.2f \n", nbin, ifloor, iom, suma, prueba); // Here the cut works over the bins
}

nbin++;

    }

fprintf(output2,"%i %i %i %3.2f %3.2f \n", nbin, ifloor, iom, suma, prueba); // here i expect the results with the cut (which are not possible yet, it is my main problem)

Thank you very, very much in advance for your help Rene.

[quote=“brun”]It is hard to understand what you try to do in this piece of code.
-Why do you redeclare and recompte bincon inside the loop?
-The statement “h_om_noise[iom][ifloor]->Fill(bincon);” looks like a bug
(the first argument of Fill is an abscissa, not a weight
-you should post code using the code option or use an attachment.

Rene[/quote]

[quote]Int_t bin1 = h_om_time[iom][ifloor]->FindBin(Tmin);
Int_t bin2 = h_om_time[iom][ifloor]->FindBin(Tnoise);[/quote]What is the meaning of those values? Are you sure bin1 is less than bin2?

Philippe.

Thank you very much Philipe. In fact bin1 is less than bin2, which are the bins where the values Tmin, Tnoise of a given histogram are. I’m trying to perform a sum over such histogram. In a first loop i want to define a cut “prueba”. In a second loop, i want to apply such cut and perform a sum ONLY along the bins whose accomplish the condition:

// Determine the noise level averaging of bin content in the range Tmin - Tnoise
Int_t nbin = 0, nbin2 = 0;
Double_t sumN = 0, sumN2 = 0;
Double_t BinSize = (Tmax - Tmin)/h_om_time[iom][ifloor]->GetXaxis()->GetNbins();

 Double_t prueba, prueba2, prueba3;
 Double_t suma = 0, suma2 = 0;
 Double_t bincon, bincon2;
 
     for (Int_t ibin = bin1; ibin <= bin2; ibin++) {

bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
sumN += bincon;
prueba = 1e6*(sumN/nbin)/(BinSizenumberOfEvents);
sumN2 += bincon
bincon;
nbin++;
h_test[iom][ifloor]->Fill(prueba);
h_om_noise[iom][ifloor]->Fill(bincon);
fprintf(output,"%i %i %i %3.2f %3.2f %3.2f \n", nbin, ifloor, iom, sumN, bincon, prueba);
}

for (Int_t ibin = bin1; ibin <= bin2; ibin++) {
if (prueba<=100.0) {
bincon2 = h_om_time[iom][ifloor]->GetBinContent(ibin);
suma += bincon2;
prueba2 = 1e6*(suma/nbin2)/(BinSizenumberOfEvents);
suma2 += bincon2
bincon2;
fprintf(output2,"%i %i %i %3.2f %3.2f %3.2f \n", nbin2, ifloor, iom, suma, bincon2, prueba2); }
nbin2++;
}

    //            Double_t avgn  = sumN/nbin;
 //    Double_t avgn2 = sumN2/nbin;
 Double_t avgn  = suma/nbin2;
 Double_t avgn2 = suma2/nbin2;
 Double_t eavgn = TMath::Sqrt((avgn2 - avgn*avgn)/nbin2);

 fprintf(output3,"%i %i %i %3.2f %3.2f %3.2f %3.2f %3.2f %3.2f\n", nbin2, ifloor, iom, suma, bincon2, prueba2, avgn, avgn2, eavgn); 
 ////////////////////////////////////////////////////////////////////////////////////////// 

The problem is that my output2 (specially the magnitude “prueba2”) doesn’t match with the cut “prueba” which is defined in the first loop.

Thank you very, very much for your help and for everybody whom can give me a hand.

[quote=“pcanal”][quote]Int_t bin1 = h_om_time[iom][ifloor]->FindBin(Tmin);
Int_t bin2 = h_om_time[iom][ifloor]->FindBin(Tnoise);[/quote]What is the meaning of those values? Are you sure bin1 is less than bin2?

Philippe.[/quote]

hi,

I suspect the problem is that in:for (Int_t ibin = bin1; ibin <= bin2; ibin++) { if (prueba<=100.0) {does not depend on ibin … (and actually this is the prueba from the last bin …).
It sounds like you need:[code]for (Int_t ibin = bin1; ibin <= bin2; ibin++) {
bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
sumN += bincon;
prueba = 1e6*(sumN/nbin)/(BinSizenumberOfEvents);
sumN2 += bincon
bincon;
nbin++;
h_test[iom][ifloor]->Fill(prueba);
h_om_noise[iom][ifloor]->Fill(bincon);
fprintf(output,"%i %i %i %3.2f %3.2f %3.2f \n", nbin, ifloor, iom, sumN, bincon, prueba);

if (prueba<=100.0) {
bincon2 = h_om_time[iom][ifloor]->GetBinContent(ibin);
suma += bincon2;
prueba2 = 1e6*(suma/nbin2)/(BinSizenumberOfEvents);
suma2 += bincon2
bincon2;
fprintf(output2,"%i %i %i %3.2f %3.2f %3.2f \n", nbin2, ifloor, iom, suma, bincon2, prueba2); }
nbin2++;
}
}[/code]

Cheers,
Philippe.

Hi:

Thank you very much. You are right, it is just i wanted, now is working. Other way to solve it was to define a vector prueba[24][4][1430] which can be identified in a second loop.

Now the whole code is working.

Thanks! =D>

[quote=“pcanal”]hi,

I suspect the problem is that in:for (Int_t ibin = bin1; ibin <= bin2; ibin++) { if (prueba<=100.0) {does not depend on ibin … (and actually this is the prueba from the last bin …).
It sounds like you need:[code]for (Int_t ibin = bin1; ibin <= bin2; ibin++) {
bincon = h_om_time[iom][ifloor]->GetBinContent(ibin);
sumN += bincon;
prueba = 1e6*(sumN/nbin)/(BinSizenumberOfEvents);
sumN2 += bincon
bincon;
nbin++;
h_test[iom][ifloor]->Fill(prueba);
h_om_noise[iom][ifloor]->Fill(bincon);
fprintf(output,"%i %i %i %3.2f %3.2f %3.2f \n", nbin, ifloor, iom, sumN, bincon, prueba);

if (prueba<=100.0) {
bincon2 = h_om_time[iom][ifloor]->GetBinContent(ibin);
suma += bincon2;
prueba2 = 1e6*(suma/nbin2)/(BinSizenumberOfEvents);
suma2 += bincon2
bincon2;
fprintf(output2,"%i %i %i %3.2f %3.2f %3.2f \n", nbin2, ifloor, iom, suma, bincon2, prueba2); }
nbin2++;
}
}[/code]

Cheers,
Philippe.[/quote]