1 #include "TGraphAligned.h" 16 #define ALIGNMENT TGRAPH_ALIGNED_ALIGNMENT 30 if (!CtorAllocate())
return;
31 FillZero(0, fNpoints);
39 memcpy(fX,g->GetX(),GetN()*
sizeof(double));
40 memcpy(fY,g->GetY(),GetN()*
sizeof(double));
49 TNamed::operator=(gr);
50 TAttLine::operator=(gr);
51 TAttFill::operator=(gr);
52 TAttMarker::operator=(gr);
54 fNpoints = gr.fNpoints;
55 fMaxSize = gr.fMaxSize;
60 if (!fFunctions->IsEmpty()) {
61 fFunctions->SetBit(kInvalidObject);
65 while ((obj = fFunctions->First())) {
66 while (fFunctions->Remove(obj)) { }
75 fFunctions = (TList*)gr.fFunctions->Clone();
77 else fFunctions =
new TList;
80 if (fHistogram)
delete fHistogram;
81 if (gr.fHistogram) fHistogram =
new TH1F(*(gr.fHistogram));
84 fMinimum = gr.fMinimum;
85 fMaximum = gr.fMaximum;
94 err = posix_memalign((
void**) &fX, ALIGNMENT, fNpoints *
sizeof(Double_t));
98 err = posix_memalign((
void**) &fY, ALIGNMENT, fNpoints *
sizeof(Double_t));
104 Int_t n = gr.GetN() *
sizeof(Double_t);
106 memcpy(fX, gr.fX, n);
107 memcpy(fY, gr.fY, n);
124 if (!CtorAllocate())
return;
125 n = fNpoints *
sizeof(Double_t);
136 Bool_t TGraphAligned::CtorAllocate()
149 err = posix_memalign((
void**) &fX, ALIGNMENT, fNpoints *
sizeof(Double_t));
154 err = posix_memalign((
void**) &fY, ALIGNMENT, fNpoints *
sizeof(Double_t));
161 if (fX == NULL || fY == NULL)
163 fprintf(stderr,
"Could not allocate aligned memory in TGraphAligned::CtorAllocate()\n");
183 void TGraphAligned::CopyAndRelease(Double_t **newarrays, Int_t ibegin, Int_t iend,
186 CopyPoints(newarrays, ibegin, iend, obegin);
206 Double_t * new_fX = NULL;
207 err = posix_memalign((
void**) &new_fX, ALIGNMENT, fMaxSize *
sizeof(Double_t));
211 Double_t * new_fY = NULL;
212 err = posix_memalign((
void**) &new_fY, ALIGNMENT, fMaxSize *
sizeof(Double_t));
217 memcpy(new_fX, fX, gr.GetN() *
sizeof(Double_t));
222 memcpy(new_fY, fY, gr.GetN() *
sizeof(Double_t));
230 Double_t** TGraphAligned::AllocateAlignedArrays(Int_t Narrays, Int_t arraySize)
237 Double_t **newarrays =
new Double_t*[Narrays];
240 for (Int_t i = 0; i < Narrays; ++i)
247 for (Int_t i = 0; i < Narrays; ++i)
249 err = posix_memalign((
void**) &newarrays[i], ALIGNMENT, arraySize *
sizeof(Double_t));
257 fMaxSize = arraySize;
262 void TGraphAligned::undBize()
266 for (
int i = 0; i < n; i++)
268 fY[i] = TMath::Power(10,fY[i]/10);
273 Double_t TGraphAligned::getSumV2(Int_t istart, Int_t iend)
const 275 aligned_double_v v = GetY();
276 __builtin_prefetch(v);
278 int real_start = istart >= 0 ? TMath::Min(istart, N-1) : TMath::Max(0, N + istart) ;
279 int real_end = iend >= 0 ? TMath::Min(iend, N-1) : TMath::Max(0, N + iend) ;
284 for (
int i = real_start; i <= real_end; i++)
293 void TGraphAligned::getMeanAndRMS(Double_t * mean, Double_t * rms, Int_t istart, Int_t iend)
const 295 aligned_double_v v = GetY();
296 __builtin_prefetch(v);
298 int real_start = istart >= 0 ? TMath::Min(istart, N-1) : TMath::Max(0, N + istart) ;
299 int real_end = iend >= 0 ? TMath::Min(iend, N-1) : TMath::Max(0, N + iend) ;
307 for (
int i = real_start; i <= real_end; i++)
314 if (mean) *mean = mn;
319 *rms = sqrt(sum2/N - mn2);
325 double * TGraphAligned::getMoments(
int N,
double origin,
double * moment)
const 327 if (!moment) moment =
new double[N];
330 for (
int i = 0; i < N; i++ )
334 for (
int j = 0; j < GetN(); j++)
336 sum += TMath::Power(fX[j] - origin, n) * fY[j];
348 void TGraphAligned::dBize(
double mindB)
352 for (
int i = 0; i < n; i++)
354 fY[i] = TMath::Max(mindB, 10 * TMath::Log10(fY[i]));
360 Double_t TGraphAligned::pk2pk(Int_t nth_max, Int_t nth_min, Int_t * location_max, Int_t * location_min, Int_t istart, Int_t iend)
const 363 int start = istart < 0 ? GetN() + istart : istart;
364 int end = iend < 0 ? GetN() + iend : iend;
370 const double * y = GetY();
374 if (nth_max == 0 && nth_min == 0)
376 for (
int i = start; i <= end; i++)
379 if (max_index < 0 || val > max)
385 if (min_index < 0 || val < min)
392 if (location_max) *location_max = max_index;
393 if (location_min) *location_min = min_index;
401 memcpy(ycopy, y+start, N *
sizeof(
double));
403 std::nth_element(ycopy, ycopy + N-1-nth_max, ycopy + N);
404 std::nth_element(ycopy, ycopy + nth_min, ycopy + N);
406 max = ycopy[N-1-nth_max];
407 min = ycopy[nth_min];
410 if (location_max || location_min)
412 bool need_max = location_max;
413 bool need_min = location_min;
414 for (
int i =start; i <=end; i++)
416 if (location_max && y[i] == max)
422 if (location_min && y[i] == min)
428 if (!need_min && !need_max)
break;
436 Double_t TGraphAligned::peakVal(Int_t * location, Int_t istart, Int_t iend,
bool abs)
const 439 int start = istart < 0 ? GetN() + istart : istart;
440 int end = iend < 0 ? GetN() + iend : iend;
444 const double * y = GetY();
446 for (
int i = start; i <= end; i++)
448 double val = abs ? fabs(y[i]) : y[i];
449 if (max_index < 0 || val > max)
456 if (location) *location = max_index;
466 out =
new TH1I(TString::Format(
"%s_value_hist", GetName()), TString::Format(
"Values for %s", GetTitle()), nbins, GetMinimum(), GetMaximum());
471 out->SetBins(nbins, GetMinimum(), GetMaximum());
477 for (
int i = 0; i < GetN(); i++)
483 for (
int i = 0; i < GetN(); i++)
485 out->Fill(GetY()[i], w? w[i]/sumw2 : 1./GetN());
493 void TGraphAligned::zeroMean()
496 for (
int i = 0; i < GetN(); i++) mean+= GetY()[i];
498 for (
int i = 0; i < GetN(); i++) GetY()[i]-=mean;
501 void TGraphAligned::shift(
int nsamp,
bool zero)
505 std::rotate(GetY(), GetY()+((GetN() + nsamp) % GetN()), GetY() + GetN());
513 for (
int i = GetN()-nsamp; i < GetN(); i++) GetY()[i] = 0;
517 for (
int i = 0; i < nsamp; i++) GetY()[i] = 0;
523 void TGraphAligned::setPlottingLimits(
double mult,
bool sym,
double dt)
527 pk2pk(0,0,&imax,&imin);
529 double val_max = GetY()[imax];
530 double val_min = GetY()[imin];
532 double abs_max = TMath::Max(val_max,-val_min);
535 GetHistogram()->SetMaximum(mult * ( sym ? abs_max : val_max));
536 GetHistogram()->SetMinimum(mult * ( sym ? -abs_max : val_min));
540 double x0 = val_max > -val_min ? GetX()[imax] : GetX()[imin];
541 GetXaxis()->SetLimits(x0-dt, x0+dt);
545 GetXaxis()->SetLimits(GetX()[0], GetX()[GetN()-1]);
549 void TGraphAligned::setBelow(
double val,
bool abso,
double to)
551 for (
int i = 0; i < GetN(); i++)
553 if (GetY()[i] < val && (!abso || GetY()[i] > -val))
TH1 * valueHist(int nbins=100, const double *weights=0, TH1 *out=0) const