Efficient Copy of UShort_t * to TVectorD

I have packed arrays of UShort_t in a TTree and I wish to unpack them into TVectorD for my analysis.

I have code that works, but it just has a loop and copies the elements one-by-one:

TVectorD get_trace(UShort_t * digiWF)
{ Int_t recL = 1024;
  Int_t chan_offset = -1;
  // some code here determines a proper chan_offset value

  TVectorD tv(recL);
  for(Int_t i=0;i<recL;i++) {
    tv[i] = digiWF[i+chan_offset*recL];
  return tv;

I tried optimizing a bit by using std::copy instead of an explicit loop:

TVectorD tv(recL); std::copy(digiWF+chan_offset*recL, digiWF+chan_offset*(recL+1), tv.GetMatrixArray()); return tv;
but it looks like it doesn’t work, probably because the type-conversion is not done properly. I also tried just using a TVectorD constructor:

But here I get a compiler error about the types (recall digiWF is a UShort_t * not a Double_t *).

My profiling tells me that this function represents a good chunk of my runtime, so I’d like to get it as tight as possible. Can anyone advise on efficiently copying some UShort_t array into a TVectorD?


Never mind, turns out std::copy does work, I just messed up the indices of the packed arrays. It should have been:

TVectorD tv(recL); std::copy(digiWF+chan_offset*recL, digiWF+(chan_offset+1)*recL, tv.GetMatrixArray()); return tv;

Unfortunately it’s no faster, because apparently std::copy only uses bulk-copy/move functions if the types are trivial and have no conversions. In my case, std::copy falls back on a simple loops, which was what I had originally. Probably to make real progress I’d have to question why I am using a TVectorD in the first place, since the original data are UShort_t, but that’s currently baked very solidly into my code.