RootTools.cxx
1 #include "RootTools.h"
2 
3 
4 
5 
6 
7 //---------------------------------------------------------------------------------------------------------
14 Double_t RootTools::getSumOfYVals(TGraph* gr){
15  Double_t sum = 0;
16  for(int i=0; i<gr->GetN(); i++){
17  sum += gr->GetY()[i];
18  }
19  return sum;
20 }
21 
22 
23 
24 
25 
26 //---------------------------------------------------------------------------------------------------------
36 void RootTools::printArray(int n, double* array, TString delimiter, TString start ,TString end){
37  std::cout << start.Data();
38  for(int i=0; i<n; i++){
39  std::cout << array[i];
40  if(i<n-1) std::cout << delimiter.Data();
41  }
42  std::cout << end.Data();
43 }
44 
45 
46 
47 
48 
49 //---------------------------------------------------------------------------------------------------------
59 void RootTools::printYVals(TGraph* gr, TString delimiter, TString start, TString end){
60  printArray(gr->GetN(), gr->GetY(), delimiter, start, end);
61 }
62 
63 
64 
65 
66 
67 //---------------------------------------------------------------------------------------------------------
77 void RootTools::printXVals(TGraph* gr, TString delimiter, TString start, TString end){
78  printArray(gr->GetN(), gr->GetX(), delimiter, start, end);
79 }
80 
81 
82 
83 
84 
85 //---------------------------------------------------------------------------------------------------------
92 TGraph* RootTools::makeDerivativeTGraph(TGraph* gr){
93  TGraph* grDer = new TGraph();
94  Int_t nDer = gr->GetN()-1;
95  grDer->Set(nDer);
96  for(int samp=0; samp<nDer; samp++){
97  Double_t dy = gr->GetY()[samp+1]-gr->GetY()[samp];
98  Double_t dx = gr->GetX()[samp+1]-gr->GetX()[samp];
99  grDer->SetPoint(samp, gr->GetX()[samp], dy/dx);
100  }
101  return grDer;
102 }
103 
104 
105 
106 
107 
108 //---------------------------------------------------------------------------------------------------------
116  /* Unwraps a circularly correlated array so the n > 2 points get -ve values */
117  Int_t n = gr->GetN();
118  Double_t dt = gr->GetX()[1] - gr->GetX()[0];
119  TGraph* grUnwrapped = new TGraph();
120  grUnwrapped->Set(n);
121  Int_t samp2=0;
122  for(Int_t samp=n/2; samp<n; samp++){
123  grUnwrapped->SetPoint(samp2, (samp - n)*dt, gr->GetY()[samp]);
124  samp2++;
125  }
126  for(Int_t samp=0; samp<n/2; samp++){
127  grUnwrapped->SetPoint(samp2, samp*dt, gr->GetY()[samp]);
128  samp2++;
129  }
130 
131  return grUnwrapped;
132 }
133 
134 
135 
136 
137 
138 //---------------------------------------------------------------------------------------------------------
146 void RootTools::getMaxMin(TGraph* gr, Double_t& maxY, Double_t& minY){
147 
148  Double_t maxX=0, minX=0;
149  RootTools::getMaxMin(gr, maxY, maxX, minY, minX);
150 }
151 
152 
153 
154 
155 //---------------------------------------------------------------------------------------------------------
165 void RootTools::getMaxMin(TGraph* gr, Double_t& maxY, Double_t& maxX,
166  Double_t& minY, Double_t& minX){
167 
168  maxY=gr->GetY()[0];
169  maxX=gr->GetX()[0];
170  minY=gr->GetY()[0];
171  minX=gr->GetX()[0];
172 
173  for(int i=0; i<gr->GetN(); i++){
174  if(gr->GetY()[i] > maxY){
175  maxY = gr->GetY()[i];
176  maxX = gr->GetX()[i];
177  }
178  if(gr->GetY()[i] < minY){
179  minY = gr->GetY()[i];
180  minX = gr->GetX()[i];
181  }
182  }
183 }
184 
185 
186 
187 
188 
189 //---------------------------------------------------------------------------------------------------------
201 void RootTools::getMaxMinWithinLimits(TGraph* gr, Double_t& maxY, Double_t& maxX,
202  Double_t& minY, Double_t& minX,
203  Double_t lowerLimit, Double_t upperLimit){
204 
205  // Macro values from <cfloat> header
206  Double_t minPoss = -DBL_MAX;
207  Double_t maxPoss = DBL_MAX;
208  maxY=minPoss;
209  maxX=minPoss;
210  minY=maxPoss;
211  minX=maxPoss;
212 
213  for(int i=0; i<gr->GetN(); i++){
214  if(gr->GetX()[i] >= lowerLimit && gr->GetX()[i] <=upperLimit){
215  if(gr->GetY()[i] > maxY){
216  maxY = gr->GetY()[i];
217  maxX = gr->GetX()[i];
218  }
219  if(gr->GetY()[i] < minY){
220  minY = gr->GetY()[i];
221  minX = gr->GetX()[i];
222  }
223  }
224  }
225 
226  if(maxY==minPoss){
227  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << "!" << std::endl;
228  std::cerr << "Unable to find value in " << gr->GetName() << " > " << minPoss << std::endl;
229  }
230  if(minY==maxPoss){
231  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << "!" << std::endl;
232  std::cerr << "Unable to find value in " << gr->GetName() << " < " << maxPoss << std::endl;
233  }
234 }
235 
236 
237 
238 
239 
240 //---------------------------------------------------------------------------------------------------------
247 void RootTools::subtractOffset(TGraph* gr, Double_t offset){
248  for(int i=0; i<gr->GetN(); i++){
249  gr->GetY()[i] -= offset;
250  }
251 }
252 
253 
254 
255 
256 
257 //---------------------------------------------------------------------------------------------------------
267 Double_t RootTools::getDeltaAngleDeg(Double_t angle1, Double_t angle2){
268 
269  Double_t deltaAngle = angle1 - angle2;
270  Int_t loopCount = 0;
271  const Int_t maxLoopCount = 5;
272  if(deltaAngle > 180){
273  while(deltaAngle >= 180){
274  deltaAngle -= 360;
275  loopCount++;
276  if(loopCount >= maxLoopCount){
277  deltaAngle = -9999;
278  break;
279  }
280  }
281  }
282  else{
283  while(deltaAngle < -180){
284  deltaAngle += 360;
285  loopCount++;
286  if(loopCount >= maxLoopCount){
287  deltaAngle = -9999;
288  break;
289  }
290  }
291  }
292 
293  return deltaAngle;
294 }
295 
296 
297 
298 
299 
300 //---------------------------------------------------------------------------------------------------------
306 void RootTools::normalize(TGraph* gr){
307  double mean, rms;
308  normalize(gr, mean, rms);
309 }
310 
311 
312 
313 
314 
315 //---------------------------------------------------------------------------------------------------------
323 void RootTools::normalize(TGraph* gr, Double_t& mean, Double_t& rms){
324  RootTools::getMeanAndRms(gr, mean, rms);
325  if(rms>0){ // Don't make any NaNs
326  for(int i=0; i<gr->GetN(); i++){
327  gr->GetY()[i] -= mean;
328  gr->GetY()[i] /= rms;
329  }
330  }
331 }
332 
333 
334 
335 
336 
337 //---------------------------------------------------------------------------------------------------------
343 TGraph* RootTools::makeNormalized(TGraph* gr){
344  /* Copies the TGraph and normalizes that */
345  TGraph* grCopy = (TGraph*) gr->Clone();
346  normalize(grCopy);
347  return grCopy;
348 }
349 
350 
351 
352 
353 
354 //---------------------------------------------------------------------------------------------------------
362 TGraph* RootTools::makeNormalized(TGraph* gr, Double_t& mean, Double_t& rms){
363  /* Copies the TGraph and normalizes that */
364  TGraph* grCopy = (TGraph*) gr->Clone();
365  normalize(grCopy, mean, rms);
366  return grCopy;
367 }
368 
369 
370 
371 
372 
373 //---------------------------------------------------------------------------------------------------------
381 void RootTools::getMeanAndRms(TGraph* gr, Double_t& mean, Double_t& rms){
382  Double_t sum = 0;
383  Double_t square = 0;
384  for(int i=0; i<gr->GetN(); i++){
385  sum += gr->GetY()[i];
386  square += gr->GetY()[i]*gr->GetY()[i];
387  }
388  mean = sum/gr->GetN();
389  rms = TMath::Sqrt(square/gr->GetN() - mean*mean);
390 }
391 
392 
393 
394 
395 
396 //---------------------------------------------------------------------------------------------------------
404 Int_t RootTools::getIndexOfMaximum(Int_t len, Double_t* arr){
405  Double_t max=-DBL_MAX;
406  Int_t maxIndex = -1;
407  for(Int_t i=0; i < len; ++i){
408  if(arr[i] > max){
409  max = arr[i];
410  maxIndex = i;
411  }
412  }
413  if(maxIndex==-1){
414  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << "!" << std::endl;
415  std::cerr << "Could not find an index > " << max << std::endl;
416  }
417 
418  return maxIndex;
419 }
420 
421 
422 
423 
424 
425 //---------------------------------------------------------------------------------------------------------
432 std::vector<Int_t> RootTools::getIndicesOfNans(TGraph* gr){
433  std::vector<Int_t> nanIndices;
434  for(Int_t i=0; i<gr->GetN(); i++){
435  if(TMath::IsNaN(gr->GetY()[i])){
436  nanIndices.push_back(i);
437  }
438  }
439  return nanIndices;
440 }
441 
442 
443 
444 
445 
446 //---------------------------------------------------------------------------------------------------------
452 void RootTools::printTGraphInfo(TGraph* gr){
453  std::cout << "******************************************************************************" << std::endl;
454  std::cout << "RootTools::printTGraphValues(TGraph* gr = " << gr << "):" << std::endl;
455  std::cout << "Name: " << gr->GetName() << std::endl;
456  std::cout << "Title: " << gr->GetTitle() << std::endl;
457  std::cout << "Num points: " << gr->GetN() << std::endl;
458  std::cout << "Xvals: ";
459  for(Int_t i=0; i<gr->GetN(); i++){
460  std::cout << gr->GetX()[i];
461  if(i<gr->GetN()-1){
462  std::cout << ", ";
463  }
464  }
465  std::cout << std::endl;
466  std::cout << "Yvals: ";
467  for(Int_t i=0; i<gr->GetN(); i++){
468  std::cout << gr->GetY()[i];
469  if(i<gr->GetN()-1){
470  std::cout << ", ";
471  }
472  }
473  std::cout << std::endl;
474  std::cout << "******************************************************************************" << std::endl;
475 }
476 
477 
478 
479 
480 
481 //---------------------------------------------------------------------------------------------------------
490 TGraph* RootTools::makeSortedTGraph(TTree* tree, TString drawText, TString cutString, Double_t wrapValue){
491 
492  // Draw
493  const Int_t nEntries = tree->Draw(drawText, cutString, "goff"); // "goff" means graphics off
494 
495  // Sort
496  std::vector<Int_t> sortedIndices(nEntries);
497  TMath::Sort(nEntries, tree->GetV2(), &sortedIndices.front(), kFALSE);
498  std::vector<Double_t> newX(nEntries);
499  std::vector<Double_t> newY(nEntries);
500 
501  for(int i=0; i<nEntries; i++){
502  newX.at(i) = tree->GetV2()[sortedIndices.at(i)];
503  newY.at(i) = tree->GetV1()[sortedIndices.at(i)];
504  }
505 
506  // Unwrap data here so interpolation works smoothly
507  // will unwrap when getting entries from graph
508  if(wrapValue != 0){
509  for(int i=1; i<nEntries; i++){
510  // If y[i] >> y[i-1] => then we went below zero to the wrap value
511  // can only modify y[i], so we should subtract wrapValue enough times
512  // that the graph becomes smooth
513  while (newY.at(i) - newY.at(i-1) > wrapValue/2){
514  newY.at(i) -= wrapValue;
515  }
516 
517  // If y[i] << y[i-1] => then we went add zero to the wrap value
518  // can only modify y[i], so we should add wrapValue enough times
519  // that the graph becomes smooth
520  while (newY.at(i) - newY.at(i-1) < -wrapValue/2){
521  newY.at(i) += wrapValue;
522  }
523 
524  }
525  }
526 
527  // sorted TGraph
528  TGraph* gr(new TGraph(nEntries,&newX.front(), &newY.front()));
529  TString title = cutString.Length() == 0 ? drawText : drawText + ", " + cutString;
530  gr->SetTitle(title);
531  TObjArray* tokens = drawText.Tokenize(":");
532  gr->GetXaxis()->SetTitle(((TObjString*) tokens->At(1))->String());
533  gr->GetYaxis()->SetTitle(((TObjString*) tokens->At(0))->String());
534  delete tokens;
535 
536  return gr;
537 }
538 
539 
540 
541 
542 
543 //---------------------------------------------------------------------------------------------------------
553 TH1D* RootTools::plotsZaxisDist(TH2* h2, TString hName, Int_t nBins, Double_t xMin, Double_t xMax){
554  TH1D* h = new TH1D(hName, hName, nBins, xMin, xMax);
555  for(int xBin=1; xBin<=h2->GetNbinsX(); xBin++){
556  for(int yBin=1; yBin<=h2->GetNbinsY(); yBin++){
557  Double_t val = h2->GetBinContent(xBin, yBin);
558  h->Fill(val);
559  }
560  }
561  return h;
562 }
563 
564 
565 
566 
567 
568 //---------------------------------------------------------------------------------------------------------
576 void RootTools::zeroPadTGraph(TGraph* gr, Int_t newLen, Double_t dt){
577  Int_t oldLen = gr->GetN();
578  gr->Set(newLen);
579  if(dt >= 0){
580  Double_t x0 = gr->GetX()[oldLen-1];
581  for(Int_t samp=oldLen; samp<newLen; samp++){
582  gr->GetX()[samp] = x0 + samp*dt;
583  }
584  }
585 }
586 
587 
588 
589 
590 
591 //---------------------------------------------------------------------------------------------------------
600 TGraph* RootTools::makeLinearlyInterpolatedGraph(TGraph* grIn, Double_t dt){
601 
602  Int_t nIn = grIn->GetN();
603  Int_t newPoints = Int_t((grIn->GetX()[nIn-1] - grIn->GetX()[0])/dt) + 1;
604  std::vector<Double_t> newTimes(newPoints);
605  std::vector<Double_t> newVolts(newPoints);
606 
607  Double_t time = grIn->GetX()[0];
608  Double_t lastTime = grIn->GetX()[nIn-1];
609 
610  Int_t sampOut = 0; // Will iterate through
611  Int_t sampIn = 0;
612 
613  Double_t y1 = grIn->GetY()[sampIn];
614  Double_t x1 = grIn->GetX()[sampIn];
615  Double_t x2 = grIn->GetX()[sampIn+1];
616  Double_t m = (grIn->GetY()[sampIn+1]-y1)/(grIn->GetX()[sampIn+1]-x1);
617 
618  while(time < lastTime){
619  if(time > x2){
620 
621  Int_t countLoop = 0;
622  while(time > x2){
623  if(sampIn < (nIn - 1)){ // if last point is equal don't need to increase?
624  sampIn++;
625  }
626  x2 = grIn->GetX()[sampIn+1];
627  countLoop++;
628  if(countLoop==1000){
629  std::cerr << "You can't do logic very well" << std::endl;
630  std::cerr << time << "\t" << x1 << "\t" << x2 << "\t" << nIn << "\t" << sampIn << std::endl;
631  exit(0);
632  }
633  }
634  x1 = grIn->GetX()[sampIn];
635  y1 = grIn->GetY()[sampIn];
636 
637  m = (grIn->GetY()[sampIn+1]-y1)/(x2-x1);
638 
639  }
640 
641  // std::cout << time << "\t" << x1 << "\t" << x2;
642 
643  // if(time < x1 || time > x2 ){
644  // std::cout << "!!!!!!!!!!!!!!!!!!!!!!!";
645  // }
646  // std::cout << std::endl;
647 
648  Double_t newY = y1 + m * (time - x1);
649 
650  newTimes.at(sampOut) = time;
651  newVolts.at(sampOut) = newY;
652  sampOut++;
653  time += dt;
654  }
655 
656  TGraph* grOut = new TGraph(newTimes.size(), &newTimes[0], &newVolts[0]);
657  // std::cout << grOut->GetN() - newPoints << " " << grIn->GetN() << " " << sampIn << " "
658  // << grIn->GetX()[0] << " " << grIn->GetX()[nIn-1] << " " << dt << std::endl;
659 
660 
661 
662 
663  return grOut;
664 }
665 
666 
667 
668 
669 
670 //---------------------------------------------------------------------------------------------------------
679 TGraph* RootTools::interpolateWithStartTime(TGraph* grIn, Double_t startTime, Double_t dt, Int_t nSamp){
680 
681  std::vector<Double_t> newTimes = std::vector<Double_t>(nSamp, 0);
682  std::vector<Double_t> newVolts = std::vector<Double_t>(nSamp, 0);
683  Double_t thisStartTime = grIn->GetX()[0];
684  Double_t lastTime = grIn->GetX()[grIn->GetN()-1];
685 
686 
687  // Quantizes the start and end times so data poInt_ts lie at Int_teger multiples of nominal sampling
688  // startTime = correlationDeltaT*TMath::Nint(startTime/correlationDeltaT + 0.5);
689  // lastTime = correlationDeltaT*TMath::Nint(lastTime/correlationDeltaT - 0.5);
690  startTime = dt*TMath::Nint(startTime/dt + 0.5);
691  lastTime = dt*TMath::Nint(lastTime/dt - 0.5);
692 
693  //ROOT Int_terpolator object constructor takes std::vector objects
694  std::vector<Double_t> tVec(grIn->GetX(), grIn->GetX() + grIn->GetN());
695  std::vector<Double_t> vVec(grIn->GetY(), grIn->GetY() + grIn->GetN());
696 
697  // This is ROOT's Int_terpolator object
698  ROOT::Math::Interpolator chanInterp(tVec,vVec,ROOT::Math::Interpolation::kAKIMA);
699 
700  // Put new data Int_to arrays
701  Double_t time = startTime;
702  for(Int_t samp = 0; samp < nSamp; samp++){
703  newTimes.at(samp) = time;
704  if(time >= thisStartTime && time <= lastTime){
705  newVolts.at(samp) = chanInterp.Eval(time);
706  }
707  else{
708  newVolts.at(samp) = 0;
709  }
710  time += dt;
711  }
712  return new TGraph(nSamp, &newTimes[0], &newVolts[0]);
713 
714 }
715 
716 
717 
718 
719 //---------------------------------------------------------------------------------------------------------
730 TCanvas* RootTools::drawArrayOfHistosPrettily(TH1D* hs[], Int_t numHists, TCanvas* can, Double_t* colWeights){
731 
732  if(can==NULL){
733  can = new TCanvas();
734  }
735  can->cd();
736 
737  Double_t minCol = 0;
738  Double_t maxCol = 255;
739  if(colWeights!=NULL){
740  maxCol = TMath::MaxElement(numHists, colWeights);
741  minCol = TMath::MinElement(numHists, colWeights);
742  }
743 
744  Int_t numDrawn = 0;
745  Int_t first = 0;
746  Double_t max = -1;
747  for(Int_t histInd=0; histInd < numHists; histInd++){
748  if(hs[histInd] != NULL){
749  if(numDrawn==0){
750  first = histInd;
751  }
752  TString opt = numDrawn == 0 ? "" : "same";
753  if(colWeights!=NULL){
754  hs[histInd]->SetLineColor(gStyle->GetColorPalette(Int_t(250.*(colWeights[histInd] - minCol)/(maxCol-minCol))));
755  // std::cout << colWeights[histInd] << "\t" << maxCol << "\t" << minCol << "\t"
756  // << ((colWeights[histInd] - minCol)/(maxCol-minCol)) << "\t"
757  // << Int_t(250.*(colWeights[histInd] - minCol)/(maxCol-minCol))
758  // << std::endl;
759  }
760  else{
761  hs[histInd]->SetLineColor(gStyle->GetColorPalette(histInd*Int_t(255./numHists)));
762  }
763  hs[histInd]->Draw(opt);
764  if(hs[histInd]->GetMaximum() > max){
765  max = hs[histInd]->GetMaximum()*1.1;
766  hs[first]->SetMaximum(max);
767  }
768  numDrawn++;
769  }
770  }
771  return can;
772 }
773 
774 
775 
776 
777 //---------------------------------------------------------------------------------------------------------
787 void RootTools::offsetTGraphXAxes(Int_t numGrs, TGraph* grs[], Double_t offsets[]){
788  for(Int_t grInd=0; grInd < numGrs; grInd++){
789  for(Int_t samp=0; samp < grs[grInd]->GetN(); samp++){
790  grs[grInd]->GetX()[samp] += offsets[grInd];
791  }
792  }
793 }
794 
795 
796 
797 
798 
799 //---------------------------------------------------------------------------------------------------------
809 void RootTools::multiplyTGraphYAxes(Int_t numGrs, TGraph* grs[], Double_t factors[]){
810  for(Int_t grInd=0; grInd < numGrs; grInd++){
811  for(Int_t samp=0; samp < grs[grInd]->GetN(); samp++){
812  grs[grInd]->GetY()[samp] *= factors[grInd];
813  }
814  }
815 }
816 
817 
818 
819 
820 
821 //---------------------------------------------------------------------------------------------------------
832 TCanvas* RootTools::drawArrayOfTGraphsPrettily(TGraph* grs[], Int_t numGrs,
833  TString drawOpt, TCanvas* can, Double_t* colWeights){
834 
835  if(can==NULL){
836  can = new TCanvas();
837  }
838  can->cd();
839 
840  Double_t colFactor = 1;
841  if(colWeights!=NULL){
842  Double_t maxCol = TMath::MaxElement(numGrs, colWeights);
843  Double_t minCol = TMath::MinElement(numGrs, colWeights);
844  colFactor = 254./(maxCol-minCol);
845  }
846 
847  Double_t max = -DBL_MAX;
848  Double_t min = DBL_MAX;
849  Int_t numDrawn = 0;
850  Int_t first = 0;
851  for(Int_t grInd=0; grInd < numGrs; grInd++){
852  if(grs[grInd]!=NULL){
853  if(grs[grInd]->GetN() > 0){
854  TString opt = numDrawn == 0 ? "a" + drawOpt : drawOpt + "same";
855  if(numDrawn==0){
856  first = grInd;
857  }
858 
859  if(colWeights!=NULL){
860  grs[grInd]->SetLineColor(gStyle->GetColorPalette(Color_t(colWeights[grInd]*colFactor)));
861  grs[grInd]->SetMarkerColor(gStyle->GetColorPalette(Color_t(colWeights[grInd]*colFactor)));
862  grs[grInd]->SetMarkerColor(gStyle->GetColorPalette(Color_t(colWeights[grInd]*colFactor)));
863  }
864  else{
865  grs[grInd]->SetLineColor(gStyle->GetColorPalette(grInd*Int_t(254./(numGrs-1))));
866  grs[grInd]->SetMarkerColor(gStyle->GetColorPalette(grInd*Int_t(254./(numGrs-1))));
867  grs[grInd]->SetMarkerColor(gStyle->GetColorPalette(grInd*Int_t(254./(numGrs-1))));
868  }
869 
870  grs[grInd]->Draw(opt);
871 
872  Double_t thisMax = TMath::MaxElement(grs[grInd]->GetN(), grs[grInd]->GetY());
873  if(thisMax > max){
874  Double_t fact = thisMax > 0 ? 1.1 : 0.9;
875  max = thisMax*fact;
876  grs[first]->SetMaximum(max);
877  }
878 
879  Double_t thisMin = TMath::MinElement(grs[grInd]->GetN(), grs[grInd]->GetY());
880  if(thisMin < min){
881  Double_t fact = thisMin < 0 ? 1.1 : 0.9;
882  min = thisMin*fact;
883  grs[first]->SetMinimum(min);
884  }
885  numDrawn++;
886  }
887  }
888  }
889 
890  return can;
891 
892 }
893 
894 
895 
896 
897 
898 //---------------------------------------------------------------------------------------------------------
913 TLegend* RootTools::makeLegend(TGraph* grs[], Int_t numGrs,
914  TString titles[], TString opt,
915  Double_t minX, Double_t minY,
916  Double_t maxX, Double_t maxY){
917 
918  TLegend* l = new TLegend(minX, minY, maxX, maxY);
919  for(Int_t grInd=0; grInd < numGrs; grInd++){
920  if(grs[grInd]!=NULL){
921  l->AddEntry(grs[grInd], titles[grInd], opt);
922  }
923  }
924  return l;
925 }
926 
927 
928 
929 
930 
931 //---------------------------------------------------------------------------------------------------------
945 TLegend* RootTools::makeLegend(TH1D* hs[], Int_t numHists,
946  TString titles[], TString opt,
947  Double_t minX, Double_t minY,
948  Double_t maxX, Double_t maxY){
949 
950  TLegend* l = new TLegend(minX, minY, maxX, maxY);
951  for(Int_t histInd=0; histInd < numHists; histInd++){
952  if(hs[histInd]!=NULL){
953  l->AddEntry(hs[histInd], titles[histInd], opt);
954  }
955  }
956  return l;
957 }
958 
959 
960 
961 
962 
963 //---------------------------------------------------------------------------------------------------------
970 void RootTools::writeTGraph(TGraph* gr, TString name){
971  // Taking laziness to a new level... replace two lines with a one line function.
972  gr->SetName(name);
973  gr->Write();
974 }
975 
976 
977 
978 
979 
980 //---------------------------------------------------------------------------------------------------------
992  Double_t& maxY, Double_t& maxX,
993  Double_t& minY, Double_t& minX){
994  getLocalMaxToMinWithinLimits(gr, maxY, maxX, minY, minX, gr->GetX()[0], gr->GetX()[gr->GetN()-1]);
995 
996 }
997 
998 
999 
1000 
1001 
1002 //---------------------------------------------------------------------------------------------------------
1016  Double_t& maxY, Double_t& maxX,
1017  Double_t& minY, Double_t& minX,
1018  Double_t lowerLimit, Double_t upperLimit){
1019 
1020 
1021  // assumes unique value at local minima and maxima, e.g. 0.1, 0.2, 0.1 for maxima.
1022  // if it goes 0.1, 0.2, 0.2, 0.1 then this will need to get more complicated
1023  std::vector<Int_t> extremaSamps;
1024 
1025  for(Int_t samp=1; samp < gr->GetN()-1; samp++){
1026  if(gr->GetX()[samp] >= lowerLimit && gr->GetX()[samp] <=upperLimit){
1027  Double_t y0 = gr->GetY()[samp-1];
1028  Double_t y1 = gr->GetY()[samp];
1029  Double_t y2 = gr->GetY()[samp+1];
1030 
1031 
1032  if(y0 > y1 && y1 < y2){
1033  // Is a local minimum
1034  extremaSamps.push_back(samp);
1035  }
1036  else if(y0 < y1 && y1 > y2){
1037  // Is a local maximum
1038  extremaSamps.push_back(samp);
1039  }
1040  }
1041  }
1042 
1043  Double_t maxOffset = -DBL_MAX;
1044  Int_t maxSampInd = -1;
1045  for(UInt_t sampInd = 0; sampInd < extremaSamps.size()-1; sampInd++){
1046  Double_t offset = TMath::Abs(gr->GetY()[extremaSamps.at(sampInd)] - gr->GetY()[extremaSamps.at(sampInd+1)]);
1047  if(offset > maxOffset){
1048  maxOffset = offset;
1049  maxSampInd = sampInd;
1050  }
1051  }
1052 
1053  if(gr->GetY()[extremaSamps.at(maxSampInd)] > gr->GetY()[extremaSamps.at(maxSampInd+1)]){
1054  maxY = gr->GetY()[extremaSamps.at(maxSampInd)];
1055  maxX = gr->GetX()[extremaSamps.at(maxSampInd)];
1056 
1057  minY = gr->GetY()[extremaSamps.at(maxSampInd+1)];
1058  minX = gr->GetX()[extremaSamps.at(maxSampInd+1)];
1059  }
1060  else{
1061  maxY = gr->GetY()[extremaSamps.at(maxSampInd+1)];
1062  maxX = gr->GetX()[extremaSamps.at(maxSampInd+1)];
1063 
1064  minY = gr->GetY()[extremaSamps.at(maxSampInd)];
1065  minX = gr->GetX()[extremaSamps.at(maxSampInd)];
1066  }
1067 }
1068 
1069 
1070 
1071 //---------------------------------------------------------------------------------------------------------
1078 void RootTools::saveCanvas(TCanvas* c, TString fileName){
1079 
1080  std::cout << "Saving this canvas as an .eps, .png, and .C file..." << std::endl;
1081  TString fName = fileName + ".eps";
1082  c->SaveAs(fName);
1083  fName = fileName + ".png";
1084  c->SaveAs(fName);
1085  fName = fileName + ".C";
1086  c->SaveAs(fName);
1087  std::cout << "...Complete!" << std::endl;
1088  c->Update();
1089 }
1090 
1091 
1092 
1093 
1094 
1095 //---------------------------------------------------------------------------------------------------------
1103 
1104  Int_t maxBin = RootTools::getPeakBinOfHistogram(h);
1105  Int_t n = h->GetNbinsX();
1106  Double_t max = h->GetBinContent(maxBin);
1107 
1108 
1109  Double_t halfMaxPos = 0;
1110  for(Int_t bin=maxBin; bin<=n; bin++){
1111  Double_t val = h->GetBinContent(bin);
1112  if(val < max/2){
1113  halfMaxPos = h->GetBinLowEdge(bin);
1114  break;
1115  }
1116  }
1117 
1118  Double_t halfMaxNeg = 0;
1119  for(Int_t bin=maxBin; bin>=1; bin--){
1120  Double_t val = h->GetBinContent(bin);
1121  if(val < max/2){
1122  halfMaxNeg = h->GetBinLowEdge(bin);
1123  break;
1124  }
1125  }
1126 
1127  return halfMaxPos - halfMaxNeg;
1128 }
1129 
1130 
1131 
1132 
1133 
1134 //---------------------------------------------------------------------------------------------------------
1142 
1143  Int_t n = h->GetNbinsX();
1144  Double_t max = -DBL_MAX;
1145  Int_t maxBin = 0;
1146  for(Int_t bin=1; bin<n; bin++){
1147  Double_t val = h->GetBinContent(bin);
1148  if(val > max){
1149  max = val;
1150  maxBin = bin;
1151  }
1152  }
1153  return maxBin;
1154 }
1155 
1156 
1157 
1158 
1159 
1160 //---------------------------------------------------------------------------------------------------------
1169 Double_t RootTools::getPeakBinOfHistogram(TH2D* hist, Int_t& binx, Int_t& biny){
1170 
1171  Int_t nx = hist->GetNbinsX();
1172  Int_t ny = hist->GetNbinsY();
1173  Double_t histPeak = -DBL_MAX;
1174  for(Int_t by = 1; by<=ny; by++){
1175  for(Int_t bx = 1; bx<=nx; bx++){
1176  Double_t val = hist->GetBinContent(bx, by);
1177  if(val > histPeak){
1178  histPeak = val;
1179  binx = bx;
1180  biny = by;
1181  }
1182  }
1183  }
1184 
1185  return histPeak;
1186 
1187 }
1188 
1189 
1190 
1191 
1192 
1193 //---------------------------------------------------------------------------------------------------------
1201 
1202  Int_t peakBin = getPeakBinOfHistogram(h);
1203  Double_t lowBinEdgeOfPeak = h->GetXaxis()->GetBinLowEdge(peakBin);
1204  return lowBinEdgeOfPeak;
1205 }
1206 
1207 
1208 
1209 
1210 
1211 
1212 //---------------------------------------------------------------------------------------------------------
1219 
1220  Double_t min = h->GetMinimum();
1221  Double_t max = h->GetMaximum();
1222 
1223  if(TMath::Abs(min) > max){
1224  h->SetMaximum(-min);
1225  }
1226  else{
1227  h->SetMinimum(-max);
1228  }
1229 }
1230 
1231 
1232 
1233 
1234 
1235 //---------------------------------------------------------------------------------------------------------
1242 Int_t RootTools::getBit(UInt_t bitIndex, UInt_t bitMask){
1243  return ((bitMask >> bitIndex) & 1);
1244 }
1245 
1246 
1247 
1248 
1249 
1250 //---------------------------------------------------------------------------------------------------------
1257 Int_t RootTools::getNumBitsSet(Int_t numBitsToCheck, UInt_t bitMask){
1258  Int_t numBitsHigh = 0;
1259  for(Int_t bitInd=0; bitInd < numBitsToCheck; bitInd++){
1260  if(getBit(bitInd, bitMask) > 0){
1261  numBitsHigh++;
1262  }
1263  }
1264  return numBitsHigh;
1265 }
1266 
1267 
1268 
1269 
1270 
1271 //---------------------------------------------------------------------------------------------------------
1278  const int NRGBs = 3, NCont = 999;
1279  gStyle->SetNumberContours(NCont);
1280  Double_t stops[NRGBs] = { 0.00, 0.50, 1.00};
1281  Double_t red[NRGBs] = { 0.00, 1.00, 1.00};
1282  Double_t green[NRGBs] = { 0.00, 1.00, 0.00};
1283  Double_t blue[NRGBs] = { 1.00, 1.00, 0.00};
1284  TColor color;
1285  color.InitializeColors();
1286  color.CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
1287 }
1288 
1289 
1290 
1291 
1292 
1293 
1294 //---------------------------------------------------------------------------------------------------------
1301 void RootTools::draw2D(TH2D* hist, TString opt){
1302  hist->Draw(opt);
1303  Double_t max = TMath::Abs(hist->GetMaximum());
1304  Double_t min = TMath::Abs(hist->GetMinimum());
1305  Double_t limit = min > max ? min : max;
1306  hist->SetMaximum(limit);
1307  hist->SetMinimum(-limit);
1308 }
1309 
1310 
1311 
1312 
1313 
1314 //---------------------------------------------------------------------------------------------------------
1322 TChain* RootTools::getHeadChain(Int_t firstRun, Int_t lastRun, RawAnitaHeader*& headPtr){
1323  TChain* c = new TChain("headTree");
1324  for(Int_t run=firstRun; run <= lastRun; run++){
1325  c->Add(TString::Format("~/UCL/ANITA/flight1415/root/run%d/headFile%d.root", run, run));
1326  }
1327  c->SetBranchAddress("header", &headPtr);
1328  return c;
1329 }
1330 
1331 
1332 
1333 
1334 
1335 //---------------------------------------------------------------------------------------------------------
1343 TChain* RootTools::getAdu5PatChain(Int_t firstRun, Int_t lastRun, Adu5Pat*& pat){
1344  TChain* c = new TChain("adu5PatTree");
1345  for(Int_t run=firstRun; run <= lastRun; run++){
1346  c->Add(TString::Format("~/UCL/ANITA/flight1415/root/run%d/gpsFile%d.root", run, run));
1347  }
1348  c->SetBranchAddress("pat", &pat);
1349  return c;
1350 }
1351 
1352 
1353 
1354 
1355 
1356 
1357 //---------------------------------------------------------------------------------------------------------
1365 TString RootTools::getAntName(AnitaPol::AnitaPol_t pol, Int_t antInd){
1366  // Assumes antInd starts at 0. Returns the name w/ +1
1367  Int_t phi = (antInd % NUM_PHI) + 1;
1368 
1369  Int_t ring = antInd / NUM_PHI;
1370 
1371  TString antName = TString::Format("%d", phi);
1372 
1373  antName += AnitaRing::ringAsChar(AnitaRing::AnitaRing_t(ring));
1374  antName += AnitaPol::polAsChar(pol);
1375 
1376  return antName;
1377 }
1378 
1379 
1380 
1381 
1382 
1383 
1384 //---------------------------------------------------------------------------------------------------------
1391 Int_t RootTools::getColorFracThroughPalette(Int_t index, Int_t maxVal){
1392  return gStyle->GetColorPalette(index*Int_t(255./maxVal));
1393 }
1394 
1395 
1396 
1397 
1398 
1399 
1400 //---------------------------------------------------------------------------------------------------------
1409 TCanvas* RootTools::drawHistsWithStatsBoxes(Int_t numHists, TH1D* hs[], TString drawOpt, TString statsOption){
1410 
1411  gStyle->SetOptStat(statsOption);
1412  TCanvas* theCan = new TCanvas();
1413 
1414  Double_t boxSize = 0.2; // In normalized coordinates
1415  const Int_t maxNumBoxes = 1./boxSize;
1416 
1417  Int_t divisor = (numHists/maxNumBoxes) + 1;
1418 
1419  // This is dumb and will get messy If I have more than 2*maxnumBoxes
1420  boxSize/=divisor;
1421 
1422  for(Int_t histInd=0; histInd < numHists; histInd++){
1423 
1424  TString thisDrawOpt = histInd == 0 ? drawOpt : drawOpt+"sames";
1425  hs[histInd]->Draw(thisDrawOpt);
1426 
1427  theCan->Update();
1428  TPaveStats* sbox = (TPaveStats*) hs[histInd]->FindObject("stats");
1429  sbox->SetTextColor(hs[histInd]->GetLineColor());
1430  sbox->SetX1NDC(0.8);
1431  sbox->SetY1NDC(1 - (histInd+1)*boxSize);
1432  sbox->SetX2NDC(0.98);
1433  sbox->SetY2NDC(0.98 - histInd*boxSize);
1434  }
1435 
1436  theCan->Update();
1437  return theCan;
1438 }
void zeroPadTGraph(TGraph *gr, Int_t newLen, Double_t dt=0)
Add a bunch of zeros to the end of a TGraph.
Definition: RootTools.cxx:576
void saveCanvas(TCanvas *c1, TString fileName)
My function to save a TCanvas for talks, and save an editable version.
Definition: RootTools.cxx:1078
TChain * getAdu5PatChain(Int_t firstRun, Int_t lastRun, Adu5Pat *&pat)
Get a TChain of the ANITA-3 adu5Pat data.
Definition: RootTools.cxx:1343
void printYVals(TGraph *gr, TString delimiter=", ", TString start="{", TString end="}\n")
Print all the the y-axis values of a TGraph to the screen for debugging.
Definition: RootTools.cxx:59
Int_t getIndexOfMaximum(Int_t len, Double_t *arr)
Find indices where input is not a number (for debugging).
Definition: RootTools.cxx:404
void multiplyTGraphYAxes(Int_t numGrs, TGraph *grs[], Double_t factors[])
Multiples a set of factors to the y-axis values of each respective TGraph.
Definition: RootTools.cxx:809
void subtractOffset(TGraph *gr, Double_t offset)
Subtract a constant value from the y-axis values at each point.
Definition: RootTools.cxx:247
void getMaxMin(TGraph *gr, Double_t &max, Double_t &min)
Scan long TGraph and update the references to the maximum and minimum input values.
Definition: RootTools.cxx:146
void printTGraphInfo(TGraph *gr)
Print summary information about gr: name, title, number of points, all x and y values.
Definition: RootTools.cxx:452
Double_t getSumOfYVals(TGraph *gr)
Adds up all y-axis values of input TGraph.
Definition: RootTools.cxx:14
Double_t getFullWidthHalfMax(TH1D *h)
Assumes a nice distribution with a single peak, finds the full width half max.
Definition: RootTools.cxx:1102
TGraph * makeUnwrappedCorrelationGraph(TGraph *gr)
Unwraps a circularly correlated graph so the -ve time lies behind the +ve time.
Definition: RootTools.cxx:115
TCanvas * drawArrayOfTGraphsPrettily(TGraph *grs[], Int_t numGrs, TString drawOpt="l", TCanvas *can=NULL, Double_t *colWeights=NULL)
Draws an array of TGraphs with a rainbow on a single TCanvas.
Definition: RootTools.cxx:832
std::vector< Int_t > getIndicesOfNans(TGraph *gr)
Find indices where input is not a number (for debugging)
Definition: RootTools.cxx:432
TCanvas * drawArrayOfHistosPrettily(TH1D *hs[], Int_t numHists, TCanvas *can=NULL, Double_t *colWeights=NULL)
Draws an array of histograms with a rainbow on a single TCanvas.
Definition: RootTools.cxx:730
Int_t getNumBitsSet(Int_t numBitsToCheck, UInt_t bitMask)
For counting how many bits are set to one in a bitmask.
Definition: RootTools.cxx:1257
void printArray(int n, double *array, TString delimiter=", ", TString start="{", TString end="}\n")
Print all the elements of a c-styles array to the screen for debugging.
Definition: RootTools.cxx:36
Int_t getPeakBinOfHistogram(TH1D *h)
Finds the bin containing the maximum value of a TH1D.
Definition: RootTools.cxx:1141
void printXVals(TGraph *gr, TString delimiter=", ", TString start="{", TString end="}\n")
Print all the the x-axis values of a TGraph to the screen for debugging.
Definition: RootTools.cxx:77
void getMaxMinWithinLimits(TGraph *gr, Double_t &maxY, Double_t &maxX, Double_t &minY, Double_t &minX, Double_t lowerLimit, Double_t upperLimit)
Scan long TGraph and update the references to the maximum and minimum input values.
Definition: RootTools.cxx:201
Int_t getColorFracThroughPalette(Int_t index, Int_t maxVal)
Gets the color a fraction through the current palette.
Definition: RootTools.cxx:1391
void draw2D(TH2D *hist, TString opt)
Draw 2D histogram and set bin limits to be symmetrical about zero.
Definition: RootTools.cxx:1301
TGraph * makeDerivativeTGraph(TGraph *gr)
Makes a derivative TGraph, contains gr->GetN() - 1 points.
Definition: RootTools.cxx:92
TGraph * makeLinearlyInterpolatedGraph(TGraph *grIn, Double_t dt)
Make interpolated TGraph using linear interpolation between points.
Definition: RootTools.cxx:600
TChain * getHeadChain(Int_t firstRun, Int_t lastRun, RawAnitaHeader *&headPtr)
Get a TChain of the ANITA-3 header data.
Definition: RootTools.cxx:1322
Int_t getBit(UInt_t bitIndex, UInt_t bitMask)
For decoding bit masks.
Definition: RootTools.cxx:1242
TCanvas * drawHistsWithStatsBoxes(Int_t numHists, TH1D *hs[], TString drawOpt, TString statsOption)
Draw histograms on the same (new) canvas with nice stats boxes.
Definition: RootTools.cxx:1409
TGraph * makeNormalized(TGraph *gr)
Creates new TGraph (leaving original unchanged) with mean = 0 & RMS = 1.
Definition: RootTools.cxx:343
void setWhiteZeroColorScale()
Set color scale where white is in the middle.
Definition: RootTools.cxx:1277
void writeTGraph(TGraph *gr, TString name)
Sets the name of a TGraph to name and then writes it to the current file.
Definition: RootTools.cxx:970
void makeZaxisScaleEqualAboutZero(TH2D *h)
For nice plotting of 2D dists about 0, makes max = -min.
Definition: RootTools.cxx:1218
void getLocalMaxToMin(TGraph *gr, Double_t &maxY, Double_t &maxX, Double_t &minY, Double_t &minX)
Updates input based on absolute largest local maximum to local minimum difference. For pulse finding.
Definition: RootTools.cxx:991
Double_t getDeltaAngleDeg(Double_t angle1, Double_t angle2)
Do angle1-angle2 (in degrees) and +- 360 such that the result lies in -180 < deltaAngle < 180...
Definition: RootTools.cxx:267
void normalize(TGraph *gr, Double_t &mean, Double_t &rms)
Modify gr to set mean=0 and rms=1.
Definition: RootTools.cxx:323
TGraph * makeSortedTGraph(TTree *tree, TString drawText, TString cutString, Double_t wrapValue)
Plot a time (or any TTree branch) ordered TGraph of some TTree branch.
Definition: RootTools.cxx:490
TString getAntName(AnitaPol::AnitaPol_t pol, Int_t antInd)
Use polarization and index to get the antenna name (1st phi-sector called 1, not 0).
Definition: RootTools.cxx:1365
void getMeanAndRms(TGraph *gr, Double_t &mean, Double_t &rms)
Get mean and rms of gr.
Definition: RootTools.cxx:381
void offsetTGraphXAxes(Int_t numGrs, TGraph *grs[], Double_t offsets[])
Adds a set of offsets to the x-axis values of each respective TGraph.
Definition: RootTools.cxx:787
Double_t getLowBinEdgeOfHistogramPeak(TH1D *h)
Finds the bin containing the maximum value of a TH1D.
Definition: RootTools.cxx:1200
TH1D * plotsZaxisDist(TH2 *h2, TString hName, Int_t nBins, Double_t xMin, Double_t xMax)
Put Z-axis values of a TH2D histogram into a TH1D histogram.
Definition: RootTools.cxx:553
void getLocalMaxToMinWithinLimits(TGraph *gr, Double_t &maxY, Double_t &maxX, Double_t &minY, Double_t &minX, Double_t lowerLimit, Double_t upperLimit)
Updates input based on absolute largest local maximum to local minimum difference. For pulse finding.
Definition: RootTools.cxx:1015
TGraph * interpolateWithStartTime(TGraph *grIn, Double_t startTime, Double_t dt, Int_t nSamp)
Wrapper function for ROOT&#39;s interpolator, can zero pad the front to start from a particular time...
Definition: RootTools.cxx:679
TLegend * makeLegend(TGraph *grs[], Int_t numGrs, TString titles[], TString opt="l", Double_t minX=0.8, Double_t minY=0.8, Double_t maxX=1, Double_t maxY=1)
Generates a TLegend from input arrays of TGraphs and titles (TStrings)
Definition: RootTools.cxx:913