Diana Software
MNoiseFrequencyCoherence.cc
Go to the documentation of this file.
2 #include "QEvent.hh"
3 #include "QEventList.hh"
4 #include "QRunData.hh"
5 #include "QRawEvent.hh"
6 #include "QGlobalHandle.hh"
7 #include "QPulseInfo.hh"
8 #include "QGlobalDataManager.hh"
9 #include "QRunDataHandle.hh"
10 #include "QBaseType.hh"
11 #include <iostream>
12 
13 
14 
16 
17 using namespace Diana;
18 
20 {
21  fOutput = GetString("Output",Q_STRING_DEFAULT);
22  std::string window = GetString("WindowType","welch");
23  fWindowType = QFFT::StrToWindowType(window);
24  if(fWindowType == QFFT::WT_None) {
25  Warn("Window type not defined, using rectangular window");
26  fWindowType = QFFT::WT_Rect;
27  }
28  else {
29  Info("Window type: %d (%s)", fWindowType,window.c_str());
30  }
31  if(fWindowType != QFFT::WT_Rect)
32  fCoherentGain = GetInt("WindowCoherentGain",2);
33  else fCoherentGain = 2;
34  fValidityKind = GetString("ValidityKind","dataset",false);
35  if(fValidityKind != "dataset" && fValidityKind != "run") {
36  Panic("ValidityKind must be set to dataset or run");
37  }
38 
39  fValidityStart = GetInt("ValidityStart",Q_INT_DEFAULT,false);
40  fValidityEnd = GetInt("ValidityEnd",Q_INT_DEFAULT,false);
41  fPulseLabel = GetString("PulseLabel","DAQ@Pulse",false);
42  ev.Require<QHeader>("DAQ","Header");
43  ev.RequireByLabel<QPulse>(fPulseLabel);
44  ev.Require<QPulseInfo>("DAQ","PulseInfo");
45 
46  fSRuns.clear();
47  fCurrentRun = -1;
48  fChannelInfos.clear();
49 }
50 
51 
52 
53 void MNoiseFrequencyCoherence::Do(QEvent& ev, const QEventList& neighbours)
54 {
55  if(neighbours.Size() != 1) return;
56  const QEvent& neigh = neighbours[0];
57 
58  const QPulseInfo& pi= ev.Get<QPulseInfo>("DAQ","PulseInfo");
59  const QPulseInfo& piB= neigh.Get<QPulseInfo>("DAQ","PulseInfo");
60 
61  const int chan = pi.GetChannelId();
62  const int chanB = piB.GetChannelId();
63  if(chan != chanB) return;
64 
65  const QVector& samples= ev.GetByLabel<QPulse>(fPulseLabel).GetSamples();
66  const int size = samples.Size();
67  const QVector& samplesB= neigh.GetByLabel<QPulse>(fPulseLabel).GetSamples();
68  if(size != (int)samplesB.Size()) return;
69 
70  // check continuity
71  const QHeader& hdr = ev.Get<QHeader>("DAQ","Header");
72  int run = hdr.GetRun();
74  GlobalData().Get("",&rHandle,"");
75  const QRunData& runData = rHandle.Get();
76  const QChannelRunData& chanRunData = runData.GetChannelRunData(chan);
77  const double samplPeriod = 1. / chanRunData.fSamplingFrequency*1e9;
78 
79  const QHeader& hdrB = neigh.Get<QHeader>("DAQ","Header");
80  Long64_t lastSampleB = size-piB.GetMasterSample().GetSampleIndex()-1;
81  lastSampleB += hdrB.GetTime().GetFromStartRunNs()/samplPeriod;
82  Long64_t firstSample = hdr.GetTime().GetFromStartRunNs()/samplPeriod-pi.GetMasterSample().GetSampleIndex();
83  if(firstSample-lastSampleB != 1) {
84  // Warn("Samples on event %d not contiguous (lastSampleB %lld, firstSample %lld",hdr.GetEventNumber(),lastSampleB,firstSample);
85  // return;
86  }
87 
88 
89  if (run != fCurrentRun) {
90  fCurrentRun = run;
91  fSRuns.push_back(fCurrentRun);
92  }
93 
94  if(fChannelInfos.find(chan) == fChannelInfos.end()) {
95  ChannelInfo& chInfo = fChannelInfos[chan];
96  chInfo.fNumEvents = 0;
97  chInfo.fCovariance.Resize(size);
98  chInfo.fPowerB.Resize(size);
99  chInfo.fPower.Resize(size);
100  chInfo.fCovariance.Initialize(0,0);
101  chInfo.fPowerB.Initialize(0);
102  chInfo.fPower.Initialize(0);
103  chInfo.fTransformer = new QRealComplexFFT(size);
104  chInfo.fTransformer->SetWindowType(fWindowType, fCoherentGain);
105  }
106  ChannelInfo& chInfo = fChannelInfos[chan];
107 
108 
109  QVectorC TransformedPulseB(size);
110  QVectorC TransformedPulse(size);
111  int err = Transform(chInfo.fTransformer,samplesB, TransformedPulseB);
112  if(err != 0) {
113  Error("QFFTRealComplex::TransformToFreq: pulse B");
114  return;
115  }
116  TransformedPulseB[0].SetRe(0);
117  TransformedPulseB[0].SetIm(0);
118  err = Transform(chInfo.fTransformer,samples, TransformedPulse);
119  if(err != 0) {
120  Error("QFFTRealComplex::TransformToFreq: pulse ");
121  return;
122  }
123  TransformedPulse[0].SetRe(0);
124  TransformedPulse[0].SetIm(0);
125 
126 
127  chInfo.fPowerB += TransformedPulseB.GetModulusSquare();
128  chInfo.fPower += TransformedPulse.GetModulusSquare();
129  chInfo.fCovariance += TransformedPulse.Mult(TransformedPulseB.Conj());
130  chInfo.fNumEvents++;
131 
132 }
133 
135 {
136  std::map<int,ChannelInfo>::iterator iter = fChannelInfos.begin();
137  while (iter != fChannelInfos.end() ) {
138  ChannelInfo& chanInfo = iter->second;
139  if(chanInfo.fNumEvents == 0) {
140  iter++;
141  continue;
142  }
143 
144  int channel = iter->first;
145  chanInfo.fPower /= chanInfo.fNumEvents;
146  chanInfo.fPowerB /= chanInfo.fNumEvents;
147  chanInfo.fCovariance /= chanInfo.fNumEvents;
148 
149  QVector fAvgPower(chanInfo.fPower.Size());
150  for(size_t f = 0; f < chanInfo.fPower.Size(); f++) {
151  fAvgPower[f] = sqrt(chanInfo.fPower[f]*chanInfo.fPowerB[f]);
152  }
153 
154  QAverageVectorC coherence;
155  coherence = chanInfo.fCovariance.Div(fAvgPower);
156  coherence[0].Set(0,0);
157  coherence.SetSourceRuns(fSRuns);
158  coherence.SetNumEvents(chanInfo.fNumEvents);
159  //std::cout<<"channel: "<<channel<<" coh: "<<coherence<<std::endl;
160  // Fill Output
161 
162  GlobalHandle<QAverageVectorC> handle("Coherence");
163  handle.SetChannel(channel);
164  if(fValidityKind=="dataset"){
165  GlobalHandle<QInt> dHandle("Dataset");
166  GlobalData().Get("",&dHandle,"");
167  int dataset = dHandle.Get();
168  handle.SetDataset(dataset);
169  }
170  else if (fValidityKind=="run"){
171  if (fSRuns.size()==1){
172  handle.SetRun(fSRuns[0]);
173  }
174  else Error("ValidityKind set to run is not permitted when reading multiple runs.");
175  }
176  else Error("ValidityKind can be set only to run or dataset. Check cfg.");
177 
178  handle.Set(coherence);
179  GlobalData().Set(&handle,fOutput);
180  iter++;
181 
182  }
183 }
184 
185 int MNoiseFrequencyCoherence::Transform(Diana::QRealComplexFFT* transformer, const Diana::QVector& samples, Diana::QVectorC& out)
186 {
187  const size_t size = samples.Size();
188  QVector zeroMeanPulse(size), meanPulse(size);
189  double mean = samples.Sum(size)/size;
190  meanPulse.Initialize(mean);
191  zeroMeanPulse = samples;
192  zeroMeanPulse -= meanPulse;
193  return transformer->TransformToFreq(zeroMeanPulse, out);
194 }
195 
196 
197 
err
Definition: CheckOF.C:114
QRunDataHandle rHandle(753)
const int channel
QChannelRunData chanRunData
#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
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.
int Transform(Diana::QRealComplexFFT *transformer, const Diana::QVector &in, Diana::QVectorC &out)
average vector object
void SetNumEvents(int nEvents)
Set the number of events in the average.
void SetSourceRuns(const std::vector< int > &sourceRuns)
Set the list of source runs.
basic channel and run based info. Used in the QRunData object.
double fSamplingFrequency
sampling frequency in Hz
list of references to const QEvent (s)
Definition: QEventList.hh:21
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
const Diana::QTime & GetTime() const
get time
Definition: QHeader.hh:28
int GetRun() const
destructor
Definition: QHeader.hh:22
Raw event: bolometer channel, trigger positions and types.
Definition: QPulseInfo.hh:18
const QSampleInfo & GetMasterSample() const
Get MasterSample.
Definition: QPulseInfo.hh:26
const int & GetChannelId() const
Get ChannelId.
Definition: QPulseInfo.hh:22
Raw event: sampled waveform.
Definition: QPulse.hh:22
Wrapper for a specific QRealComplexFFT algorithm class.
global handle for QRunData
Basic run based info.
Definition: QRunData.hh:20
const QChannelRunData & GetChannelRunData(const int channel) const
get channel based run data quantities
Definition: QRunData.cc:339
Int_t GetSampleIndex() const
Get SampleIndex from the beginning of the waveform.
Definition: QSampleInfo.hh:51
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...