Std::vector loses last element throughout program

Hi there rooters,

I am passing a std::vector from a separate class back to my main and everything seems to work. However, after a few lines of code, the last element seems to be missing. I don’t understand what’s going on and would appreciate some help/insight.

Here is a snippet of my code.

void coincidences()
   bool peaks = 1;
   std::vector<double *> peakPosX;
   TFile* fout;
   if (peaks) {
      peakPosX = pf.GetPeakPos();  //pf is a member of my own class to find peaks in a spectrum 
      cout << peakPosX[5][0] << endl;
   fout = new TFile(foutName,"recreate");
   for (int i  = 0; i < nchannels; i++) {
      if (peaks) {
         //draw two histograms in same canvas, save that canvas and close it
         printf("(%d) position of peaks in channel %d: %f\n",i,channels[i],peakPosX[i][0]);


(5) position of peaks in channel 349: 0.000000

There really is nothing more of interest inbetween and I don’t understand why this last element of my vector peakPosX is lost. Do you have any ideas?

Best regards

ROOT Version: 6.18/04
Platform: Centos 7

I am missing some details. Did you really mean:

std::vector<double *> peakPosX;


std::vector<double> *peakPosX = nullptr;

(ROOT can not store std::vector<double *> as it can not know how many double are pointee to by the double*

Either way, we would need more information (for example a complete reproducing code) to understand where the problem.


Thanks for your reply Philippe,
I actually do mean
std::vector<double *> peakPosX;
To explain: I use the TSpectrum::SearchHighRes() method to find peaks in my spectrum and get their x values with the TSpectrum::GetPositionX() method, which returns a double * with all x positions. This happens in my own class where I store these coordinates in a separate vector via
where s is my TSpectrum.
This seems to work fine since I write the results into a separate file and there everything is saved correctly.

If that is true, then I wonder whether this could be the reason for why my method won’t work properly. But wouldn’t that mean that my whole method shouldn’t work at all?

Thanks so far,

That can work properly if there is no attempt to store the vector inside a ROOT file.

Oh okay, thank you for the information!
I guess I will have to work around this by implementing a 2D vector or something similar.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.