12 #include <gsl/gsl_blas.h>
13 #include <gsl/gsl_statistics_double.h>
14 #include <gsl/gsl_sort_vector.h>
38 a=(
double *)fftw_malloc(size*
sizeof(
double));
43 catch(std::exception &e){
44 std::stringstream msg;
45 msg<<
"QVector::ArrayAlloc: Attempt to allocate double array of size "
46 << size <<
" failed!" << std::endl;
47 msg<<
"\n\tMessage: " << e.what();
51 std::stringstream msg;
52 msg<<
"QVector::ArrayAlloc: Attempt to allocate double array of size "
53 << size <<
" failed!" << std::endl;
60 void QVector::ArrayCopy(
const double* orig,
const UInt_t origStride,
double* dest,
const UInt_t destStride,
const UInt_t size)
62 if(size > 0 && orig && dest) {
63 if(origStride == 1 && destStride == 1)
64 memcpy(dest,orig,
sizeof(
double)*size);
66 for(UInt_t i =0; i < size; i++) {
67 dest[i*destStride] = orig[i*origStride];
124 : fDataOwner(dataOwner),
179 }
else if(newSize != (UInt_t)
fSize) {
181 "Not data owner, are you trying to resize a "
182 "QVector get from a QVector(Const)View?") );
210 fMathVec = (gsl_vector*) malloc(
sizeof(gsl_vector));
226 std::stringstream stream;
227 stream<<
"Size: "<<
fSize<<
" Index: "<<i;
236 std::stringstream stream;
237 stream<<
"Size: "<<
fSize<<
" Index: "<<i;
283 gsl_vector_add_constant(
fMathVec,v);
290 gsl_vector_add_constant(
fMathVec,-v);
377 for(
size_t i = 0; i <
Size(); i++) {
384 int newSize =
Size()/rebin;
385 double norm = (sum ? 1. : 1./rebin);
386 for(
int i=0;i<newSize;i++){
387 (*this)[i]=norm*(*this)[rebin*i];
388 for(
int j=1;j<rebin;j++){
389 (*this)[i]+=norm*(*this)[rebin*i+j];
397 bool gtz = nstep > 0 ? true :
false;
398 if(nstep == 0 ||
Size() == 0)
return *
this;
399 unsigned int unstep =
abs(nstep) %
Size();
402 for(
unsigned int i = 0; i < unstep; i++) rest[i] = (*
this)[
Size() - unstep + i];
403 for(
int i =
Size() - unstep - 1; i >= 0 ; i--) (*
this)[i + unstep] = (*this)[i];
404 for(
unsigned int i = 0; i < unstep; i++) (*
this)[i] = rest[i];
406 for(
unsigned int i = 0; i < unstep; i++) rest[i] = (*
this)[i];
407 for(
unsigned int i = unstep; i <
Size(); i++) (*
this)[i - unstep] = (*this)[i];
408 for(
unsigned int i = 0; i < unstep; i++) (*
this)[i +
Size() - unstep] = rest[i];
415 int nstep = (int) ceil(fstep);
417 double fracs = nstep - fstep;
418 double zero = (*this)[0];
419 for(
int i = 0; i < (int)
Size() - 1; i++) {
420 (*this)[i] = (*this)[i]*(1.-fracs) + fracs*(*
this)[i+1];
422 (*this)[
Size()-1] = (*this)[
Size()-1]*(1.-fracs) + fracs*zero;
429 for(UInt_t i=0;i<
vec.
Size();i++)
430 vec[i]=TMath::Abs(
vec[i]);
438 if(!asc) gsl_vector_reverse(
fMathVec);
444 for(UInt_t i =
Size()-1; i > 0; i--)
445 (*
this)[i] = (*this)[i] - (*this)[i-1] ;
446 (*this)[0] = (*this)[1];
463 for(UInt_t i = 0; i < me.
Size()-1; i ++) {
464 result[i] = (me[i+1]-me[i])/delta;
466 result[me.
Size()-1] = result[me.
Size()-2];
473 result[0] = (-3.*me[0]+4.*me[1]-me[2]) / (2.*delta);
474 for(UInt_t i = 1; i < me.
Size()-1; i ++) {
475 result[i] = (me[i+1]-me[i-1]) / (2.*delta);
477 result[me.
Size()-1] = (me[me.
Size()-3]-4.*me[me.
Size()-2]+3.*me[me.
Size()-1]) / (2.*delta);
485 result[0] = (-3.*me[0]+4.*me[1]-me[2]) / (2.*delta);
486 result[1] = (me[2]-me[0]) / (2.*delta);
487 for(UInt_t i = 2; i < me.
Size(); i ++) {
488 result[i] = (me[i-2]-4.*me[i-1]+3.*me[i]) / (2.*delta);
498 for(i = 0; i < me.
Size()-2; i++) {
499 result[i] = (-3.*me[i]+4.*me[i+1]-me[i+2]) / (2.*delta);
502 result[i] = (me[i+1]-me[i-1]) / (2.*delta);
504 result[i] = (me[i-2]-4.*me[i-1]+3.*me[i]) / (2.*delta);
511 const UInt_t size = me.
Size();
513 result[0] = (-25.*me[0]+48.*me[1]-36.*me[2]+16.*me[3]-3.*me[4]) / (12.*delta);
514 result[1] = (-3.*me[0]-10.*me[1]+18.*me[2]-6.*me[3]+1.*me[4]) / (12.*delta);
515 for(UInt_t i = 2; i < size-2; i++) {
516 result[i] = (me[i-2]-8.*me[i-1]+8.*me[i+1]-me[i+2]) / (12.*delta);
518 result[size-2] = (3.*me[size-1]+10.*me[size-2]-18.*me[size-3]+6.*me[size-4]-1.*me[size-5]) / (12.*delta);
519 result[size-1] = (25.*me[size-1]-48.*me[size-2]+36.*me[size-3]-16.*me[size-4]+3.*me[size-5]) / (12.*delta);
527 const UInt_t size = me.
Size();
529 result[0] = (-25.*me[0]+48.*me[1]-36.*me[2]+16.*me[3]-3.*me[4]) / (12.*delta);
530 result[1] = (-3.*me[0]-10.*me[1]+18.*me[2]-6.*me[3]+1.*me[4]) / (12.*delta);
531 for(UInt_t i = 2; i < 4; i++) {
532 result[i] = (me[i-2]-8.*me[i-1]+8.*me[i+1]-me[i+2]) / (12.*delta);
534 for(UInt_t i = 4; i < size; i++)
535 result[i] = (25.*me[i]-48.*me[i-1]+36.*me[i-2]-16.*me[i-3]+3.*me[i-4]) / (12.*delta);
544 const UInt_t size = me.
Size();
546 result[0] = (-3.*me[0]+4.*me[1]-me[2]) / (2.*delta);
547 for(UInt_t i = 1; i < size; i++)
548 result[i] = (me[i]-me[i-1])*2./delta - result[i-1];
555 (*this)[0] += constTerm;
556 for(UInt_t i = 1; i <
Size(); i++)
557 (*
this)[i] += (*this)[i-1];
574 Form(
"Cannot reshape vector of size %ui into matrix of size %uix%ui!",
Size(),n,m));
577 "Cannot reshape vector with a stride greater than 1!");
588 Form(
"Cannot reshape vector of size %ui into matrix of size %uix%ui!",
Size(),n,m));
591 "Cannot reshape vector with a stride greater than 1!");
620 return sqrt((*
this)*(*
this));
627 UInt_t last = first + nelem;
628 for(UInt_t i = first; i < last; i++) sum += this->
operator[](i);
635 double mean =
Sum(nelem, first);
637 double rms = gsl_stats_variance_m(&(
fMathVec->data[first]), 1,nelem, mean);
648 gsl_vector* copy = gsl_vector_alloc(
Size());
650 gsl_sort_vector(copy);
652 if (
Size() % 2 == 1) {
653 median = gsl_vector_get(copy, (
Size() - 1) / 2);
656 median = ( gsl_vector_get(copy,
Size() / 2 - 1)
657 + gsl_vector_get(copy,
Size() / 2 ) ) / 2.0;
661 gsl_vector_free(copy);
672 for (
unsigned int i = 0; i <
Size(); ++i) {
673 absDeviations[i] = fabs(
operator[](i) - median);
684 gsl_vector_const_view vectorview = gsl_vector_const_subvector(
fMathVec,first,nelem);
685 return gsl_vector_max_index(&(vectorview.vector))+first;
692 gsl_vector_const_view vectorview = gsl_vector_const_subvector(
fMathVec,first,nelem);
693 return gsl_vector_min_index(&(vectorview.vector))+first;
702 if(start+size >
Size())
704 "Sub vector would exceed beyond original vector!"));
705 stride = (stride < 1 ? 1 : stride);
711 if(start+size >
Size())
713 "Sub vector would exceed beyond original vector!"));
714 stride = (stride < 1 ? 1 : stride);
731 for (UInt_t i=0;i<
Size();i++){
732 if(i!=0)s+=std::string(
",");
734 snprintf(d,20,
"%.10g",
operator[](i));
737 s+=std::string(
"}'");
746 TH1D* h =
new TH1D(
"Diana::QVector",
"",
fSize,-0.5,
double(
fSize)-0.5);
748 for(UInt_t i = 0; i <
fSize; i++)
749 h->SetBinContent(i+1,this->operator[](i));
750 h->SetBit(kCanDelete);
763 const QVector& timeline = *
this;
764 TGraph* graph =
new TGraph(timeline.
Size());
765 for(UInt_t i = 0; i < timeline.
Size(); i++)
767 graph->SetPoint(i,
double(i)/samplingFrequency,timeline[i]*scale);
774 s<<
"Size: "<<this->
Size()<<
"; Elements: ";
775 for (UInt_t i = 0; i < this->
Size(); i++) s<<this->
operator[](i)<<
" ";
787 Diana::QVector
operator+(
double t,
const Diana::QVector &v){
788 Diana::QVector tmp(v);
793 Diana::QVector
operator-(
double t,
const Diana::QVector &v){
794 Diana::QVector tmp(v);
798 Diana::QVector
operator*(
double t,
const Diana::QVector &v){
799 Diana::QVector tmp(v);
804 Diana::QVector
operator/(
double t,
const Diana::QVector &v){
806 Diana::QVector tmp(v.Size());
811 Diana::QVector
abs(
const Diana::QVector &v){
815 Diana::QVector
sin(
const Diana::QVector &v) {
816 Diana::QVector tmp(v.Size());
817 for(UInt_t i=0;i<v.Size();i++) tmp[i]=
sin(v[i]);
821 Diana::QVector
cos(
const Diana::QVector &v) {
822 Diana::QVector tmp(v.Size());
823 for(UInt_t i=0;i<v.Size();i++) tmp[i]=
cos(v[i]);
827 Diana::QVector
tan(
const Diana::QVector &v) {
828 Diana::QVector tmp(v.Size());
829 for(UInt_t i=0;i<v.Size();i++) tmp[i]=
tan(v[i]);
833 Diana::QVector
exp(
const Diana::QVector &v) {
834 Diana::QVector tmp(v.Size());
835 for(UInt_t i=0;i<v.Size();i++) tmp[i]=
exp(v[i]);
839 Diana::QVector
log(
const Diana::QVector &v) {
840 Diana::QVector tmp(v.Size());
841 for(UInt_t i=0;i<v.Size();i++) tmp[i] = TMath::Log(v[i]);
845 Diana::QVector
log10(
const Diana::QVector &v) {
846 Diana::QVector tmp(v.Size());
847 for(UInt_t i=0;i<v.Size();i++) tmp[i] = TMath::Log10(v[i]);
852 Diana::QVector
pow(
const Diana::QVector &v,
double p) {
853 Diana::QVector tmp(v.Size());
854 for(UInt_t i=0;i<v.Size();i++) tmp[i]=TMath::Power(v[i],p);
858 Diana::QVector
pow(
double p,
const Diana::QVector &v) {
859 Diana::QVector tmp(v.Size());
860 for(UInt_t i=0;i<v.Size();i++) tmp[i]=TMath::Power(p,v[i]);
864 Diana::QVector
pow(
const Diana::QVector &u,
const Diana::QVector &v) {
865 if(u.Size()!=v.Size())
867 "Cannot raise a vector to the power of another vector if the sizes do not match!");
868 Diana::QVector tmp(v.Size());
869 for(UInt_t i=0;i<v.Size();i++) tmp[i]=TMath::Power(u[i],v[i]);
874 double max(
const Diana::QVector &v){
878 double min(
const Diana::QVector &v){
#define Q_BEGIN_NAMESPACE
Diana::QVector operator/(double t, const Diana::QVector &v)
Diana::QVector tan(const Diana::QVector &v)
QObjectImp(Diana::QVector)
Diana::QVector operator-(double t, const Diana::QVector &v)
Diana::QVector pow(const Diana::QVector &v, double p)
Diana::QVector log(const Diana::QVector &v)
Diana::QVector operator*(double t, const Diana::QVector &v)
Diana::QVector sin(const Diana::QVector &v)
Diana::QVector abs(const Diana::QVector &v)
Q_END_NAMESPACE Diana::QVector operator+(double t, const Diana::QVector &v)
Diana::QVector log10(const Diana::QVector &v)
double min(const Diana::QVector &v)
Diana::QVector cos(const Diana::QVector &v)
Diana::QVector exp(const Diana::QVector &v)
double max(const Diana::QVector &v)
error class with error type and description
Interface for matrices in Diana analysis.
void SetRow(UInt_t nrow, const QVector &vec)
set row to specific vectors
Interface for raw daq vectors in Diana.
Interface for vectors in Diana analysis.
const QVector & Rebin(const int rebin, bool sum=false)
Rebin the vector.
const QVector & operator*=(const double t)
multiplication by scalar
QVector Derivative3PF(const double delta=1.) const
compute derivative with 3 point interpolation, forward mode
QVector operator-() const
revert sign to all components
QVector Div(const QVector &other) const
multiplication element by element
static void ArrayCopy(const double *orig, const UInt_t origStride, double *dest, const UInt_t destStride, const UInt_t size)
const QVector & Differentiate()
make vector derivative
UInt_t Size() const
size of QVector
QVector operator+(const QVector &other) const
sum vector
const QVector & operator+=(const double v)
sum scalar to all elements
virtual ~QVector()
destructor
QVector DerivativeEulero(const double delta=1.) const
compute derivative with Eulero interpolation, backward mode
UInt_t fSize
size of the array
static void ArrayFree(double *array)
QVector Mult(const QVector &other) const
multiplication element by element
double GetRMS() const
Get RMS.
QVector Derivative5P(const double delta=1.) const
compute derivative with 5 point interpolation, symmetric
void Draw(Option_t *option="")
Draw root.
double & operator[](const UInt_t i)
retrieve an element
QVector Derivative2P(const double delta=1.) const
compute derivative with 2 point interpolation, uses Differentiate()
virtual void Dump(std::ostream &o) const
Dump object to stream.
void SetMathVector() const
const bool fDataOwner
if this vector onws its data
QVector Derivative3PB(const double delta=1.) const
compute derivative with 3 point interpolation, backward mode
UInt_t fStride
data stride
QMatrix T() const
transpose
TGraph * GetGraph(double samplingFrequency=1., double scale=1.) const
Get a root TGraph (owned by the caller)
static double * ArrayAlloc(const UInt_t size)
const QVector & Reverse()
time reversal of the vector
QVector GetSubVector(UInt_t size, UInt_t start=0, UInt_t stride=1)
double * GetArray() const
get an array of doubles, owned by the caller!
double GetMedian() const
Get median.
QVector Derivative2PF(const double delta=1.) const
compute derivative with forward 2 point interpolation
const QVector & ShiftReal(const double fstep)
Cyclic shift of vector by a real number, first shift by the integer part of fstep then linear interpo...
QVector Abs() const
Return a vector of the absolute values.
const QVector & Shift(const int nstep)
Cyclic shift of vector.
void Append(const double val)
append
QMatrix Reshape(UInt_t n, UInt_t m)
reshape the vector into a matrix
double Norma() const
norma
double operator*(const QVector &other) const
scalar product
void SetArray(const double *orig, const UInt_t size)
get a constant pointer to the content
QVector()
default constructor
QVector Derivative5PB(const double delta=1.) const
compute derivative with 5 point interpolation, backward mode
double GetMedianAbsoluteDeviation() const
Get median absolute deviation.
const QVector & operator=(const QVector &orig)
copy the content from another QVector
const QVector & Integrate(const double constTerm=0)
Integrate vector.
const QVector & Sort(const bool asc=true)
sort this vector
QVector Derivative3P(const double delta=1.) const
compute derivative with 3 point interpolation, symmetric
double Sum() const
sum elements
QVector operator/(double t) const
divide by scalar
const QVector & operator/=(const double v)
divide by scalar
void Resize(const UInt_t newsize)
resize a QVector
std::string sqlString() const
const QVector & operator-=(const double v)
subtract scalar to all elements
void Initialize(const double val=0)
initialize elements (default to 0)
gsl_vector * fMathVec
temporary gsl_vector to use the gsl library
UInt_t GetMaxIndex() const
get maximum element index
UInt_t GetMinIndex() const
get minimum element index