NoiseMonitor.cc
1 #include "NoiseMonitor.h"
2 #include "FilteredAnitaEvent.h"
3 #include "RawAnitaHeader.h"
4 #include "FilterStrategy.h"
5 #include "FilterOperation.h"
6 #include "AnitaDataset.h"
7 #include <numeric>
8 
9 #include "TTree.h"
10 #include "TFile.h"
11 #include "TProfile2D.h"
12 #include <stdlib.h>
13 
14 void NoiseMonitor::getRmsDirEnv(){
15  fRmsDir = getenv("ANITA_RMS_DIR");
16  if(!fRmsDir){
17  std::cerr << "Warning in " << __PRETTY_FUNCTION__
18  << ", can't see env ANITA_RMS_DIR. "
19  << "Will look for/make rms profiles in pwd"
20  << std::endl;
21  fRmsDir = ".";
22  }
23 }
24 
25 UInt_t NoiseMonitor::makeStratHashFromDesc(const FilterStrategy* fs){
26  TString hasher;
27  for(unsigned i=0; i < fs->nOperations(); i++){
28  hasher += fs->getOperation(i)->description();
29  }
30  UInt_t hash = hasher.Hash();
31  return hash;
32 }
33 
34 
35 TString NoiseMonitor::getFileName(int run){
36  return TString::Format("%s/rms_anita%d_run%d_hash%u.root", fRmsDir, AnitaVersion::get(), run, fHash);
37 }
38 
39 void NoiseMonitor::getProfilesFromFileRun(int run){
40 
41  TString theRootPwd = gDirectory->GetPath();
42  TString fileName = getFileName(run);
43  if(fFile){
44  fFile->Close();
45  }
46  fFile = TFile::Open(fileName);
47 
48  ProfPair p;
49  if(fFile){
50  TProfile2D* pH = (TProfile2D*) fFile->Get(getHistName(AnitaPol::kHorizontal, run));
51  TProfile2D* pV = (TProfile2D*) fFile->Get(getHistName(AnitaPol::kVertical, run));
52  p.set(pH, pV);
53  }
54  else{
55  std::cerr << "Warning in " << __PRETTY_FUNCTION__ << ", couldn't find file " << fileName << ", will need to generate it. This may take some time." << std::endl;
56  }
57 
58  if(!p.get(AnitaPol::kVertical) || !p.get(AnitaPol::kHorizontal)){
59  std::cerr << "Error! unable to get the profiles I just tried to read for run " << run << ", "
60  << p.get(AnitaPol::kVertical) << ", " << p.get(AnitaPol::kHorizontal) << std::endl;
61  makeProfiles(run);
62 
63  fFile = TFile::Open(fileName);
64  TProfile2D* pH = (TProfile2D*) fFile->Get(getHistName(AnitaPol::kHorizontal, run));
65  TProfile2D* pV = (TProfile2D*) fFile->Get(getHistName(AnitaPol::kVertical, run));
66  p.set(pH, pV);
67  }
68 
69  if(!p.get(AnitaPol::kVertical) || !p.get(AnitaPol::kHorizontal)){
70  std::cerr << "Warning in " << __PRETTY_FUNCTION__
71  << ", unable to find RMS profiles for run " << run
72  << ". Something bad is about to happen!"
73  << std::endl;
74  }
75  // fFiles[run] = fFile;
76 
77  fCurrent.set(p);
78 
79  gDirectory->cd(theRootPwd);
80 }
81 
82 
83 TString NoiseMonitor::getHistName(AnitaPol::AnitaPol_t pol, int run){
84  TString name = TString::Format("rms?_%d_%u", run, fHash);
85  const Ssiz_t namePolCharPos = name.First('?');
86  name[namePolCharPos] = AnitaPol::polAsChar(pol);
87  return name;
88 }
89 
90 
91 void NoiseMonitor::makeProfiles(int run){
92 
93  TString fileName = getFileName(run);
94 
95  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", generating minimim bias RMS profiles for run " << run << " in file " << fileName << std::endl;
96  TFile* f = TFile::Open(fileName, "recreate");
97 
98  AnitaDataset d(run);
99  const int n = d.N();
100  RawAnitaHeader* header = NULL;
101 
102  d.first();
103  header = d.header();
104  double startTime = header->realTime;
105 
106  d.last();
107  header = d.header();
108  double endTime = header->realTime + 1; // make sure last event is in bin
109 
110  const int nBin = (endTime - startTime)/double(defaultTimeScaleSeconds);
111 
112  // temp turn off output, turned back on at the end of the function
113  std::vector<bool> original_enable_outputs = fFilterStrat->enable_outputs;
114  for(unsigned i=0; i < fFilterStrat->enable_outputs.size(); i++){
115  fFilterStrat->enable_outputs.at(i) = false;
116  }
117 
118  TString name = TString::Format("rms?_%d_%u", run, fHash);
119  TString title = TString::Format("RMS for each ?Pol for run %d (FilterStrategy operations descriptions hash = %u);Antenna;realTime", run, fHash);
120 
121  const Ssiz_t titlePolCharPos = title.First('?');
122 
123  std::vector<TProfile2D*> profs(AnitaPol::kNotAPol, NULL);
124  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
126 
127  TString name = getHistName(pol, run);
128 
129  title[titlePolCharPos] = AnitaPol::polAsChar(pol);
130  profs.at(pol) = new TProfile2D(name, title, NUM_SEAVEYS, -0.5, NUM_SEAVEYS-0.5, nBin, startTime, endTime);
131  }
132 
133  const double deltaPrint = double(n)/1000;
134  double nextPrint = 0;
135 
136  for(int entry=0; entry < n; entry++){
137  d.getEntry(entry);
138  header = d.header();
139  if(header->getTriggerBitRF()==0){
140 
141  FilteredAnitaEvent fEv(d.useful(), fFilterStrat, d.gps(), header);
142  for(int polInd=0; polInd < AnitaPol::kNotAPol; polInd++){
144 
145  for(int ant=0; ant < NUM_SEAVEYS; ant++){
146  const AnalysisWaveform* wf = fEv.getFilteredGraph(ant, pol);
147  const TGraphAligned* gr = wf->even();
148  double rms = TMath::RMS(gr->GetN(), gr->GetY());
149  profs.at(pol)->Fill(ant, header->realTime, rms);
150  }
151  }
152  }
153  if(entry >= nextPrint){
154  const int nm = 50;
155  int m = nm*nextPrint/n;
156  fprintf(stderr, "\r%4.2f %% complete", 100*nextPrint/n);
157  std::cerr << "[";
158  for(int i=0; i < m; i++) std::cerr << "=";
159  for(int i=m; i < nm; i++) std::cerr << " ";
160  std::cerr << "]";
161  nextPrint += deltaPrint;
162  if(nextPrint >= n){std::cerr << std::endl;}
163  }
164 
165  // if(entry == nextPrint){
166  // std::cerr << "\r" << 100*(entry+1)/n << "% ";
167  // nextPrint += printEvery;
168  // nextPrint = nextPrint >= n ? n - 1 : nextPrint;
169  // if(entry==n-1){
170  // std::cerr << std::endl;
171  // }
172  // }
173  }
174 
175  // turn the outputs back on, if they were on
176  fFilterStrat->enable_outputs = original_enable_outputs;
177 
178  f->Write();
179  f->Close();
180 
181  std::cout << "Info in " << __PRETTY_FUNCTION__ << ", complete!" << std::endl;
182 }
183 
184 
185 
193  : fFilterStrat(fs), fFile(NULL)
194 {
195  getRmsDirEnv();
196  fHash = makeStratHashFromDesc(fs);
197 }
198 
199 
200 
208  : fFilterStrat(NULL), fFile(NULL)
209 {
210  getRmsDirEnv();
211  fHash = hash;
212 }
213 
214 
215 
216 
221 
222  // std::map<int, TFile*>::iterator it;
223  // for(it = fFiles.begin(); it != fFiles.end(); it++){
224  // it->second->Close();
225  // }
226  if(fFile){
227  fFile->Close();
228  fFile = NULL;
229  }
230 }
231 
232 
233 
234 // /**
235 // * Set fCurrent if found,
236 // *
237 // * @param realTime is used to find the run, to find the profile in memory
238 // */
239 // void NoiseMonitor::findProfilesInMemoryFromTime(UInt_t realTime){
240 
241 // int run = AnitaDataset::getRunAtTime(double(realTime));
242 // findProfilesInMemoryFromRun(run);
243 // }
244 
245 
246 
247 // /**
248 // * Set fCurrent if found,
249 // *
250 // * @param realTime is used to find the run, to find the profile in memory
251 // */
252 // void NoiseMonitor::findProfilesInMemoryFromRun(Int_t run){
253 
254 // // first look in the map
255 // std::map<int, ProfPair>::iterator it = fRunProfiles.find(run);
256 // if(it!=fRunProfiles.end()){
257 // // then this the map
258 // fCurrent.set(it->second);
259 // }
260 // else{
261 // getProfilesFromFileRun(run);
262 // }
263 // }
264 
265 
267  int run = AnitaDataset::getRunAtTime(double(realTime));
268  getProfilesFromFileRun(run); // update fCurrent
269 }
270 
271 
272 double NoiseMonitor::getRMS(AnitaPol::AnitaPol_t pol, Int_t ant, UInt_t realTime){
273 
274  AnitaVersion::setVersionFromUnixTime(realTime);
275 
276  const TProfile2D* p = fCurrent.get(pol);
277 
278  if(!(p && realTime >= fCurrent.startTime() && realTime < fCurrent.endTime())){
279  getProfilesFromFileTime(realTime); // update fCurrent
280  p = fCurrent.get(pol); // should get p again
281  }
282 
283  // surely find bin should be const?
284  double rms = 0;
285  if(p){
286  Int_t bin = ((TProfile2D*)p)->FindBin(ant, realTime);
287  rms = p->GetBinContent(bin);
288  }
289  return rms;
290 }
291 
292 
293 void NoiseMonitor::ProfPair::set(const TProfile2D* h, const TProfile2D* v){
294 
295  H = const_cast<TProfile2D*>(h);
296  V = const_cast<TProfile2D*>(v);
297  // use both H and V so if one is NULL it fucks up
298  // presumably the limits are the same.. but I'm not going to check right now
299  fStartTime = H->GetYaxis()->GetBinLowEdge(1);
300  fEndTime = V->GetYaxis()->GetBinLowEdge(V->GetNbinsY());
301 
302  // prettiness
303  if(H && V){
304  H->GetYaxis()->SetTimeDisplay(1);
305  V->GetYaxis()->SetTimeDisplay(1);
306 
307  const char* timeFormat = "#splitline{%H:%M}{%d/%m/%Y}";
308  H->GetYaxis()->SetTimeFormat(timeFormat);
309  V->GetYaxis()->SetTimeFormat(timeFormat);
310 
311  H->GetYaxis()->SetTimeOffset(0);
312  V->GetYaxis()->SetTimeOffset(0);
313 
314  H->GetYaxis()->SetLabelSize(0.02);
315  V->GetYaxis()->SetLabelSize(0.02);
316 
317  fStartTime = H->GetYaxis()->GetBinLowEdge(1);
318  fEndTime = V->GetYaxis()->GetBinLowEdge(V->GetNbinsY());
319  }
320  else{
321  // something impossible
322  fStartTime = 0;
323  fEndTime = 0;
324  }
325 
326  // H->GetXaxis()->LabelsOption("h");
327  // V->GetXaxis()->LabelsOption("h");
328 }
329 
330 void NoiseMonitor::ProfPair::set(const ProfPair& other){
331 
332  set(other.get(AnitaPol::kHorizontal), other.get(AnitaPol::kVertical));
333 
334 }
static int getRunAtTime(double t)
virtual ~NoiseMonitor()
NoiseMonitor(FilterStrategy *fs)
virtual const char * description() const =0
A filter strategy defines the sets of filters that are used and provides some introspection abilities...
RawAnitaHeader – The Raw ANITA Event Header.
Int_t getTriggerBitRF() const
USeful in for loops.
size_t nOperations() const
Vertical Polarisation.
void getProfilesFromFileTime(UInt_t realTime)
char polAsChar(AnitaPol::AnitaPol_t pol)
Returns the polarisation as a character string.
const TGraphAligned * even() const
Horizontal Polarisation.
UInt_t realTime
unixTime of readout
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.
This class is intended to be the main storage vessel for ANITA waveforms. It is similar in principle ...
const FilterOperation * getOperation(size_t i) const