1 #include "AnitaTemplates.h" 18 void AnitaTemplateSummary::zeroInternals(){
20 for (Int_t poli=0; poli<NUM_POLS; poli++) {
23 memset(&coherent[poli][dir],0,
sizeof(SingleTemplateResult));
25 memset(&deconvolved[poli][dir],0,
sizeof(SingleTemplateResult));
42 : length(inLength), dT(0.1)
45 kTmpltsLoaded =
false;
46 kTmpltsDeconv =
false;
51 theImpTemplate = NULL;
52 theWaisTemplate = NULL;
53 fUseAverageCRTemplate =
false;
56 for (
int i=0; i < numCRTemplates; i++) {
57 theCRTemplates[i] = NULL;
59 if(AnitaVersion::get() == 4) fillNotchConfigs();
72 if (theImpTemplate != NULL) {
delete theImpTemplate; theImpTemplate = NULL; }
73 if (theWaisTemplate != NULL) {
delete theWaisTemplate; theWaisTemplate = NULL; }
74 for (
int i=0; i < numCRTemplates; i++) {
75 delete theCRTemplates[i];
76 theCRTemplates[i] = NULL;
82 delete[] theWaisTemplateFFT;
83 for (
int i=0; i < numCRTemplates; i++) {
84 delete[] theCRTemplates[i];
86 std::cout <<
"delete CR ffts" << std::endl;
89 kTmpltsLoaded =
false;
93 void AnitaTemplateMachine::fillNotchConfigs()
97 char* installDir = getenv(
"ANITA_UTIL_INSTALL_DIR");
98 std::ifstream inf(Form(
"%s/share/AnitaAnalysisFramework/responses/TUFFs/index.txt", installDir));
99 while(inf >> tempStr >> tempTime)
101 payloadTimes.push_back(tempTime);
102 notchConfigs.push_back(tempStr);
107 void AnitaTemplateMachine::getImpulseResponseTemplate(
int version) {
110 char* installDir = getenv(
"ANITA_UTIL_INSTALL_DIR");
114 name = Form(
"%s/share/AnitaAnalysisFramework/responses/TUFFs/averages/%s.imp", installDir, fNotchStr.c_str());
116 else name = Form(
"%s/share/AnitaAnalysisFramework/responses/SingleBRotter/all.imp", installDir);
117 TGraph *grTemplateRaw =
new TGraph(name.Data());
120 delete grTemplateRaw;
124 double xStart = grTemplatePadded->GetX()[0];
125 for (
int pt=0; pt<grTemplatePadded->GetN(); pt++) grTemplatePadded->GetX()[pt] -= xStart;
136 delete grTemplatePadded;
139 grTemplate->SetName(
"templateImp");
141 theImpTemplate = grTemplate;
151 void AnitaTemplateMachine::getCRTemplates(
int version) {
153 char* installDir = getenv(
"ANITA_UTIL_INSTALL_DIR");
155 if(version == 4 && !fUseAverageCRTemplate)
157 fname = Form(
"%s/share/AnitaAnalysisFramework/templates/crTmpltsA4_%s.root", installDir, fNotchStr.c_str());
159 else if(version == 4 && fUseAverageCRTemplate)
161 fname = Form(
"%s/share/AnitaAnalysisFramework/templates/averageA4CR_%s.txt", installDir, fNotchStr.c_str());
163 else if(version != 4 && fUseAverageCRTemplate)
165 fname = Form(
"%s/share/AnitaAnalysisFramework/templates/averageA3CR.txt", installDir);
167 else fname = Form(
"%s/share/AnitaAnalysisFramework/templates/crTmpltsA3.root", installDir);
169 if(!fUseAverageCRTemplate)
171 TFile *inFile = TFile::Open(fname.Data());
172 std::stringstream name;
174 for (
int i=0; i<numCRTemplates; i++) {
178 name <<
"disp" << wave;
179 TGraph *grTemplateRaw = (TGraph*)inFile->Get(name.str().c_str());
184 delete grTemplateRaw;
187 double xStart = grTemplateCut->GetX()[0];
188 for (
int pt=0; pt<grTemplateCut->GetN(); pt++) grTemplateCut->GetX()[pt] -= xStart;
193 delete grTemplateCut;
196 grTemplate->SetName(name.str().c_str());
202 theCRTemplates[i] = grTemplate;
203 theCRTemplateFFTs[i] = theTemplateFFT;
209 if(fUseAverageCRTemplate)
211 TGraph* grTemplateCut =
new TGraph(fname.Data());
214 delete grTemplateCut;
217 grTemplate->SetName(
"disp0");
221 theCRTemplates[0] = grTemplate;
222 theCRTemplateFFTs[0] = theTemplateFFT;
225 fname = Form(
"%s/share/AnitaAnalysisFramework/templates/crTmpltsA4_%s.root", installDir, fNotchStr.c_str());
226 TFile *inFile = TFile::Open(fname.Data());
227 std::stringstream name;
232 name <<
"disp" << wave;
233 TGraph *grTemplateRaw = (TGraph*)inFile->Get(name.str().c_str());
238 delete grTemplateRaw;
241 double xStart = grTemplateCut->GetX()[0];
242 for (
int pt=0; pt<grTemplateCut->GetN(); pt++) grTemplateCut->GetX()[pt] -= xStart;
247 delete grTemplateCut;
250 grTemplate->SetName(name.str().c_str());
254 theCRTemplates[1] = grTemplate;
255 theCRTemplateFFTs[1] = theTemplateFFT;
262 void AnitaTemplateMachine::getWaisTemplate(
int version) {
264 char* installDir = getenv(
"ANITA_UTIL_INSTALL_DIR");
265 std::stringstream name;
267 if(AnitaVersion::get() == 4) name << installDir <<
"/share/AnitaAnalysisFramework/templates/waisTemplateA4.root";
268 else name << installDir <<
"/share/AnitaAnalysisFramework/templates/waisTemplateA3.root";
269 TFile *inFile = TFile::Open(name.str().c_str());
270 TGraph *grTemplateRaw = (AnitaVersion::get() == 4) ? (TGraph*)inFile->Get(
"wais") : (TGraph*)inFile->Get(
"wais01TH");
275 TGraph *grTemplateCut = (AnitaVersion::get() == 4) ?
new TGraph(grTemplateRaw->GetN(), grTemplateRaw->GetX(), grTemplateRaw->GetY()) :
WindowingTools::windowCut(grTemplateRaw,
length);
277 delete grTemplateRaw;
280 double xStart = grTemplateCut->GetX()[0];
281 for (
int pt=0; pt<grTemplateCut->GetN(); pt++) grTemplateCut->GetX()[pt] -= xStart;
286 delete grTemplateCut;
289 grTemplate->SetName(
"templateWais");
294 theWaisTemplateFFT =
FFTtools::doFFT(grTemplate->GetN(),grTemplate->GetY());
295 theWaisTemplate = grTemplate;
304 std::cout <<
"Loading templates, length=" <<
length << std::endl;
305 std::string tempStr =
"";
309 while(i < payloadTimes.size())
311 if(evTime < payloadTimes.at(i))
break;
314 tempStr = notchConfigs.at(i);
317 if(tempStr.compare(fNotchStr) == 0)
return;
319 kTmpltsDeconv =
false;
323 getImpulseResponseTemplate(version);
324 getWaisTemplate(version);
325 getCRTemplates(version);
327 kTmpltsLoaded =
true;
335 if(kTmpltsDeconv)
return;
337 std::cout <<
"Deconvolving templates" << std::endl;
339 std::stringstream name;
342 std::cout <<
"Error in AnitaTemplateAnalyzer::deconvolveTemplates(): AnitaTemplateMachine says it has no templates!" << std::endl;
343 std::cout <<
" Try doing AnitaTemplateMachine::loadTemplates() at some point maybe? " << std::endl;
347 double dF = 1./(dT*
length);
349 int lengthFFT = (
length/2 + 1);
352 std::cout <<
"0" << std::endl;
354 for (
int i=0; i<lengthFFT; i++) {
357 std::cout <<
"1" << std::endl;
360 std::cout <<
"2" << std::endl;
361 theImpTemplate_deconv =
new TGraph(
length,theImpTemplate->GetX(),theImpTemplate_deconv_y);
362 theImpTemplate_deconv->SetName(
"deconvImp");
366 for (
int i=0; i<(
length/2 + 1); i++) {
367 theWaisTemplateFFT_deconv[i] = theWaisTemplateFFT[i];
371 theWaisTemplate_deconv =
new TGraph(
length,theWaisTemplate->GetX(),theWaisTemplate_deconv_y);
372 theWaisTemplate_deconv->SetName(
"deconvWais");
376 if(!fUseAverageCRTemplate)
378 for (
int cr = 0; cr<numCRTemplates; cr++ ) {
380 for (
int i=0; i<(
length/2 + 1); i++) {
381 theCRTemplateFFTs_deconv[cr][i] = theCRTemplateFFTs[cr][i];
385 theCRTemplates_deconv[cr] =
new TGraph(
length,theCRTemplates[cr]->GetX(),theCRTemplate_deconv_y);
387 name <<
"deconvCR" << cr;
388 theCRTemplates_deconv[cr]->SetName(name.str().c_str());
392 if(fUseAverageCRTemplate)
394 char* installDir = getenv(
"ANITA_UTIL_INSTALL_DIR");
395 TString crfname = Form(
"%s/share/AnitaAnalysisFramework/templates/deconvolvedCRA4average.txt", installDir);
397 TGraph* grTemplateCut =
new TGraph(crfname.Data());
399 delete grTemplateCut;
402 grTemplate->SetName(
"disp0");
404 if(fDoWindow) grTemplate = WindowingTools::windowWave(grTemplate, phl, 30, 30, 50, 50);
408 theCRTemplates_deconv[0] = grTemplate;
409 theCRTemplateFFTs_deconv[0] = theTemplateFFT;
412 kTmpltsDeconv =
true;
426 std::cout <<
"Error in AnitaTemplateAnalyzer::doTemplateAnalysis(): AnitaTemplateMachine says it has no templates!" << std::endl;
427 std::cout <<
" Try doing AnitaTemplateMachine::loadTemplates() at some point maybe? " << std::endl;
435 TGraph *coherent =
new TGraph(coherentAligned->GetN(),coherentAligned->GetX(),coherentAligned->GetY());
441 delete coherentResamp;
451 double maxValue,minValue,value,value_loc;
459 maxValue = TMath::MaxElement(
length,dCorr);
460 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
461 if (TMath::Max(maxValue,minValue) == maxValue) {
463 value_loc = TMath::LocMax(
length,dCorr);
468 value_loc = TMath::LocMin(
length,dCorr);
471 templateSummary->coherent[poli][dir].impulse = value;
472 templateSummary->coherent[poli][dir].impulse_loc = value_loc;
473 templateSummary->coherent[poli][dir].impulse_pol = value_pol;
482 dCorr = FFTtools::getCorrelationFromFFT(
length,theWaisTemplateFFT,coherentFFT);
483 maxValue = TMath::MaxElement(
length,dCorr);
484 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
485 if (TMath::Max(maxValue,minValue) == maxValue) {
487 value_loc = TMath::LocMax(
length,dCorr);
492 value_loc = TMath::LocMin(
length,dCorr);
495 templateSummary->coherent[poli][dir].wais = value;
496 templateSummary->coherent[poli][dir].wais_loc = value_loc;
497 templateSummary->coherent[poli][dir].wais_pol = value_pol;
504 if(!fUseAverageCRTemplate)
506 for (
int i=0; i<numCRTemplates; i++) {
508 dCorr = FFTtools::getCorrelationFromFFT(
length,theCRTemplateFFTs[i],coherentFFT);
509 maxValue = TMath::MaxElement(
length,dCorr);
510 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
511 if (TMath::Max(maxValue,minValue) == maxValue) {
513 value_loc = TMath::LocMax(
length,dCorr);
518 value_loc = TMath::LocMin(
length,dCorr);
521 templateSummary->coherent[poli][dir].cRay[i] = value;
522 templateSummary->coherent[poli][dir].cRay_loc[i] = value_loc;
523 templateSummary->coherent[poli][dir].cRay_pol[i] = value_pol;
528 if(fUseAverageCRTemplate && do_cr)
530 dCorr = FFTtools::getCorrelationFromFFT(
length,theCRTemplateFFTs[0],coherentFFT);
531 maxValue = TMath::MaxElement(
length,dCorr);
532 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
533 if (TMath::Max(maxValue,minValue) == maxValue) {
535 value_loc = TMath::LocMax(
length,dCorr);
540 value_loc = TMath::LocMin(
length,dCorr);
543 templateSummary->coherent[poli][dir].cRay[0] = value;
544 templateSummary->coherent[poli][dir].cRay_loc[0] = value_loc;
545 templateSummary->coherent[poli][dir].cRay_pol[0] = value_pol;
548 delete[] coherentFFT;
554 void AnitaTemplateMachine::doDeconvolvedTemplateAnalysis(
const AnalysisWaveform *waveform,
557 bool do_impulse,
bool do_wais,
bool do_cr) {
562 std::cout <<
"Error in AnitaTemplateAnalyzer::doTemplateAnalysis(): AnitaTemplateMachine says it has no templates!" << std::endl;
563 std::cout <<
" Try doing AnitaTemplateMachine::loadTemplates() at some point maybe? " << std::endl;
570 if (!deconvolvedAligned || deconvolvedAligned->GetN() == 0) {
573 TGraph *deconvolved =
new TGraph(deconvolvedAligned->GetN(),deconvolvedAligned->GetX(),deconvolvedAligned->GetY());
582 deconvolvedPad = WindowingTools::windowWave(deconvolvedPad, phl, 30, 30, 50, 50);
586 delete deconvolvedPad;
592 double maxValue,minValue,value,value_loc;
599 dCorr = FFTtools::getCorrelationFromFFT(
length,theImpTemplateFFT_deconv,deconvolvedFFT);
600 maxValue = TMath::MaxElement(
length,dCorr);
601 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
602 if (TMath::Max(maxValue,minValue) == maxValue) {
604 value_loc = TMath::LocMax(
length,dCorr);
609 value_loc = TMath::LocMin(
length,dCorr);
612 templateSummary->deconvolved[poli][dir].impulse = value;
613 templateSummary->deconvolved[poli][dir].impulse_loc = value_loc;
614 templateSummary->deconvolved[poli][dir].impulse_pol = value_pol;
622 dCorr = FFTtools::getCorrelationFromFFT(
length,theWaisTemplateFFT_deconv,deconvolvedFFT);
623 maxValue = TMath::MaxElement(
length,dCorr);
624 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
625 if (TMath::Max(maxValue,minValue) == maxValue) {
627 value_loc = TMath::LocMax(
length,dCorr);
632 value_loc = TMath::LocMin(
length,dCorr);
635 templateSummary->deconvolved[poli][dir].wais = value;
636 templateSummary->deconvolved[poli][dir].wais_loc = value_loc;
637 templateSummary->deconvolved[poli][dir].wais_pol = value_pol;
643 if(!fUseAverageCRTemplate)
645 for (
int i=0; i<numCRTemplates; i++) {
647 dCorr = FFTtools::getCorrelationFromFFT(
length,theCRTemplateFFTs_deconv[i],deconvolvedFFT);
648 maxValue = TMath::MaxElement(
length,dCorr);
649 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
650 if (TMath::Max(maxValue,minValue) == maxValue) {
652 value_loc = TMath::LocMax(
length,dCorr);
657 value_loc = TMath::LocMin(
length,dCorr);
660 templateSummary->deconvolved[poli][dir].cRay[i] = value;
661 templateSummary->deconvolved[poli][dir].cRay_loc[i] = value_loc;
662 templateSummary->deconvolved[poli][dir].cRay_pol[i] = value_pol;
668 if(fUseAverageCRTemplate && do_cr)
672 double normalization = 1./(normDeconvolved->GetRMS(2) * theCRTemplates_deconv[0]->GetRMS(2) * normDeconvolved->GetN()/TMath::Power(2,
int(TMath::Log2(normDeconvolved->GetN()))));
673 maxValue = normalization * TMath::MaxElement(gCorr->GetN(), gCorr->GetY());
674 minValue = normalization * TMath::Abs(TMath::MinElement(gCorr->GetN(), gCorr->GetY()));
675 if (TMath::Max(maxValue,minValue) == maxValue) {
677 value_loc = TMath::LocMax(gCorr->GetN(), gCorr->GetY());
682 value_loc = TMath::LocMin(gCorr->GetN(), gCorr->GetY());
690 dCorr = FFTtools::getCorrelationFromFFT(
length,theCRTemplateFFTs_deconv[0],deconvolvedFFT);
691 maxValue = TMath::MaxElement(
length,dCorr);
692 minValue = TMath::Abs(TMath::MinElement(
length,dCorr));
693 if (TMath::Max(maxValue,minValue) == maxValue) {
695 value_loc = TMath::LocMax(
length,dCorr);
700 value_loc = TMath::LocMin(
length,dCorr);
706 templateSummary->deconvolved[poli][dir].cRay[0] = value;
707 templateSummary->deconvolved[poli][dir].cRay_loc[0] = value_loc;
708 templateSummary->deconvolved[poli][dir].cRay_pol[0] = value_pol;
712 delete[] deconvolvedFFT;
713 delete normDeconvolved;
724 std::cout <<
"Error in AnitaTemplateAnalyzer::writeTemplatesToFile(): AnitaTemplateMachine says it is missing templates!" << std::endl;
725 std::cout <<
" Try doing AnitaTemplateMachine::loadTemplates() and deconvolveTempaltes() at some point maybe? " << std::endl;
730 theImpTemplate->Write();
731 theImpTemplate_deconv->Write();
732 theWaisTemplate->Write();
733 theWaisTemplate_deconv->Write();
734 for (
int i=0; i<numCRTemplates; i++) {
735 theCRTemplates[i]->Write();
736 theCRTemplates_deconv[i]->Write();
754 TGraph *outGraph = (TGraph*)inGraph->Clone();
758 for (
int pt=0; pt<outGraph->GetN(); pt++) waveSum += pow(outGraph->GetY()[pt],2);
759 for (
int pt=0; pt<outGraph->GetN(); pt++) outGraph->GetY()[pt] /= TMath::Sqrt(waveSum / (outGraph->GetN()/4));
766 double *FFTtools::getCorrelationFromFFT(
int length,
const FFTWComplex *theFFT1,
const FFTWComplex *theFFT2)
770 int newLength=(length/2)+1;
774 for(
int i=0;i<newLength;i++) {
775 double reFFT1=theFFT1[i].
re;
776 double imFFT1=theFFT1[i].
im;
777 double reFFT2=theFFT2[i].
re;
778 double imFFT2=theFFT2[i].
im;
781 tempStep[i].
re=(reFFT1*reFFT2+imFFT1*imFFT2)/
double(no2/2);
783 tempStep[i].
im=(imFFT1*reFFT2-reFFT1*imFFT2)/
double(no2/2);
806 const int beforeTime = 500;
807 const int wings = 10;
808 int afterTime = length - beforeTime - 2*wings;
810 return windowWave(inGraph,tempPeak,beforeTime,afterTime,wings,wings);
813 TGraph *WindowingTools::windowDispersed(TGraph *inGraph,
int &peakHilbertLoc) {
814 return windowWave(inGraph,peakHilbertLoc,
819 TGraph *WindowingTools::windowDispersed(TGraph *inGraph) {
822 return windowDispersed(inGraph,tempPeak);
826 TGraph *WindowingTools::windowEField(TGraph *inGraph,
int &peakHilbertLoc) {
827 return windowWave(inGraph,peakHilbertLoc,
832 TGraph *WindowingTools::windowEField(TGraph *inGraph) {
835 return windowEField(inGraph,tempPeak);
840 TGraph *WindowingTools::windowWave(TGraph *inGraph,
int &peakHilbertLoc,
841 const int upRampLoc,
const int downRampLoc,
842 const int upRampLen,
const int downRampLen) {
865 if (peakHilbertLoc == -1) {
867 peakHilbertLoc = TMath::LocMax(hilbert->GetN(),hilbert->GetY());
871 int startLoc = peakHilbertLoc - upRampLoc;
872 int stopLoc = peakHilbertLoc + downRampLoc;
874 if (stopLoc+downRampLen > inGraph->GetN()) {
875 if (debug) std::cout <<
"****";
876 int overrun = (stopLoc+downRampLen) - inGraph->GetN() + 1;
881 if (debug) std::cout <<
"inGraph->GetN()=" << inGraph->GetN() <<
" startLoc=" << startLoc <<
" stopLoc=" << stopLoc;
885 TGraph *outGraph =
new TGraph();
886 for (
int pt=0; pt<inGraph->GetN(); pt++) {
887 if (pt <= (startLoc-upRampLen)) {
889 if (debug) std::cout << pt <<
" - 1" << std::endl;
891 else if (pt > (startLoc-upRampLen) && pt <= startLoc ) {
892 int ptMod = pt - (startLoc-upRampLen);
893 double modValue = 0.5-(TMath::Cos(ptMod * ( TMath::Pi()/upRampLen ))/2.);
894 double value = modValue * inGraph->GetY()[pt];
895 outGraph->SetPoint(outGraph->GetN(),inGraph->GetX()[pt],value);
896 if (debug) std::cout << pt <<
" - 2 - " << ptMod <<
" - " << modValue << std::endl;
898 else if (pt > startLoc && pt <= stopLoc) {
899 outGraph->SetPoint(outGraph->GetN(),inGraph->GetX()[pt],inGraph->GetY()[pt]);
900 if (debug) std::cout << pt <<
" - 3" << std::endl;
902 else if (pt > stopLoc && pt <= (stopLoc+downRampLen)) {
903 double ptMod = pt - stopLoc;
904 double modValue = (1+TMath::Cos(ptMod*( TMath::Pi()/downRampLen ))/2.) - 0.5;
905 double value = modValue * inGraph->GetY()[pt];
906 outGraph->SetPoint(outGraph->GetN(),inGraph->GetX()[pt],value);
907 if (debug) std::cout << pt <<
" - 4 - " << ptMod <<
" - " << modValue << std::endl;
909 else if (pt > stopLoc+downRampLen) {
911 if (debug) std::cout << pt <<
" - 5" << std::endl;
915 if (debug) std::cout <<
" outGraph->GetN()=" << outGraph->GetN() << std::endl;
FFTWComplex * theImpTemplateFFT
double im
The imaginary part.
virtual ~AnitaTemplateSummary()
static const Int_t maxDirectionsPerPol
AnitaTemplateMachine(const int inLength=2048)
Default Constructor.
void loadTemplates(unsigned int evTime=0, int version=AnitaVersion::get())
void deconvolveTemplates(AnitaResponse::DeconvolutionMethod *deconv)
This is a wrapper class for a complex number.
void doTemplateAnalysis(const AnalysisWaveform *waveform, int poli, int dir, AnitaTemplateSummary *summary, bool do_impulse=true, bool do_wais=true, bool do_cr=true)
void writeTemplatesToFile(TFile *outFile)
AnitaTemplateSummary()
Default Constructor.