TH2F Fill() , wrong thrird argument

Hi, I have this piece of code and I fail in the line h2fp->Fill(ch,st,150.);. My problem is it does not take the 3rd argument that assigned for the fill function. It does not complain, it enters the if statement conditional statement, but it fills the histogram with the variable value. What did i miss. Many thanks.

TH2F *foo(int minChan, int maxChan, TH2F *h2, int stepInterval) {
	int totalChannels = maxChan-minChan+1;
	int totalSteps = h2->GetYaxis()->GetNbins(); printf("totalSteps %d\n",totalSteps );
	int step_low = (h2->GetYaxis()->GetXmin()) +1; printf("step_low %d\n", step_low );
	int step_high = h2->GetYaxis()->GetXmax(); printf("step_high %d\n", step_high);
	Double_t step;
	Double_t value;
	
	TH2F *h2fp = new TH2F ("h2fp", "", totalChannels,minChan-0.5, maxChan+0.5, ((step_high-step_low)/stepInterval)+1, step_low-(Double_t)stepInterval/2.,step_high + (Double_t)  stepInterval/2.);
	h2fp->SetStats(kFALSE);
	for (int ch=minChan;ch<maxChan; ch++){
		TH1F *h1 = getCh(h2, ch);
		TH1F *h1_spectrum = spectrumFromCh(h1);	
		for (int st =step_low ; st<step_high+1; st=st+stepInterval){								
			value= h1_spectrum->GetBinContent(st);	
			if(value>150.) {				
				h2fp->Fill(ch,st,150.);				
			}
				h2fp->Fill(ch,st,value);
		
		}			
		delete h1;
		delete h1_spectrum;
	}
	return h2fp;
}

h2fp->Fill(ch, st, ((value > 150.0) ? 150.0 : value));

BTW. In your code, the line “h2fp->Fill(ch,st,value);” will ALWAYS be executed.

I think you mean:

         if (value>150.) {            
            h2fp->Fill(ch,st,150.);            
         } else {
            h2fp->Fill(ch,st,value);
         }

or more compact:

         if (value>150.) h2fp->Fill(ch,st,150.);            
         else h2fp->Fill(ch,st,value);

Thanks. i tried this too, but I failed. Wile’s suggestion helped . Thanks!

[quote=“couet”]I think you mean:

         if (value>150.) {            
            h2fp->Fill(ch,st,150.);            
         } else {
            h2fp->Fill(ch,st,value);
         }

or more compact:

if (value>150.) h2fp->Fill(ch,st,150.); else h2fp->Fill(ch,st,value); [/quote]

Actually, my suggestion (which uses the ?: ternary operator) is a “compact form” of what Olivier shows, so in both cases you should get exactly the same results.

Ok, but also not what Wile said:

he is right, the way your code is written you will fill your histogram twice when value > 150.

May be that’s what you want … up to you .

Acknowledged. Thanks