Diana Software
MNoiseCrossFrequencyCovariance.cc
Go to the documentation of this file.
2 #include "QEvent.hh"
3 #include "QEventList.hh"
4 #include "QRealComplexFFT.hh"
5 #include "QRunData.hh"
6 #include "QRawEvent.hh"
7 #include "QGlobalHandle.hh"
8 #include "QPulseInfo.hh"
9 #include "QGlobalDataManager.hh"
10 #include "QRunDataHandle.hh"
11 #include "QStdVector.hh"
12 #include "QBaseType.hh"
13 #include "QVectorView.hh"
14 #include "QAverageNoiseHandle.hh"
15 #include <iostream>
16 
17 
18 
20 
21 using namespace Diana;
22 
24 {
25  fOutput = GetString("Output",Q_STRING_DEFAULT);
26  std::string window = GetString("WindowType","welch");
27  fWindowType = QFFT::StrToWindowType(window);
28  if(fWindowType == QFFT::WT_None) {
29  Warn("Window type not defined, using rectangular window");
30  fWindowType = QFFT::WT_Rect;
31  }
32  else {
33  Info("Window type: %d (%s)", fWindowType,window.c_str());
34  }
35  if(fWindowType != QFFT::WT_Rect)
36  fCoherentGain = GetInt("WindowCoherentGain",2);
37  else fCoherentGain = 2;
38  fUseMainEvent = GetBool("UseMainEvent",false);
39  fValidityKind = GetString("ValidityKind","dataset",false);
40  if(fValidityKind != "dataset" && fValidityKind != "run") {
41  Panic("ValidityKind must be set to dataset or run");
42  }
43 
44  fValidityStart = GetInt("ValidityStart",Q_INT_DEFAULT,false);
45  fValidityEnd = GetInt("ValidityEnd",Q_INT_DEFAULT,false);
46  fSingleChannel = GetBool("SingleChannel",false,false);
47  fPulseLabel = GetString("PulseLabel","DAQ@Pulse",false);
48  ev.Require<QHeader>("DAQ","Header");
49  ev.RequireByLabel<QPulse>(fPulseLabel);
50  ev.Require<QPulseInfo>("DAQ","PulseInfo");
51 
52  fSRuns.clear();
53  fCurrentRun = -1;
54 }
55 
56 
57 
59 {
60 
61  QEventList allchans;
62  if(fUseMainEvent) allchans.Push(&ev);
63  for(size_t ch = 0; ch < neighbours.Size(); ch++) {
64  allchans.Push(&neighbours[ch]);
65  }
66  int run = ev.Get<QHeader>("DAQ","Header").GetRun();
67  if (run != fCurrentRun) {
68  fCurrentRun = run;
69  fSRuns.push_back(fCurrentRun);
70  }
71 
72 
73  if(fTimeLength == 0) {
74  fTimeLength = ev.GetByLabel<QPulse>(fPulseLabel).GetSamples().Size()/2;
75  if(fTimeLength %2 != 0) {
76  fTimeLength -= 1;
77  Info("Window size reduced to %d",fTimeLength);
78  }
79  fTransformer = new QRealComplexFFT(fTimeLength);
80  fTransformer->SetWindowType(fWindowType, fCoherentGain);
81  }
82 
83  QVectorC TransformedPulseIB(fTimeLength);
84  QVectorC TransformedPulseIF(fTimeLength);
85  QVectorC TransformedPulseJB(fTimeLength);
86  QVectorC TransformedPulseJF(fTimeLength);
87  for(size_t i = 0; i < allchans.Size(); i++) {
88  const int chanI = allchans[i].Get<QPulseInfo>("DAQ","PulseInfo").GetChannelId();
89  const QVector& samplesIorig = allchans[i].GetByLabel<QPulse>(fPulseLabel).GetSamples();
90 
91  QVectorConstView samplesIviewB(samplesIorig,0,fTimeLength);
92  const QVector& samplesIB = samplesIviewB.GetVector();
93  int err = Transform(samplesIB, TransformedPulseIB);
94  if(err != 0) {
95  Error("QFFTRealComplex::TransformToFreq: pulse I");
96  return;
97  }
98  TransformedPulseIB[0].SetRe(0);
99  TransformedPulseIB[0].SetIm(0);
100 
101  QVectorConstView samplesIviewF(samplesIorig,fTimeLength,fTimeLength);
102  const QVector& samplesIF = samplesIviewF.GetVector();
103  err = Transform(samplesIF, TransformedPulseIF);
104  if(err != 0) {
105  Error("QFFTRealComplex::TransformToFreq: pulse I");
106  return;
107  }
108  TransformedPulseIF[0].SetRe(0);
109  TransformedPulseIF[0].SetIm(0);
110 
111  for(size_t j = i ; j < (fSingleChannel ? i+1: allchans.Size()); j++) {
112  const int chanJ = allchans[j].Get<QPulseInfo>("DAQ","PulseInfo").GetChannelId();
113  const QVector& samplesJorig = allchans[j].GetByLabel<QPulse>(fPulseLabel).GetSamples();
114  QVectorConstView samplesJviewF(samplesJorig,fTimeLength,fTimeLength);
115  const QVector& samplesJF = samplesJviewF.GetVector();
116  err = Transform(samplesJF, TransformedPulseJF);
117  if(err != 0) {
118  Error("QFFTRealComplex::TransformToFreq: pulse J");
119  return;
120  }
121  TransformedPulseJF[0].SetRe(0);
122  TransformedPulseJF[0].SetIm(0);
123 
124  QVectorConstView samplesJviewB(samplesJorig,0,fTimeLength);
125  const QVector& samplesJB = samplesJviewB.GetVector();
126 
127  err = Transform(samplesJB, TransformedPulseJB);
128  if(err != 0) {
129  Error("QFFTRealComplex::TransformToFreq: pulse J");
130  return;
131  }
132  TransformedPulseJB[0].SetRe(0);
133  TransformedPulseJB[0].SetIm(0);
134 
135  ChannelInfo& chInfo = fRosetta[chanI][chanJ];
136  if(chInfo.fNevents == 0) {
137  chInfo.fCovarianceFreq.Resize(fTimeLength,fTimeLength);
138  chInfo.fCovarianceFreq.Initialize(0,0);
139  if(chanI == chanJ) {
140  fChannelList.push_back(chanJ);
141  }
142  }
143  QMatrixC element,phases;
144  element = TransformedPulseIF*TransformedPulseJF.H();
145  phases = TransformedPulseIB*TransformedPulseJB.H();
146  QMatrixC magnitudes(phases.Magnitude());
147  phases.Div(magnitudes);
148  phases.Conjugate();
149  element.Mult(phases);
150  chInfo.fCovarianceFreq += element;
151  if(chanI == chanJ) {
152  if(chInfo.fNevents == 0) {
153  chInfo.fPower.Resize(fTimeLength);
154  chInfo.fPower.Initialize(0);
155  chInfo.fCoherence.Resize(fTimeLength);
156  chInfo.fCoherence.Initialize(0);
157  }
158  chInfo.fPower += TransformedPulseIF.GetModulusSquare();
159  chInfo.fCoherence += TransformedPulseIB.Mult(TransformedPulseIF.Conj());
160  }
161  chInfo.fNevents++;
162  }
163  }
164 
165 }
166 
168 {
169  if(fTransformer != 0) delete fTransformer;
170  std::stringstream str;
171  str<<"Channel List: ";
172  for(size_t i=0;i<fChannelList.size();i++) {
173  str<<fChannelList[i]<<" ";
174  }
175  str<<std::endl;
176  Debug(str.str().c_str());
177 
178 
179  std::map<int,std::map<int,ChannelInfo> >::const_iterator iterI = fRosetta.begin();
180  while(iterI != fRosetta.end() ) {
181  std::map<int,ChannelInfo>::const_iterator iterJ = iterI->second.begin();
182  while(iterJ != iterI->second.end() ) {
183  int chI =iterI->first;
184  int chJ =iterJ->first;
185  const ChannelInfo& chInfo=iterJ->second;
186  QMatrixC cov = chInfo.fCovarianceFreq/chInfo.fNevents;
187  GlobalHandle<QMatrixC> covHandle("CovarianceFreq");
188 
189  int dataset = -1;
190  if(fValidityKind=="dataset") {
191  GlobalHandle<QInt> dHandle("Dataset");
192  GlobalData().Get("",&dHandle,"");
193  dataset = dHandle.Get();
194  covHandle.SetDataset(dataset);
195  } else if (fValidityKind=="run") {
196  if (fSRuns.size()==1){
197  covHandle.SetRun(fSRuns[0]);
198  }
199  else Panic("ValidityKind set to run is not permitted when reading multiple runs.");
200  }
201  else Panic("ValidityKind can be set only to run or dataset. Check cfg.");
202  covHandle.SetChannel(chI);
203  covHandle.SetChannel2(chJ);
204  covHandle.Set(cov);
205  GlobalData().Set(&covHandle,fOutput);
206 
207  if(chI == chJ) {
208  QAverageNoiseHandle nfh(chI,"NoiseAvgPS");
209  nfh.Set(chInfo.fPower / chInfo.fNevents);
210  GlobalHandle<QVectorC> cohHandle("Coherence");
211  cohHandle.SetChannel(chI);
212  cohHandle.Set(chInfo.fCoherence / chInfo.fNevents);
213  if(fValidityKind =="run") {
214  nfh.SetRun(fSRuns[0]);
215  cohHandle.SetRun(fSRuns[0]);
216  } else {
217  nfh.SetDataset(dataset);
218  cohHandle.SetDataset(dataset);
219  }
220  GlobalData().Set(&nfh, fOutput);
221  GlobalData().Set(&cohHandle,fOutput);
222  }
223  iterJ++;
224  }
225  iterI++;
226  }
227 
228 }
229 
230 int MNoiseCrossFrequencyCovariance::Transform(const Diana::QVector& samples, Diana::QVectorC& out)
231 {
232  const size_t size = samples.Size();
233  QVector zeroMeanPulse(size), meanPulse(size);
234  double mean = samples.Sum(size)/size;
235  meanPulse.Initialize(mean);
236  zeroMeanPulse = samples;
237  zeroMeanPulse -= meanPulse;
238  return fTransformer->TransformToFreq(zeroMeanPulse, out);
239 }
240 
err
Definition: CheckOF.C:114
#define Q_STRING_DEFAULT
Definition: QDiana.hh:38
#define Q_INT_DEFAULT
Definition: QDiana.hh:26
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
compute correlation between different frequencies
int Transform(const Diana::QVector &in, Diana::QVectorC &out)
void Init(Diana::QEvent &ev)
Init method.
void Do(Diana::QEvent &ev, const Diana::QEventList &neighbours)
Do method. Declare and implement only one of the two versions.
global handle for average noise power spectra
list of references to const QEvent (s)
Definition: QEventList.hh:21
void Push(const QEvent *obj)
add a QEvent
Definition: QEventList.cc:17
size_t Size() const
number of QEvent (s)
Definition: QEventList.hh:36
diana event
Definition: QEvent.hh:46
const Q & GetByLabel(const QEventLabel &label) const
Get a QObject in read mode by label.
Definition: QEvent.hh:135
void RequireByLabel(const QEventLabel &label) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:242
void Require(const std::string &owner, const std::string &name) const
notify the QEvent that we need a QObject, if not found an exception is thrown
Definition: QEvent.hh:232
void Get(const char *owner, ReadHandle< Q > &handle) const
Get a QObject Handle in read mode.
Definition: QEvent.hh:74
static WindowType StrToWindowType(const std::string &winName)
Convert string to window type.
Definition: QFFT.cc:64
@ WT_Rect
Definition: QFFT.hh:33
@ WT_None
Definition: QFFT.hh:31
Raw event: basic information like run number and time.
Definition: QHeader.hh:16
Interface for complex matrices in Diana analysis.
Definition: QMatrixC.hh:27
QMatrix Magnitude() const
return magnitude of each element
Definition: QMatrixC.cc:436
const QMatrixC & Mult(const QMatrixC &mat)
Multiply element by element.
Definition: QMatrixC.cc:334
const QMatrixC & H()
compute hermitian conjugate
Definition: QMatrixC.cc:410
const QMatrixC & Div(const QMatrixC &mat)
Divide element by element.
Definition: QMatrixC.cc:342
const QMatrixC & Conjugate()
conjugate this matrix
Definition: QMatrixC.cc:421
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
Raw event: sampled waveform.
Definition: QPulse.hh:22
Wrapper for a specific QRealComplexFFT algorithm class.
QVectorView for const QVector.
Definition: QVectorView.hh:52
const QVector & GetVector() const
Get subview QVector.
Definition: QVectorView.hh:66
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...