Diana Software
MLockinDenoising.cc
Go to the documentation of this file.
1 #include "MLockinDenoising.hh"
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 "QAverageVectorC.hh"
11 #include "QRealComplexFFT.hh"
12 #include "QBool.hh"
13 #include "QBaseType.hh"
14 #include <iostream>
15 
16 
17 
18 
20 
21 using namespace Diana;
22 
24 {
25  fPulseLabel = GetString("PulseLabel","DAQ@Pulse",false);
26  ev.Require<QHeader>("DAQ","Header");
27  ev.RequireByLabel<QPulse>(fPulseLabel);
28  ev.Require<QPulseInfo>("DAQ","PulseInfo");
29  fNoiseCoherenceOwner = GetString("NoiseCoherenceOwner","NoiseFrequencyCoherence",false);
30  fNoiseCoherenceInput = GetString("NoiseCoherenceInput","");
31  ev.Add<QPulse>("Pulse");
32  ev.Add<QBool>("IsDenoised");
33 
34 
35 }
36 
37 void MLockinDenoising::Do(QEvent& ev, const QEventList& neighbours)
38 {
39  const QHeader& hdr = ev.Get<QHeader>("DAQ","Header");
40  const QPulseInfo& pi= ev.Get<QPulseInfo>("DAQ","PulseInfo");
41  const QPulse& pulse = ev.GetByLabel<QPulse>(fPulseLabel);
42  ev.Get<QPulse>("Pulse") = pulse;
43  ev.Get<QBool>("IsDenoised") = false;
44 
45  const int chan = pi.GetChannelId();
46 
47 
48 
49  if(fMap.find(chan) == fMap.end()) {
50  fMap[chan].BlackSheep = false;
51  GlobalHandle<QInt> dHandle("Dataset");
52  GlobalData().Get("",&dHandle,"");
53  GlobalHandle<QAverageVectorC> Coherence_Handle("Coherence");
54  Coherence_Handle.SetDataset(dHandle.Get());
55  Coherence_Handle.SetChannel(chan);
56  GlobalData().Get(fNoiseCoherenceOwner,&Coherence_Handle,fNoiseCoherenceInput);
57  // try with an of run
58  if(!Coherence_Handle.IsValid()) {
59  Coherence_Handle.SetRun(hdr.GetRun());
60  Coherence_Handle.SetDataset(Q_INT_DEFAULT);
61  GlobalData().Get(fNoiseCoherenceOwner,&Coherence_Handle,fNoiseCoherenceInput);
62  }
63  if(!Coherence_Handle.IsValid()) {
64  Warn("%s. Channel %d will not be processed",Coherence_Handle.GetError().GetDescription().c_str(),chan);
65  fMap[chan].BlackSheep = true;
66  } else {
67  fMap[chan].fCoherence = Coherence_Handle.Get();
68  }
69  }
70 
71  ChannelInfo& chanInfo = fMap[chan];
72  if (chanInfo.BlackSheep) return;
73 
74  if(neighbours.Size() != 1) return;
75 
76  const QEvent& neigh = neighbours[0];
77 
78  const QPulseInfo& piB= neigh.Get<QPulseInfo>("DAQ","PulseInfo");
79  const int chanB = piB.GetChannelId();
80  if(chan != chanB) return;
81 
82  const QVector& samples= pulse.GetSamples();
83  const int size = samples.Size();
84  const QVector& samplesB= neigh.GetByLabel<QPulse>(fPulseLabel).GetSamples();
85  if(size != (int)samplesB.Size()) return;
86 
87  // check continuity
88  int run = hdr.GetRun();
90  GlobalData().Get("",&rHandle,"");
91  const QRunData& runData = rHandle.Get();
92  const QChannelRunData& chanRunData = runData.GetChannelRunData(chan);
93  const double samplPeriod = 1. / chanRunData.fSamplingFrequency*1e9;
94 
95  const QHeader& hdrB = neigh.Get<QHeader>("DAQ","Header");
96  Long64_t lastSampleB = size-piB.GetMasterSample().GetSampleIndex()-1;
97  lastSampleB += hdrB.GetTime().GetFromStartRunNs()/samplPeriod;
98  Long64_t firstSample = hdr.GetTime().GetFromStartRunNs()/samplPeriod-pi.GetMasterSample().GetSampleIndex();
99  if(firstSample-lastSampleB != 1) {
100  // Warn("Samples on event %d not contiguous (lastSampleB %lld, firstSample %lld",hdr.GetEventNumber(),lastSampleB,firstSample);
101  // return;
102  }
103 
104 
105  // Lockin denoising
106  QRealComplexFFT transformer(size);
107  QVectorC TransformedPulseB(size);
108  QVectorC TransformedPulse(size);
109 
110  int err = transformer.TransformToFreq(samplesB, TransformedPulseB);
111  if(err != 0) {
112  Error("QFFTRealComplex::TransformToFreq: pulse B");
113  return;
114  }
115 
116  err = transformer.TransformToFreq(samples, TransformedPulse);
117  if(err != 0) {
118  Error("QFFTRealComplex::TransformToFreq: pulse ");
119  return;
120  }
121 
122  for(int i =1; i < size; i++) {
123  TransformedPulse[i] -= TransformedPulseB[i]*chanInfo.fCoherence[i];
124  }
125 
126 
127  QVector output;
128  transformer.TransformFromFreq(TransformedPulse,output);
129 
130  ev.Get<QPulse>("Pulse").Clear();
131  ev.Get<QPulse>("Pulse").SetSamplesADC(output);
132  ev.Get<QPulse>("Pulse").Validate();
133  ev.Get<QBool>("IsDenoised") = true;
134 
135 
136 
137 }
138 
140 {
141  /* This method is called at the end of the event loop.
142  * Here you can:
143  *
144  * 1) Operate on the sequence execution parameters(see QBaseModule.hh).
145  *
146  * 2) Read/Write global data.
147  */
148 }
149 
err
Definition: CheckOF.C:114
QRunDataHandle rHandle(753)
QChannelRunData chanRunData
#define Q_INT_DEFAULT
Definition: QDiana.hh:26
#define REGISTER_MODULE(clazz)
Definition: QDriver.hh:133
one-line description of your module
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.
void Done()
Done method.
bool wrapped into a QObject
Definition: QBool.hh:17
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
void Add(WriteHandle< Q > &handle)
Add a QObject to the event.
Definition: QEvent.hh:193
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.
virtual int TransformFromFreq(const QVectorC &FT, QVector &spectrum, bool compress=false)
transform from the frequencies to the times
virtual int TransformToFreq(const QVector &data, QVectorC &FFT, bool compress=false)
transform from the times to the frequencies
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...