AntarcticaMapPlotter.cxx
1 #include "AntarcticaMapPlotter.h"
2 
3 ClassImp(AntarcticaMapPlotter);
4 
5 //---------------------------------------------------------------------------------------------------------
10  initializeInternals();
11 }
12 
13 
14 
15 //---------------------------------------------------------------------------------------------------------
23 AntarcticaMapPlotter::AntarcticaMapPlotter(TString name, TString title, Int_t nBinsX, Int_t nBinsY){
24  initializeInternals();
25  fName = name;
26  fTitle = title;
27  addHistogram("h" + name, title, nBinsX, nBinsY);
28 }
29 
30 
31 
32 //---------------------------------------------------------------------------------------------------------
39  std::map<TString, TH2D*>::iterator histItr;
40  for(histItr=hists.begin(); histItr != hists.end(); ++histItr){
41  delete histItr->second;
42  }
43  std::map<TString, TGraph*>::iterator grItr;
44  for(grItr=grs.begin(); grItr != grs.end(); ++grItr){
45  delete grItr->second;
46  }
47 }
48 
49 
50 //---------------------------------------------------------------------------------------------------------
58 void AntarcticaMapPlotter::initializeInternals(){
59  /* The numbers in this function also come from Matt Mottram or Ryan */
60 
61  // This image taken from http://earthobservatory.nasa.gov/IOTD/view.php?id=6087
62  TString mapName = "antarcticaMosaic.png";
63  // TString mapName = "antarcticaIceMapBW.png";
64 
65 
66  // From Ryan
67  if(mapName=="antarcticaMosaic.png"){
68  TrueScaleLat=71;
69  RadiusOfEarth=6378.1e3; //Metres
70  xOffest=381.5; //369; //394; //400; //375;
71  yOffset=337; //312.5;
72  // scale=271.5/2.19496e+06;
73  scale=265/2.19496e+06;
74  xSize=725; //700; //750; //2701
75  ySize=625; //2333
76  }
77  else{ // Here I try and get the parameters for the pettier image... with moderate success.
78  TrueScaleLat=71;
79  RadiusOfEarth=6378.1e3; //Metres
80  xOffest=375;
81  yOffset=312.5;
82  // xOffest=0;
83  // yOffset=0;
84  scale=271.5/2.19496e+06;
85  xSize=750;
86  ySize=625;
87  }
88 
89  // Load the png image stuff.
90  const char* anitaUtilInstallDir = getenv("ANITA_UTIL_INSTALL_DIR");
91 
92  TString mapFileName = TString::Format("%s/share/anitaMap/%s", anitaUtilInstallDir, mapName.Data());
93  img = TImage::Open(mapFileName);
94  if(img){
95  img->SetConstRatio(kFALSE);
96  }
97  else{
98  std::cerr << "Warning in " << __FILE__ << "! Unable to open " << mapFileName.Data()
99  << ", check ANITA_UTIL_INSTALL_DIR environment variable is correctly set" << std::endl;
100  }
101 
102  hCurrent = NULL;
103  grCurrent = NULL;
104 }
105 
106 
107 
108 
109 
110 //---------------------------------------------------------------------------------------------------------
118 void AntarcticaMapPlotter::getRelXYFromLatLong(Double_t latitude, Double_t longitude,Double_t &x, Double_t &y){
119  /* This function, which really does all the hard work, was made either by Matt Mottram or Ryan */
120 
121  // Negative longitude is west
122  // All latitudes assumed south
123  Double_t absLat=TMath::Abs(latitude);
124  Double_t r=RadiusOfEarth*TMath::Cos((90.-TrueScaleLat)*TMath::DegToRad())*TMath::Tan((90-absLat)*TMath::DegToRad());
125  y=r*TMath::Cos(longitude*TMath::DegToRad());
126  x=r*TMath::Sin(longitude*TMath::DegToRad());
127 
128  y*=scale;
129  x*=scale;
130 
131  y+=yOffset;
132  x+=xOffest;
133 
134  y/=ySize;
135  x/=xSize;
136 
137 }
138 
139 
140 
141 
142 
143 
144 
145 //---------------------------------------------------------------------------------------------------------
153 Int_t AntarcticaMapPlotter::Fill(Double_t latitude, Double_t longitude, Double_t weight){
154  Double_t x, y;
155  getRelXYFromLatLong(latitude, longitude, x, y);
156  return hCurrent->Fill(x, y, weight);
157 }
158 
159 
160 
161 
162 
163 
164 //---------------------------------------------------------------------------------------------------------
169 
170  TString canName = TString::Format("can%s", hCurrent->GetName());
171  TString canTitle = TString::Format("Canvas of %s", hCurrent->GetTitle());
172  TCanvas* can = new TCanvas(canName, canTitle, (Int_t) xSize, (Int_t) ySize);
173  can->Draw();
174  can->SetTopMargin(0.03);
175  can->SetBottomMargin(0.03);
176  can->SetLeftMargin(0.03);
177  can->SetRightMargin(0.15);
178  if(img){
179  img->Draw("same");
180  }
181  hCurrent->Draw(opt + "same");
182 }
183 
184 
185 
186 
187 
188 
189 
190 //---------------------------------------------------------------------------------------------------------
195 
196  TString canName = TString::Format("can%s", grCurrent->GetName());
197  TString canTitle = TString::Format("Canvas of %s", grCurrent->GetTitle());
198  TCanvas* can = new TCanvas(canName, canTitle, (Int_t) xSize, (Int_t) ySize);
199  can->Draw();
200  can->SetTopMargin(0.03);
201  can->SetBottomMargin(0.03);
202  can->SetLeftMargin(0.03);
203  can->SetRightMargin(0.15);
204  if(img){
205  img->Draw("same");
206  }
207  grCurrent->Draw(opt + "same");
208 }
209 
210 
211 
212 
213 
214 
215 
216 
217 //---------------------------------------------------------------------------------------------------------
223 
224  std::map<TString, TH2D*>::iterator histItr = hists.find(name);
225  Int_t successState = 0;
226  if(histItr == hists.end()){
227  successState = 0;
228  }
229  else{
230  hCurrent = histItr->second;
231  successState = 1;
232  }
233  return successState;
234 }
235 
236 
237 
238 
239 
240 
241 //---------------------------------------------------------------------------------------------------------
249 void AntarcticaMapPlotter::addHistogram(TString name, TString title, Int_t nBinsX, Int_t nBinsY){
250 
251  if(setCurrentHistogram(name)==0){
252  TH2D* theHist = new TH2D(name, title, nBinsX, 0, 1, nBinsY, 0, 1);
253  hists[name] = theHist;
254  hCurrent = theHist;
255  }
256  else{
257  std::cerr << "Warning in " << __FILE__ << "! Histogram with name " << name.Data() << ", already exists!" << std::endl;
258  }
259 }
260 
261 
262 
263 
264 
265 
266 
267 //---------------------------------------------------------------------------------------------------------
276 void AntarcticaMapPlotter::addTGraph(TString name, TString title, Int_t n,
277  Double_t* latitudes, Double_t* longitudes){
278 
279  if(setCurrentTGraph(name)==0){
280  std::vector<Double_t> xs(n);
281  std::vector<Double_t> ys(n);
282  for(Int_t i=0; i<n; i++){
283  getRelXYFromLatLong(latitudes[i], longitudes[i], xs[i], ys[i]);
284  }
285  TGraph* theGraph = new TGraph(n, &xs[0], &ys[0]);
286 
287  theGraph->SetName(name);
288  theGraph->SetTitle(title);
289  grs[name] = theGraph;
290  grCurrent = theGraph;
291  }
292  else{
293  std::cerr << "Warning in " << __FILE__ << "! TGraph with name "
294  << name.Data() << ", already exists!" << std::endl;
295  if(n>0){
296  std::cerr << "No points added to " << name.Data() << "!" << std::endl;
297  }
298  }
299 }
300 
301 
302 
303 
304 
305 
306 
307 
308 //---------------------------------------------------------------------------------------------------------
314 
315  std::map<TString, TGraph*>::iterator grItr = grs.find(name);
316  Int_t successState = 0;
317  if(grItr == grs.end()){
318  successState = 0;
319  }
320  else{
321  grCurrent = grItr->second;
322  successState = 1;
323  }
324  return successState;
325 }
326 
327 
328 
329 
330 
331 
332 
333 //---------------------------------------------------------------------------------------------------------
339  return grCurrent;
340 }
341 
342 
343 
344 
345 
346 
347 //---------------------------------------------------------------------------------------------------------
353  return hCurrent;
354 }
TH2D * getCurrentHistogram()
Return pointer to current histogram.
void DrawTGraph(TString opt)
Draws the canvas, image and histogram.
Int_t setCurrentTGraph(TString name)
Sets the current histrogram pointer* hCurrent.
TGraph * getCurrentTGraph()
Return pointer to current TGraph.
void DrawHist(TString opt)
Draws the canvas, image and histogram.
Class to plot reconstructed events onto a picture of Antarctica.
Int_t Fill(Double_t latitude, Double_t longitude, Double_t weight=1)
How to fill the histogram, converts latitude and longitude into x/y bins first.
Int_t setCurrentHistogram(TString name)
Sets the current histogram pointer* hCurrent.
void addHistogram(TString name, TString title, Int_t nBinsX, Int_t nBinsY)
Creates a new histogram to go in the internal map and sets it as the current histogram.
void addTGraph(TString name, TString title, Int_t n=0, Double_t *latitude=NULL, Double_t *longitude=NULL)
Creates a new graph to go in the internal map and sets it as the current histogram.
AntarcticaMapPlotter()
Constructor.
~AntarcticaMapPlotter()
Destructor.