Reason for a floating point exception?
This is part of a macro I am writing in the ROOT framework using C++ and some predefined classes. I get a floating point exception upon including the lines marked below (starting with minv = ...
). What is the reason for this error?
Double_t ppx[3000],ppy[3000],ppz[3000],pE[3000];
Double_t m = 0.000511;
Double_t minv,epx,epy,epz,eE;
for(Int_t n = 0; n < nEvents; n++) {
inTree->GetEntry(n);
Int_t nTracks = trackArray->GetEntries();
htrack->Fill(nTracks);
for(Int_t i = 0; i < nTracks; i++) {
Track* trackData = (Track*)trackArray->At(i);
if(trackData->fCharge ==1)
{
ppx[i] = (trackData->fPt) * TMath::Cos(trackData->fPhi);
ppy[i] = (trackData->fPt) * TMath::Sin(trackData->fPhi);
ppz[i] = (trackData->fPt) * sinh(trackData->fEta);
pE[i] = m * m - ppx[i] * ppx[i] - ppy[i] * ppy[i] - ppz[i] * ppz[i];
}
hPt->Fill(trackData->fPt);
}
for(Int_t i = 0; i < nTracks; i++) {
Track* trackData = (Track*)trackArray->At(i);
if(trackData->fCharge == -1)
for (Int_t k=0;k<nTracks;k++){
epx = (trackData->fPt) * TMath::Cos(trackData->fPhi);
epy = (trackData->fPt) * TMath::Sin(trackData->fPhi);
epz = (trackData->fPt) * sinh(trackData->fEta);
eE = m*m - epx *epx - epy * epy - epz * epz;
// the following two lines cause the exception:
minv = ((eE +pE[k]) * (eE + pE[k])) - ((epx + ppx[k]) * (epx + ppx[k])) - ((epy + ppy[k]) * (epy + ppy[k])) - ((epz + ppz[k]) * (epz + ppz[k]) 开发者_高级运维 );
invm->Fill(minv);
}
}
}
nEventsProcessed++;
}
PS: I know this is not very efficient coding, I am a beginner.
I printed out the values of minv. Here is a small sample indicative of the output
-0.225634
-0.657662
-0.225634
1.53201
-0.225634
nan
-0.630927
-0.225634
-0.225634
-0.225634
-0.225634
-0.228794
-0.225634
-0.7196
-0.225634
-0.225634
-0.520265
-0.228796
0.608326
-0.225634
-0.225634
-0.225634
-0.733564
2.74301
-0.763932
-0.225634
-0.225634
nan
-0.228643
-0.225634
-0.225634
-0.225634
-0.584549
Addition: The nan's are very strange. The numbers in their vicinity look normal and this is not a function that blows up, so am I using into junk values of the array?
In the first loop over all tracks, you only fill the arrays if the track charge is positive. In the second loop, you access the arrays no matter whether the charge of the k-th track is positive.
If you want to fill minv
(I guess the invariant mass) only for opposite-charged tracks, remove the first if
that checks for a positive charge and change the last loop to:
for (Int_t i=0; i<nTracks; i++) {
Track* trackData = (Track*)trackArray->At(i);
for (Int_t k=0; k<nTracks; k++){
Track* trackData2 = (Track*)trackArray->At(k);
if (trackData->fCharge == trackData2->fCharge)
continue;
// calculate minv here
invm->Fill(minv);
}
}
精彩评论