ThermalChain.cxx
1 #include "ThermalChain.h"
2 #include "SummarySet.h"
3 #include "TChain.h"
4 #include "TEntryList.h"
5 #include "ProgressBar.h"
6 #include "TROOT.h"
7 #include "AnitaDataset.h"
8 
9 
10 Acclaim::ThermalChain::ThermalChain(const char* glob, const char* treeName){
11 
12  fChain = new TChain(treeName);
13  fChain->Add(glob);
14 
15  TString hiCalGlob(glob);
16  hiCalGlob.ReplaceAll("makeThermalTree", "makeHiCalTree");
17 
18  fFriendChain1 = new TChain("hiCalTree");
19  fFriendChain1->Add(hiCalGlob);
20 
21  TString surfaceGlob(glob);
22  surfaceGlob.ReplaceAll("makeThermalTree", "makeSurfaceTree");
23 
24  fFriendChain2 = new TChain("surfaceTree");
25  fFriendChain2->Add(surfaceGlob);
26 
27  // std::cout << fFriendChain2->GetEntries() << std::endl;
28  fChain->AddFriend(fFriendChain1);
29  fChain->AddFriend(fFriendChain2);
30 
31  gROOT->ProcessLine("#include \"FFTtools.h\""); // hack to get various delta phi wrap Draw things to work in stand alone executables
32 
33  fCut = "";
34  fEntryListDirty = true;
35  fEntryList = NULL;
36  fUseProof = false;
37  fAnitaVersion = 0;
38  setBranches();
39 }
40 
41 Acclaim::ThermalChain::~ThermalChain(){
42 
43  if(fEntryList){
44  fChain->SetEntryList(0);
45  delete fEntryList;
46  fEntryList = NULL;
47  }
48 
49  // Pretty sure ROOT takes care of this...
50  fFriendChain1 = NULL;
51  fFriendChain2 = NULL;
52  // if(fFriendChain){
53  // delete fFriendChain;
54  // fFriendChain = NULL;
55  // }
56 
57  if(fChain){
58  delete fChain;
59  fChain = NULL;
60  }
61 }
62 
63 
64 void Acclaim::ThermalChain::setBranches(){
65 
66  // these ones need a type conversion before public consumption...
67  fChain->SetBranchAddress("pol", &polFloat);
68  fChain->SetBranchAddress("peakInd", &peakIndFloat);
69  fChain->SetBranchAddress("eventNumber", &eventNumberInt);
70  fChain->SetBranchAddress("realTime", &realTimeInt);
71 
72  fChain->SetBranchAddress("run", &run);
73  fChain->SetBranchAddress("peak_phi", &peak_phi);
74  fChain->SetBranchAddress("peak_theta", &peak_theta);
75  fChain->SetBranchAddress("anitaLocation_longitude", &anita_longitude);
76  fChain->SetBranchAddress("anitaLocation_latitude", &anita_latitude);
77  fChain->SetBranchAddress("anitaLocation_altitude", &anita_altitude);
78  fChain->SetBranchAddress("anitaLocation_heading", &anita_heading);
79  fChain->SetBranchAddress("coherent_filtered_snr", &coherent_filtered_snr);
80  fChain->SetBranchAddress("weight", &weight);
81  fChain->SetBranchAddress("mc_energy", &mc_energy);
82 
83  fChain->SetBranchAddress("peak_value", &peak_value);
84  fChain->SetBranchAddress("coherent_filtered_peakHilbert", &coherent_filtered_peakHilbert);
85  fChain->SetBranchAddress("deconvolved_filtered_peakHilbert", &deconvolved_filtered_peakHilbert);
86  fChain->SetBranchAddress("coherent_filtered_impulsivityMeasure", &coherent_filtered_impulsivityMeasure);
87  fChain->SetBranchAddress("deconvolved_filtered_impulsivityMeasure", &deconvolved_filtered_impulsivityMeasure);
88  fChain->SetBranchAddress("coherent_filtered_fracPowerWindowGradient", &coherent_filtered_fracPowerWindowGradient);
89  fChain->SetBranchAddress("deconvolved_filtered_fracPowerWindowGradient", &deconvolved_filtered_fracPowerWindowGradient);
90 
91 
92  fFriendChain1->SetBranchAddress("duringHiCal", &duringHiCal);
93  fFriendChain1->SetBranchAddress("hiCalPhi", &hiCalPhi);
94  fFriendChain1->SetBranchAddress("hiCalTheta", &hiCalTheta);
95  fFriendChain1->SetBranchAddress("eventNumber2", &eventNumber2);
96  fFriendChain1->SetBranchAddress("run2", &run2);
97 
98 
99  fFriendChain2->SetBranchAddress("longitude", &longitude);
100  fFriendChain2->SetBranchAddress("latitude", &latitude);
101  fFriendChain2->SetBranchAddress("altitude", &altitude);
102  fFriendChain2->SetBranchAddress("thetaAdjustmentRequired", &thetaAdjustmentRequired);
103  fFriendChain2->SetBranchAddress("onContinent", &onContinent);
104  fFriendChain2->SetBranchAddress("onIceShelf", &onIceShelf);
105  fFriendChain2->SetBranchAddress("iceThickness", &iceThickness);
106  fFriendChain2->SetBranchAddress("eventNumber3", &eventNumber3);
107  fFriendChain2->SetBranchAddress("run3", &run3);
108 
109 }
110 
111 
112 
113 
114 
120 void Acclaim::ThermalChain::setCut(const TCut& cut){
121  fCut = "";
122  addCut(cut);
123 }
124 
130 void Acclaim::ThermalChain::setCut(const char* cut){
131  fCut = "";
132  addCut(cut);
133 }
134 
135 
142 void Acclaim::ThermalChain::addCut(const char* cut){
143  TCut cut2(cut);
144  addCut(cut2);
145 }
146 
147 
154 void Acclaim::ThermalChain::addCut(const TCut& cut){
155  TCut oldCut;
156  fCut += cut;
157  if(fCut != oldCut){
158  fEntryListDirty = true;
159  }
160 }
161 
162 
163 void Acclaim::ThermalChain::SetUseProof(bool useProof){
164  if(useProof){
166  }
167  fChain->SetProof(useProof);
168  fUseProof = useProof;
169 }
170 
171 
176 void Acclaim::ThermalChain::makeSelection() const {
177  if(fEntryListDirty){
178 
179  if(fEntryList){
180  fChain->SetEntryList(0);
181  delete fEntryList;
182  fEntryList = NULL;
183  }
184 
185  fChain->SetProof(false);
186 
187  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", updating fEntryList..." << std::endl;
188  ProgressBar p(1);
189  fChain->Draw(">>fEntryList", fCut, "entrylist");
190  fEntryList = dynamic_cast<TEntryList*>(gROOT->FindObject("fEntryList"));
191  if(!fEntryList){
192  std::cerr << "Error! couldn't find fEntryList!" << std::endl;
193  }
194  else{
195  fEntryListDirty = false;
196  fChain->SetEntryList(fEntryList);
197  }
198 
199  // Turn proof back on, if it was on...
200  fChain->SetProof(fUseProof);
201 
202  p++;
203  }
204 }
205 
206 
211 Long64_t Acclaim::ThermalChain::N() const {
212  makeSelection();
213  return fEntryList->GetN();
214 }
215 
216 Long64_t Acclaim::ThermalChain::getEntry(Long64_t entry){
217 
218  makeSelection();
219  Int_t treeIndex = -1;
220  Long64_t treeEntry = fEntryList->GetEntryAndTree(entry,treeIndex);
221  Long64_t chainEntry = treeEntry+fChain->GetTreeOffset()[treeIndex];
222 
223  Long64_t retVal = fChain->GetEntry(chainEntry);
224 
225  doTypeConversions();
226 
227  return retVal;
228 }
229 
230 void Acclaim::ThermalChain::doTypeConversions(){
231  pol = (AnitaPol::AnitaPol_t) polFloat;
232  peakInd = (Int_t) peakIndFloat;
233  eventNumber = (UInt_t) eventNumberInt;
234  realTime = (UInt_t) realTimeInt;
235 
236  if(eventNumber2 != eventNumber || run2 != run){
237  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", mismatch in friendChain1!" << std::endl;
238  }
239  if(eventNumber3 != eventNumber || run3 != run){
240  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", mismatch in friendChain2!" << std::endl;
241  }
242 }
243 
244 
245 
246 
247 Long64_t Acclaim::ThermalChain::getEvent(UInt_t eventNumber){
248  // Rolled my own lookup-by-eventNumber since TChainIndex doesn't seem up to the task
249  // maybe because the eventNumber marker isn't as well behaved as we would like?
250 
251  // First set the correct AnitaVersion in case I compile with the default to ANITA-4
252  if(!fAnitaVersion){
253  fChain->GetEntry(0);
254  doTypeConversions();
255  AnitaVersion::setVersionFromUnixTime(realTime);
256  fAnitaVersion = AnitaVersion::get();
257  }
258 
259  // Look up the run, and find the appropriate file
260  Int_t run = AnitaDataset::getRunContainingEventNumber(eventNumber);
261  TString desiredFileName = TString::Format("makeThermalTree_%d_", run);
262  TIter next(fChain->GetListOfFiles());
263  Int_t desiredTreeNumber = -1;
264  while(TObject* f = next()){
265  desiredTreeNumber++;
266  TString fileName = f->GetTitle();
267  if(fileName.Contains(desiredFileName)){
268  break;
269  }
270  }
271 
272 
273  // Check boundaries
274  if(desiredTreeNumber > -1 && desiredTreeNumber < fChain->GetListOfFiles()->GetEntries()){
275 
276  // Get the first entry in file corresponding to run
277  Long64_t entry = fChain->GetTreeOffset()[desiredTreeNumber];
278  fChain->GetEntry(entry);
279  doTypeConversions();
280 
281  // If event number was perfectly monotonic with no gaps this would work first time
282  // But as it is, need multiple attempts
283  const int maxTries = 50; // (Allow a lot of attempts)
284  for(int numTries = 0; numTries < maxTries; numTries++){
285 
286  // Update entry difference in eventNumber from last event and desired event
287  Int_t deltaEntries = eventNumber - this->eventNumber;
288  entry += deltaEntries;
289 
290  // Get most recent guessed entry
291  Long64_t nb = fChain->GetEntry(entry);
292  doTypeConversions();
293 
294  // If it's a match then we're done, if not then loop again
295  if(this->eventNumber == eventNumber){
296  // std::cout << this->eventNumber << "\t" << eventNumber << std::endl;
297  return nb;
298  break;
299  }
300 
301  }
302  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", unable to find eventNumber "
303  << eventNumber << " after " << maxTries << " attempts, giving up!" << std::endl;
304  return -1;
305  }
306  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", unable to find file corresponding to run " << run << std::endl;
307  return -1;
308 }
309 
310 
311 Adu5Pat Acclaim::ThermalChain::pat(){
312  Adu5Pat pat;
313  pat.latitude = anita_latitude;
314  pat.longitude = anita_longitude;
315  pat.altitude = anita_altitude;
316  pat.heading = anita_heading;
317  pat.realTime = realTime;
318  return pat;
319 }
320 
321 
322 
323 Double_t Acclaim::ThermalChain::fisherDiscriminant(){
325  // THIS NEEDS TO BE MAINTAINED BY HAND! //
327 
328  // from DrawStrings.h
329  // const TString fisherDiscriminant = "0.898497+(1.929594*coherent_filtered_fracPowerWindowGradient/deconvolved_filtered_fracPowerWindowGradient)+(-0.195909*deconvolved_filtered_fracPowerWindowGradient)+(5.943355*coherent_filtered_impulsivityMeasure)+(0.826114*deconvolved_filtered_impulsivityMeasure)+(0.021763*coherent_filtered_peakHilbert)+(-0.012670*deconvolved_filtered_peakHilbert)+(-0.394201*peak_value)";
330 
331  return 0.898497+(1.929594*coherent_filtered_fracPowerWindowGradient/deconvolved_filtered_fracPowerWindowGradient)+(-0.195909*deconvolved_filtered_fracPowerWindowGradient)+(5.943355*coherent_filtered_impulsivityMeasure)+(0.826114*deconvolved_filtered_impulsivityMeasure)+(0.021763*coherent_filtered_peakHilbert)+(-0.012670*deconvolved_filtered_peakHilbert)+(-0.394201*peak_value);
332 }
void setCut(const TCut &cut)
Adu5Pat – The ADU5 Position and Attitude Data.
Definition: Adu5Pat.h:26
static TProof * startProof()
You take it, you own it, you delete it.
Definition: SummarySet.cxx:61
Float_t latitude
In degrees.
Definition: Adu5Pat.h:42
Float_t longitude
In degrees.
Definition: Adu5Pat.h:43
UInt_t realTime
Time from the GPS unit.
Definition: Adu5Pat.h:37
void addCut(const TCut &cut)
Float_t altitude
In metres.
Definition: Adu5Pat.h:44
Long64_t N() const
Float_t heading
0 is facing north, 180 is facing south
Definition: Adu5Pat.h:45
enum AnitaPol::EAnitaPol AnitaPol_t
Polarisation enumeration.
static int getRunContainingEventNumber(UInt_t eventNumber)
Get the run that contains the eventNumber.