SineSubtractCache.cc
1 #include "SineSubtractCache.h"
2 #include <iostream>
3 #include "AnitaDataset.h"
4 #include "TFile.h"
5 #include "TTree.h"
6 #include "FilteredAnitaEvent.h"
7 #include "SineSubtractCache.h"
8 #include "FilterStrategy.h"
9 #include "RawAnitaHeader.h"
10 #include "TDirectory.h"
11 
12 
13 const TString ssrTreeName = "sineSubResultTree";
14 
15 TString UCorrelator::SineSubtractCache::branchName(AnitaPol::AnitaPol_t pol, Int_t ant){
16  return TString::Format("ssr_%d_%d", (int)pol, ant);
17 }
18 
19 TString UCorrelator::SineSubtractCache::fileName(const char* specDir, UInt_t hash, Int_t run){
20  int av = AnitaVersion::get();
21  return TString::Format("%s/sineSubResults_%u_anita%d_run%d.root", specDir, hash, av, run);
22 }
23 
24 
25 void UCorrelator::SineSubtractCache::makeCache(int run, SineSubtractFilter* ssf){
26 
27  UCorrelator::SineSubtractFilter::setUseCache(false);
28  FFTtools::SineSubtractResult* results[AnitaPol::kNotAPol][NUM_SEAVEYS] = {{NULL}};
29 
30  const char* ssDesc = ssf->description();
31  if(!ssDesc){
32  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", can't cache or use cached results without SineSubtract description!" << std::endl;
33  }
34  else{
35  UInt_t hash = TString(ssDesc).Hash();
36 
37  const char* specDir = getenv("UCORRELATOR_SPECAVG_DIR");
38  if(!specDir){
39  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", can't cache or use cached results without UCORRELATOR_SPECAVG_DIR environment variable!" << std::endl;
40  }
41  else{
42 
43  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", will generate cached sine subtraction results!" << std::endl;
44  AnitaDataset d(run);
45 
46  FilterStrategy fs;
47  fs.addOperation(ssf);
48 
49  TFile* fOut = new TFile(fileName(specDir, hash, run), "recreate");
50  TTree* tOut = new TTree(ssrTreeName, ssrTreeName);
51  UInt_t eventNumber = 0;
52  tOut->Branch("eventNumber", &eventNumber);
53  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
55  for(int ant=0; ant < NUM_SEAVEYS; ant++){
56  tOut->Branch(branchName(pol, ant), &results[pol][ant]);
57  }
58  }
59 
60 
61  const int n = d.N();
62  const double deltaPrint = double(n)/1000;
63  double nextPrint = 0;
64  for(int entry=0; entry < d.N(); entry++){
65  d.getEntry(entry);
66 
67  FilteredAnitaEvent fEv(d.useful(), &fs, d.gps(), d.header());
68 
69  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
71  for(int ant=0; ant < NUM_SEAVEYS; ant++){
72  const FFTtools::SineSubtract* ss = ssf->sinsub(pol, ant);
74  results[pol][ant] = ssr;
75  }
76  }
77 
78  eventNumber = d.header()->eventNumber;
79  tOut->Fill();
80  if(entry >= nextPrint){
81  const int nm = 50;
82  int m = nm*nextPrint/n;
83  fprintf(stderr, "\r%4.2f %% complete", 100*nextPrint/n);
84  std::cerr << "[";
85  for(int i=0; i < m; i++) std::cerr << "=";
86  for(int i=m; i < nm; i++) std::cerr << " ";
87  std::cerr << "]";
88  nextPrint += deltaPrint;
89  if(nextPrint >= n){std::cerr << std::endl;}
90  }
91  // if(entry > 100) break;
92  }
93  tOut->BuildIndex("eventNumber"); // does this get saved?
94  fOut->Write();
95  fOut->Close();
96 
97  }
98  }
99 }
100 
101 
102 
103 
104 
105 
106 
107 
108 
115  : fDebug(false), fFile(NULL), fTree(NULL), fDescHash(0), fSpecDir(), fCurrentRun(-1), fLastAttemptedRun(-1), fLastEventNumber(0)
116 {
117 
118 
119  for(int pol=0; pol < AnitaPol::kNotAPol; pol++){
120  for(int ant=0; ant < NUM_SEAVEYS; ant++){
121  results[pol][ant] = NULL;
122  }
123  }
124 
125  if(!ssDesc){
126  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", can't cache or use cached results without SineSubtract description!" << std::endl;
127  }
128  else{
129  fDescHash = TString(ssDesc).Hash();
130 
131  const char* specDir = getenv("UCORRELATOR_SPECAVG_DIR");
132  if(!specDir){
133  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", can't cache or use cached results without UCORRELATOR_SPECAVG_DIR environment variable!" << std::endl;
134  }
135  else{
136  fSpecDir = specDir;
137  }
138  }
139 }
140 
141 
142 
143 UCorrelator::SineSubtractCache::~SineSubtractCache(){
144 
145  if(fFile){
146  fFile->Close();
147  }
148 }
149 
150 
151 
152 const FFTtools::SineSubtractResult* UCorrelator::SineSubtractCache::getResult(UInt_t eventNumber, AnitaPol::AnitaPol_t pol, Int_t ant){
153 
154  // hard to check whether anita version is correct...
155  // this should happen
156 
157  // std::cerr << eventNumber << "\t" << pol << "\t" << ant << std::endl;
158 
159  if(eventNumber != fLastEventNumber){
160  int run = AnitaDataset::getRunContainingEventNumber(eventNumber);
161  if(run!=fLastAttemptedRun){//fCurrentRun){
162  loadRun(run);
163  }
164  if(fTree){
165  Int_t entry = fTree->GetEntryNumberWithIndex(eventNumber);
166  if(entry >= 0){
167  fTree->GetEntry(entry);
168 
169  if(eventNumber != fLastEventNumber){
170  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", loaded sine subtract cache: run = " << fCurrentRun << ", eventNumber requested = "
171  << eventNumber << ", but eventNumber read = " << fLastEventNumber << std::endl;
172  }
173  }
174  else{
175  static int numEntryWarnings = 0;
176  const int maxEntryWarnings = 20;
177  if(numEntryWarnings < maxEntryWarnings || fDebug){
178  std::cerr << "Warning " << (numEntryWarnings+1) << " of " << maxEntryWarnings << " in " << __PRETTY_FUNCTION__ << ", can't find entry "
179  << entry << " in " << fTree->GetName() << " in file " << fFile->GetName()
180  << " for eventNumber " << eventNumber << std::endl;
181  numEntryWarnings++;
182  }
183  return NULL;
184  }
185  }
186  else{
187  static int numTreeWarnings = 0;
188  const int maxTreeWarnings = 20;
189  if(numTreeWarnings < maxTreeWarnings || fDebug){
190  std::cerr << "Warning " << (numTreeWarnings+1) << " of " << maxTreeWarnings << " in " << __PRETTY_FUNCTION__ << ", can't find "
191  << ssrTreeName << " in file " << fFile->GetName() << std::endl;
192  numTreeWarnings++;
193  }
194  }
195  }
196 
197  return results[pol][ant];
198 }
199 
200 
201 
202 void UCorrelator::SineSubtractCache::loadRun(Int_t run){
203 
204  if(fSpecDir.Length() > 0 && fDescHash > 0){
205  if(fFile){
206  fFile->Close();
207  fFile = NULL;
208  fTree = NULL;
209  }
210 
211  const TString theRootPwd = gDirectory->GetPath();
212  TString fName = fileName(fSpecDir, fDescHash, run);
213  fFile = TFile::Open(fName, "read");
214  if(fFile){
215  fTree = (TTree*) fFile->Get(ssrTreeName);
216 
217  fTree->SetBranchAddress("eventNumber", &fLastEventNumber);
218  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
220  for(int ant=0; ant < NUM_SEAVEYS; ant++){
221  fTree->SetBranchAddress(branchName(pol, ant), &results[pol][ant]);
222  }
223  }
224  fTree->BuildIndex("eventNumber");
225  fTree->GetEntry(0);
226  fCurrentRun = run;
227 
228  if(fDebug){
229  std::cerr << "Loaded first entry in run " << fCurrentRun << ", which has eventNumber " << fLastEventNumber << std::endl;
230  }
231  gDirectory->cd(theRootPwd);
232  }
233  else{
234  std::cerr << "Error in " << __PRETTY_FUNCTION__ << ", couldn't open file " << fName << std::endl;
235  }
236  fLastAttemptedRun = run;
237  }
238 }
A filter strategy defines the sets of filters that are used and provides some introspection abilities...
USeful in for loops.
const SineSubtractResult * getResult() const
Definition: SineSubtract.h:657
This class is intended to store all the necessary data about an ANITA event for filtering and analysi...
enum AnitaPol::EAnitaPol AnitaPol_t
Polarisation enumeration.
static int getRunContainingEventNumber(UInt_t eventNumber)
Get the run that contains the eventNumber.
void addOperation(FilterOperation *f, bool enable_output=false)