Diana Software
MNoiseCrossPowerSpectrum.cc
Go to the documentation of this file.
2 #include "QEvent.hh"
3 #include "QEventList.hh"
4 #include "QRealComplexFFT.hh"
5 #include "QCountPulsesData.hh"
6 #include "QBaselineData.hh"
7 #include "QRunData.hh"
8 #include "QRawEvent.hh"
9 #include "QGlobalHandle.hh"
10 #include "QPulseInfo.hh"
11 #include "QGlobalDataManager.hh"
12 #include "QRunDataHandle.hh"
13 #include "QStdVector.hh"
14 #include "QBaseType.hh"
15 #include "QStdVector.hh"
16 #include <iostream>
17 
18 using namespace Diana;
19 
21 
22 
24 {
25  // called before event loop
26  fTimeLength = 0;
27  fTransformer = 0;
28  fOutput = GetString("Output",Q_STRING_DEFAULT);
29  std::string window = GetString("WindowType","welch");
30  fWindowType = QFFT::StrToWindowType(window);
31  if(fWindowType == QFFT::WT_None) {
32  Warn("Window type not defined, using rectangular window");
33  fWindowType = QFFT::WT_Rect;
34  }
35  else {
36  Info("Window type: %d (%s)", fWindowType,window.c_str());
37  }
38  if(fWindowType != QFFT::WT_Rect)
39  fCoherentGain = GetInt("WindowCoherentGain",2);
40  else fCoherentGain = 2;
41  fUseMainEvent = GetBool("UseMainEvent",false);
42  fValidityKind = GetString("ValidityKind","dataset",false);
43  if(fValidityKind != "dataset" && fValidityKind != "run") {
44  Panic("ValidityKind must be set to dataset or run");
45  }
46 
47  fValidityStart = GetInt("ValidityStart",Q_INT_DEFAULT,false);
48  fValidityEnd = GetInt("ValidityEnd",Q_INT_DEFAULT,false);
49 
50  std::vector<std::string> pulseLabels;
51 
52  pulseLabels.push_back("DAQ@Pulse");
53  fPulseLabels = GetVectorString("PulseLabels",pulseLabels,false);
54  if(fPulseLabels.empty()) Panic("PulseLabels is empty");
55 
56  ev.Require<QHeader>("DAQ","Header");
57  ev.Require<QPulseInfo>("DAQ","PulseInfo");
58  for(size_t i = 0; i < fPulseLabels.size(); i++) {
59  ev.RequireByLabel<QPulse>(fPulseLabels[i]);
60  }
61 
62 
63  fSRuns.clear();
64  fCurrentRun = -1;
65 
66 }
67 
68 void MNoiseCrossPowerSpectrum::Do(QEvent& ev, const QEventList& neighbours)
69 {
70  // called event by event
71  QEventList allchans;
72  if(fUseMainEvent) allchans.Push(&ev);
73  for(size_t ch = 0; ch < neighbours.Size(); ch++) {
74  allchans.Push(&neighbours[ch]);
75  }
76  int run = ev.Get<QHeader>("DAQ","Header").GetRun();
77  if (run != fCurrentRun) {
78  fCurrentRun = run;
79  fSRuns.push_back(fCurrentRun);
80  }
81 
82 
83  if(fTimeLength == 0) {
84  fTimeLength = ev.GetByLabel<QPulse>(fPulseLabels[0]).GetSamples().Size();
85  fTransformer = new QRealComplexFFT(fTimeLength);
86  fTransformer->SetWindowType(fWindowType, fCoherentGain);
87  fCov.SetNFreq(fTimeLength);
88  }
89 
90  QVectorC TransformedPulseI(fTimeLength);
91  QVectorC TransformedPulseJ(fTimeLength);
92  QVector zeroMeanPulse(fTimeLength), meanPulse(fTimeLength);
93  for(size_t i = 0; i < allchans.Size(); i++) {
94  int chanI = allchans[i].Get<QPulseInfo>("DAQ","PulseInfo").GetChannelId();
95  for(size_t li = 0; li < fPulseLabels.size(); li++) {
96  if(li>0) chanI += 1000;
97  const QVector& samplesI = allchans[i].GetByLabel<QPulse>(fPulseLabels[li]).GetSamples();
98  double mean = samplesI.Sum(samplesI.Size())/samplesI.Size();
99  meanPulse.Initialize(mean);
100  zeroMeanPulse = samplesI;
101  zeroMeanPulse -= meanPulse;
102  int err = fTransformer->TransformToFreq(zeroMeanPulse, TransformedPulseI);
103  if(err != 0) {
104  Error("QFFTRealComplex::TransformToFreq: pulse I");
105  return;
106  }
107  TransformedPulseI[0].SetRe(0);
108  TransformedPulseI[0].SetIm(0);
109  for(size_t j = i ; j < allchans.Size(); j++) {
110  int chanJ = allchans[j].Get<QPulseInfo>("DAQ","PulseInfo").GetChannelId();
111  for(size_t lj = 0; lj < fPulseLabels.size(); lj++) {
112  if(lj>0) chanJ += 1000;
113  const QVector& samplesJ = allchans[j].GetByLabel<QPulse>(fPulseLabels[lj]).GetSamples();
114  mean = samplesJ.Sum(samplesJ.Size())/samplesJ.Size();
115  meanPulse.Initialize(mean);
116  zeroMeanPulse = samplesJ;
117  zeroMeanPulse -= meanPulse;
118 
119  int err = fTransformer->TransformToFreq(zeroMeanPulse, TransformedPulseJ);
120  if(err != 0) {
121  Error("QFFTRealComplex::TransformToFreq: pulse J");
122  return;
123  }
124  TransformedPulseJ[0].SetRe(0);
125  TransformedPulseJ[0].SetIm(0);
126 
127  ChannelInfo& chInfo = fRosetta[chanI][chanJ];
128  if(chInfo.fCovariance.Size() == 0) {
129  chInfo.fNevents = 0;
130  chInfo.fCovariance.Resize(fTimeLength);
131  chInfo.fCovariance.Initialize(0,0);
132  if(chanI == chanJ) {
133  fChannelList.push_back(chanJ);
134  }
135  }
136  ChannelInfo& chInfoT = fRosetta[chanJ][chanI];
137  if(chInfoT.fCovariance.Size() == 0) {
138  chInfoT.fNevents = 0;
139  chInfoT.fCovariance.Resize(fTimeLength);
140  chInfoT.fCovariance.Initialize(0,0);
141  }
142  QVectorC element = TransformedPulseI.Mult(TransformedPulseJ.Conj());
143  chInfo.fCovariance += element;
144  chInfo.fNevents++;
145 
146  chInfoT.fCovariance += element.Conj();
147  chInfoT.fNevents++;
148  }
149  }
150  }
151  }
152 
153 }
154 
156 {
157  std::stringstream str;
158  str<<"Channel List: ";
159  for(size_t i=0;i<fChannelList.size();i++) {
160  str<<fChannelList[i]<<" ";
161  }
162  str<<std::endl;
163  Debug(str.str().c_str());
164 
165  fCov.SetChannels(fChannelList);
166 
167 
168  std::map<int,std::map<int,ChannelInfo> >::const_iterator iterI = fRosetta.begin();
169  while(iterI != fRosetta.end() ) {
170  std::map<int,ChannelInfo>::const_iterator iterJ = iterI->second.begin();
171  while(iterJ != iterI->second.end() ) {
172  int chI =iterI->first;
173  int chJ =iterJ->first;
174  const ChannelInfo& chInfo=iterJ->second;
175  QVectorC vec = chInfo.fCovariance/chInfo.fNevents;
176  fCov.SetCovariance(chI,chJ,vec);
177  if(chI == chJ) {
178  fCov.SetNEvents(chI,chJ,chInfo.fNevents/2);
179  } else {
180  fCov.SetNEvents(chI,chJ,chInfo.fNevents);
181  }
182  iterJ++;
183  }
184  iterI++;
185  }
186 
187  GlobalHandle<QChannelCovariance> CovSaver("Covariance");
188  CovSaver.Set(fCov);
189  GlobalHandle<QStdVector<std::string> > LabelsSaver("PulseLabels");
190  LabelsSaver.Set(fPulseLabels);
191  GlobalHandle<QInt> StepSaver("PulseStep");
192  StepSaver.Set(1000);
193  if(fValidityKind=="dataset"){
194  GlobalHandle<QInt> dHandle("Dataset");
195  GlobalData().Get("",&dHandle,"");
196  int dataset = dHandle.Get();
197  CovSaver.SetDataset(dataset);
198  LabelsSaver.SetDataset(dataset);
199  StepSaver.SetDataset(dataset);
200  }
201  else if (fValidityKind=="run"){
202  if (fSRuns.size()==1){
203  CovSaver.SetRun(fSRuns[0]);
204  CovSaver.SetRun(fSRuns[0]);
205  LabelsSaver.SetRun(fSRuns[0]);
206  StepSaver.SetRun(fSRuns[0]);
207  }
208  else Error("ValidityKind set to run is not permitted when reading multiple runs.");
209  }
210  else Error("ValidityKind can be set only to run or dataset. Check cfg.");
211 
212 
213 
214 
215  GlobalData().Set(&CovSaver,fOutput);
216  GlobalData().Set(&StepSaver,fOutput);
217  GlobalData().Set(&LabelsSaver,fOutput);
218  if(fTransformer != 0) delete fTransformer;
219 }
220 
err
Definition: CheckOF.C:114
QVector vec(3)
#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 cross correlated noise power spectrum on different channels. * This module needs side pulses ...
void Do(Diana::QEvent &ev, const Diana::QEventList &neighbours)
Do method.
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 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
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.
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...