RootTools.h
1 /* -*- C++ -*-.*/
2 /**************************************************************************************************
3  Author: Ben Strutt
4  Email: b.strutt.12@ucl.ac.uk
5 
6  Description:
7  Slightly complicated functions to manipulate ROOT objects should go in here.
8  I want to only ever write them once, so when I need to do something I'll add it here.
9 *************************************************************************************************************** */
10 
11 #ifndef ROOTTOOLS_H
12 #define ROOTTOOLS_H
13 
14 #include "TObjArray.h"
15 #include "TGraph.h"
16 #include "TPaveStats.h"
17 #include "TTree.h"
18 #include "TAxis.h"
19 #include "TMath.h"
20 #include "iostream"
21 #include "TH2.h"
22 #include "TH1D.h"
23 #include "TStyle.h"
24 #include "TCanvas.h"
25 #include "TLegend.h"
26 #include "cfloat"
27 #include "TColor.h"
28 #include "TChain.h"
29 #include "Math/Interpolator.h"
30 #include "Math/InterpolationTypes.h"
31 
32 #include "RawAnitaHeader.h"
33 #include "Adu5Pat.h"
34 
45 namespace RootTools{
46 
47  void writeTGraph(TGraph* gr, TString name);
48  void printArray(int n, double* array, TString delimiter = ", ", TString start = "{" ,TString end = "}\n");
49  void printYVals(TGraph* gr, TString delimiter = ", ", TString start = "{" ,TString end = "}\n");
50  void printXVals(TGraph* gr, TString delimiter = ", ", TString start = "{" ,TString end = "}\n");
51  void printTGraphInfo(TGraph* gr);
52 
53 
54  /* Get info about input */
55  Int_t getIndexOfMaximum(Int_t len, Double_t* arr);
56  std::vector<Int_t> getIndicesOfNans(TGraph* gr);
57  Double_t getSumOfYVals(TGraph* gr);
58  void getMaxMin(TGraph* gr, Double_t& max, Double_t& min);
59  void getMeanAndRms(TGraph* gr, Double_t& mean, Double_t& rms);
60  void getMaxMin(TGraph* gr, Double_t& maxY, Double_t& maxX, Double_t& minY, Double_t& minX);
61  void getMaxMinWithinLimits(TGraph* gr, Double_t& maxY, Double_t& maxX,
62  Double_t& minY, Double_t& minX,
63  Double_t lowerLimit, Double_t upperLimit);
64  void getLocalMaxToMin(TGraph* gr,
65  Double_t& maxY, Double_t& maxX,
66  Double_t& minY, Double_t& minX);
67  void getLocalMaxToMinWithinLimits(TGraph* gr,
68  Double_t& maxY, Double_t& maxX,
69  Double_t& minY, Double_t& minX,
70  Double_t lowerLimit, Double_t upperLimit);
71 
72  Int_t getPeakBinOfHistogram(TH1D* h);
73  Double_t getPeakBinOfHistogram(TH2D* hist, Int_t& binx, Int_t& biny);
74  Double_t getLowBinEdgeOfHistogramPeak(TH1D* h);
75  Double_t getFullWidthHalfMax(TH1D* h);
76  Int_t getBit(UInt_t bitIndex, UInt_t bitMask);
77  Int_t getNumBitsSet(Int_t numBitsToCheck, UInt_t bitMask);
78 
79  /* Do geometric things */
80  Double_t getDeltaAngleDeg(Double_t angle1, Double_t angle2);
81 
82 
83  /* Modify input */
84  void subtractOffset(TGraph* gr, Double_t offset);
85  void normalize(TGraph* gr, Double_t& mean, Double_t& rms);
86  void normalize(TGraph* gr);
87  void zeroPadTGraph(TGraph* gr, Int_t newLen, Double_t dt=0);
88  void offsetTGraphXAxes(Int_t numGrs, TGraph* grs[], Double_t offsets[]);
89  void multiplyTGraphYAxes(Int_t numGrs, TGraph* grs[], Double_t factors[]);
90 
91 
92  /* Prettify */
93  void makeZaxisScaleEqualAboutZero(TH2D* h);
94 
95  /* Make new output based on input */
96  TGraph* makeNormalized(TGraph* gr);
97  TGraph* makeNormalized(TGraph* gr, Double_t& mean, Double_t& rms);
98  TGraph* makeSortedTGraph(TTree* tree, TString drawText, TString cutString, Double_t wrapValue);
99  TGraph* makeLinearlyInterpolatedGraph(TGraph* grIn, Double_t dt);
100  TGraph* makeDerivativeTGraph(TGraph* gr);
101  TGraph* makeUnwrappedCorrelationGraph(TGraph* gr);
102  TGraph* interpolateWithStartTime(TGraph* grIn, Double_t startTime, Double_t dt, Int_t nSamp);
103 
104  TH1D* plotsZaxisDist(TH2* h2, TString hName, Int_t nBins, Double_t xMin, Double_t xMax);
105  TCanvas* drawArrayOfHistosPrettily(TH1D* hs[], Int_t numHists, TCanvas* can=NULL,
106  Double_t* colWeights = NULL);
107  TCanvas* drawArrayOfTGraphsPrettily(TGraph* grs[], Int_t numGrs,
108  TString drawOpt = "l", TCanvas* can=NULL,
109  Double_t* colWeights = NULL);
110  TLegend* makeLegend(TGraph* grs[], Int_t numGrs, TString titles[], TString opt = "l",
111  Double_t minX=0.8, Double_t minY=0.8,Double_t maxX=1, Double_t maxY=1);
112  TLegend* makeLegend(TH1D* hs[], Int_t numHists, TString titles[], TString opt = "l",
113  Double_t minX=0.8, Double_t minY=0.8,Double_t maxX=1, Double_t maxY=1);
114 
115 
116 
117  void saveCanvas(TCanvas* c1, TString fileName);
118  void setWhiteZeroColorScale();
119  void draw2D(TH2D* hist, TString opt);
120  Int_t getColorFracThroughPalette(Int_t index, Int_t maxVal);
121  TCanvas* drawHistsWithStatsBoxes(Int_t numHists, TH1D* hs[], TString drawOpt, TString statsOption);
122  TString getAntName(AnitaPol::AnitaPol_t pol, Int_t antInd);
123 
124 
125  /* Load ROOT data into chains quickly*/
126  TChain* getHeadChain(Int_t firstRun, Int_t lastRun, RawAnitaHeader*& headPtr);
127  TChain* getAdu5PatChain(Int_t firstRun, Int_t lastRun, Adu5Pat*& pat);
128 
129 }
130 
131 #endif
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
My commonly used, general functions to manipulate ROOT objects; so I only ever write them once...
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