FancyTTreeInterpolator.cxx
1 #include "FancyTTreeInterpolator.h"
2 
3 
4 
5 
6 
7 //---------------------------------------------------------------------------------------------------------
18  /* Do use this constructor */
19  fXAxisText = xAxisText;
20  fTree = t;
21  TGraph* gr = makeSortedTGraph(xAxisText + ":" + xAxisText);
22  // TGraph* gr = makeSortedTGraph(xAxisText + ":" + xAxisText);
23  fXmin = gr->GetX()[0];
24  fXmax = gr->GetX()[gr->GetN()-1];
25 
26  // TFile* fout = new TFile("ftti.root", "recreate");
27  // gr->Write();
28  // fout->Close();
29 
30  delete gr;
31 
32  /* For error reporting, want precision of unsigned int ~10 digits*/
33  std::cerr.precision(10);
34 }
35 
36 
37 
38 
39 
40 //---------------------------------------------------------------------------------------------------------
47  std::map<TString, TGraph*> ::iterator i;
48  for(i=fStringToGraph.begin(); i!=fStringToGraph.end(); ++i){
49  delete (*i).second;
50  (*i).second = NULL;
51  }
52 }
53 
54 
55 
56 
57 //---------------------------------------------------------------------------------------------------------
65  return makeSortedTGraph(drawText, "", 0);
66 }
67 
68 
69 
70 
71 //---------------------------------------------------------------------------------------------------------
79 TGraph* Acclaim::FancyTTreeInterpolator::makeSortedTGraph(TString drawText, TString cutString){
80  return makeSortedTGraph(drawText, cutString, 0);
81 }
82 
83 
84 
85 
86 
87 //---------------------------------------------------------------------------------------------------------
98 TGraph* Acclaim::FancyTTreeInterpolator::makeSortedTGraph(TString drawText, Double_t wrapValue){
99  return makeSortedTGraph(drawText, "", wrapValue);
100 }
101 
102 
103 
104 
105 
106 //---------------------------------------------------------------------------------------------------------
118 TGraph* Acclaim::FancyTTreeInterpolator::makeSortedTGraph(TString drawText, TString cutString, Double_t wrapValue){
119  /*
120  This function:
121  Uses TTree::Draw to select the data.
122  Sorts it using TMath::Sort.
123  Returns the graph.
124  Note: This does not add it to the std::map of graphs that this class uses for storage.
125  To create a graph and add it to the std::map use Acclaim::FancyTTreeInterpolator::add.
126  */
127 
128  // Draw
129  const Int_t nEntries = fTree->Draw(drawText, cutString, "goff"); // "goff" means graphics off
130 
131  // Sort
132  std::vector<Int_t> sortedIndices(nEntries);
133  TMath::Sort(nEntries, fTree->GetV2(), &sortedIndices.front(), kFALSE);
134  std::vector<Double_t> newX(nEntries);
135  std::vector<Double_t> newY(nEntries);
136 
137  for(int i=0; i<nEntries; i++){
138  newX.at(i) = fTree->GetV2()[sortedIndices.at(i)];
139  newY.at(i) = fTree->GetV1()[sortedIndices.at(i)];
140  }
141 
142  // Unwrap data here so interpolation works smoothly
143  // will unwrap when getting entries from graph
144  if(wrapValue != 0){
145  for(int i=1; i<nEntries; i++){
146  // If y[i] >> y[i-1] => then we went below zero to the wrap value
147  // can only modify y[i], so we should subtract wrapValue enough times
148  // that the graph becomes smooth
149  while (newY.at(i) - newY.at(i-1) > wrapValue/2){
150  newY.at(i) -= wrapValue;
151  }
152 
153  // If y[i] << y[i-1] => then we went add zero to the wrap value
154  // can only modify y[i], so we should add wrapValue enough times
155  // that the graph becomes smooth
156  while (newY.at(i) - newY.at(i-1) < -wrapValue/2){
157  newY.at(i) += wrapValue;
158  }
159 
160  }
161  }
162 
163  // sorted TGraph
164  TGraph* gr(new TGraph(nEntries,&newX.front(), &newY.front()));
165  gr->SetTitle(drawText + ", " + cutString);
166  gr->GetXaxis()->SetTitle(fXAxisText);
167  gr->GetYaxis()->SetTitle(drawText);
168 
169  return gr;
170 
171 }
172 
173 
174 
175 
176 
177 //---------------------------------------------------------------------------------------------------------
183 void Acclaim::FancyTTreeInterpolator::add(TString yAxisText){
184  add(yAxisText, "", 0);
185 }
186 
187 
188 
189 
190 //---------------------------------------------------------------------------------------------------------
197 void Acclaim::FancyTTreeInterpolator::add(TString yAxisText, TString cutString){
198  add(yAxisText, cutString, 0);
199 }
200 
201 
202 
203 
204 
205 //---------------------------------------------------------------------------------------------------------
212 void Acclaim::FancyTTreeInterpolator::add(TString yAxisText, Double_t wrapValue){
213  add(yAxisText, "", wrapValue);
214 }
215 
216 
217 
218 
219 
220 //---------------------------------------------------------------------------------------------------------
228 void Acclaim::FancyTTreeInterpolator::add(TString yAxisText, TString cutString, Double_t wrapValue){
229  TString drawText = yAxisText + ":" + fXAxisText;
230  TGraph* gr = makeSortedTGraph(drawText, cutString, wrapValue);
231  fStringToGraph[yAxisText] = gr;
232  fStringToWrapValue[yAxisText] = wrapValue;
233 }
234 
235 
236 
237 
238 
239 //---------------------------------------------------------------------------------------------------------
246 TGraph* Acclaim::FancyTTreeInterpolator::get(TString yAxisText){
247  /* Use this to access a graph you've made with the add function */
248 
249  if(fStringToGraph.count(yAxisText)==0){
250  std::cerr << "Can't find TGraph in FancyTTreeInterpolator created with text " + yAxisText << std::endl;
251 
252  // Hack for c++98... in 2014
253  throw std::invalid_argument("Can't find TGraph in FancyTTreeInterpolator created with text " + std::string(yAxisText.Data()));
254  }
255  else{
256  return fStringToGraph.find(yAxisText)->second;
257  }
258  return NULL;
259 }
260 
261 
262 
263 
264 
265 //---------------------------------------------------------------------------------------------------------
273 Double_t Acclaim::FancyTTreeInterpolator::interp(TString yAxisText, Double_t xAxisValue){
274  /* This function handles the getting of values from the appropriate TGraph */
275 
276  TGraph* gr = get(yAxisText);
277 
278  if(xAxisValue >= fXmin && xAxisValue <= fXmax){
279  Double_t tempVal = gr->Eval(xAxisValue);
280 
281  // do the unwrapping if required
282  Double_t wrap = fStringToWrapValue.find(yAxisText)->second;
283  if(wrap != 0){
284  while (tempVal < 0){
285  tempVal += wrap;
286  }
287  while (tempVal >= wrap){
288  tempVal -= wrap;
289  }
290  }
291  return tempVal;
292  }
293  else{
294  std::cerr << "Value " << xAxisValue << " lies outside range of " << fXAxisText << std::endl;
295  std::cerr << "xMin = " << fXmin << ", xMax = " << fXmax << std::endl;
296  throw std::domain_error("");
297  }
298  return 0;
299 
300 }
TString fXAxisText
Branch name with which the intepolater was initialized.
TGraph * makeSortedTGraph(TString yAxisText)
Makes a sorted TGraph from fTree->Draw() with no cuts.
Double_t fXmax
Stored x-axis lower limit.
TTree * fTree
TTree with which the intepolater was initialized.
void add(TString yAxisText)
Adds a TGraph to the interally stored TGraphs.
Double_t interp(TString yAxisText, Double_t xAxisValue)
Get interpolated yAxisText variable at xAxisValue (time).
Double_t fXmin
Stored x-axis lower limit.
FancyTTreeInterpolator(TTree *t, TString xAxisText)
Constructor.
TGraph * get(TString yAxisText)
Access the interally stored TGraph via the yAxisText.