Diana Software
QAverageNoiseHandle.cc
Go to the documentation of this file.
1 #include "QAverageNoiseHandle.hh"
2 #include "QAverageVector.hh"
3 #include "QDianaDb.hh"
4 
5 using namespace Diana;
6 
8 {
10 
11  QAverageVector av;
12  av.Clear();
13 
14  std::string bareOwner, extraLabel;
15  GetLabel().GetBareOwnerAndExtraLabel(bareOwner,extraLabel);
16  if (extraLabel=="") extraLabel="None";
17 
18  const int dataset = GetDataset();
19  const int run = GetRun();
20 
21  // set validity_kind
22  std::string validity_kind;
23  int validity_start = Q_INT_DEFAULT, validity_end = Q_INT_DEFAULT;
24 
25  if (dataset!=Q_INT_DEFAULT)
26  //validity kind set to dataset
27  {
28  validity_kind = "'data_set'";
29  validity_start = dataset;
30  validity_end = dataset;
31  }
32  else if (run!=Q_INT_DEFAULT)
33  //validity kind set to run
34  {
35  validity_kind = "'run'";
36  validity_start = run;
37  validity_end = run;
38  }
39  else {
41  err.SetDescription(__FILE__,__LINE__,"Must provide run (SetRun()), or DataSet (SetDataSet()))");
42  }
43  if(err != QERR_SUCCESS) return err;
44 
45  std::vector<int> sruns;
46  std::vector<double> avgnoise;
47 
48  QDianaDb *db = QDianaDb::Get();
49 
50  // query noise_power_spectrum
51  std::stringstream squery;
52  squery<<"select noise_power_spectrum from noise_power_spectra where nps_algo = '"<<bareOwner
53  <<"' and nps_version= '"<<GetVersion()
54  <<"' and nps_extralabel= '"<<extraLabel
55  <<"' and channel= "<< GetChannel()
56  <<" and validity_kind= "<<validity_kind
57  <<" and validity_start= "<<validity_start
58  <<" and validity_end= "<<validity_end;
59 
60  try {
61  avgnoise = db->DoQueryVectorDouble(squery.str());
62  }
63 
64  catch(const QError& cerr) {
65  if(cerr!=QERR_SUCCESS) {
66  err = cerr;
67  return err;
68  }
69  }
70 
71  for(size_t i=0; i<avgnoise.size(); i++)
72  {
73  av.Append(avgnoise[i]);
74  }
75 
76  squery.str("");
77 
78  // query source_runs
79  squery<<"select source_runs from noise_power_spectra where nps_algo = '"<<bareOwner
80  <<"' and nps_version= '"<<GetVersion()
81  <<"' and nps_extralabel= '"<<extraLabel
82  <<"' and channel= "<< GetChannel()
83  <<" and validity_kind= "<<validity_kind
84  <<" and validity_start= "<<validity_start
85  <<" and validity_end= "<<validity_end;
86 
87  try {
88  sruns = db->DoQueryVectorInt(squery.str());
89  }
90 
91  catch(const QError& cerr) {
92  if(cerr!=QERR_SUCCESS) {
93  err = cerr;
94  return err;
95  }
96  }
97 
98  for(size_t i=0; i<sruns.size(); i++)
99  {
100  av.GetSourceRuns().push_back(sruns[i]);
101  }
102 
103  squery.str("");
104 
105  // query numevents
106  squery<<"select num_events from noise_power_spectra where nps_algo = '"<<bareOwner
107  <<"' and nps_version= '"<<GetVersion()
108  <<"' and nps_extralabel= '"<<extraLabel
109  <<"' and channel= "<< GetChannel()
110  <<" and validity_kind= "<<validity_kind
111  <<" and validity_start= "<<validity_start
112  <<" and validity_end= "<<validity_end;
113 
114  try {
115  av.SetNumEvents(db->DoQueryInt(squery.str()));
116  }
117 
118  catch(const QError& cerr) {
119  if(cerr!=QERR_SUCCESS) {
120  err = cerr;
121  return err;
122  }
123  }
124 
125  Set(av);
126 
127  return err;
128 }
129 
131 {
133 
134  const QAverageVector& av = Get();
135 
136  std::string bareOwner, extraLabel;
137  GetLabel().GetBareOwnerAndExtraLabel(bareOwner,extraLabel);
138  if (extraLabel=="") extraLabel="None";
139 
140  const int dataset = GetDataset();
141  const int run = GetRun();
142 
143  std::string validity_kind;
144  int validity_start = Q_INT_DEFAULT, validity_end = Q_INT_DEFAULT;
145 
146  // set validity_kind
147  if (dataset!=Q_INT_DEFAULT)
148  //validity kind set to dataset
149  {
150  validity_kind = "'data_set'";
151  validity_start = dataset;
152  validity_end = dataset;
153  }
154  else if (run!=Q_INT_DEFAULT)
155  //validity kind set to run
156  {
157  validity_kind = "'run'";
158  validity_start = run;
159  validity_end = run;
160  }
161  else {
163  err.SetDescription(__FILE__,__LINE__,"Must provide run (SetRun()), or DataSet (SetDataSet()))");
164  }
165  if(err != QERR_SUCCESS) return err;
166 
167 
168 
169 
170  // creates a string containing source runs list
171  std::stringstream list_run;
172  list_run<< "'{";
173 
174  for(size_t i=0; i<av.GetSourceRuns().size(); i++)
175  {
176  if(i==av.GetSourceRuns().size()-1) {
177  list_run<<av.GetSourceRuns()[i]<<"}'";;
178  } else{
179  list_run<<av.GetSourceRuns()[i]<<",";
180  }
181  }
182 
183 
184  // creates a string containing all components of the qvector
185 
186  std::stringstream list_av ;
187  list_av<<"'{";
188 
189  for(size_t i=0; i<av.Size(); i++)
190  {
191  if(i==av.Size()-1) {
192  list_av<<av[i]<<"}'";;
193  } else{
194  list_av<<av[i]<<",";
195  }
196  }
197 
198 
199 
200  // DO QUERY
201 
202  QVdt::QVdt_vector fields,values;
203  std::stringstream squery;
204  fields.push_back((std::string)"nps_algo"); values.push_back("'"+bareOwner+"'");
205  fields.push_back((std::string)"nps_version"); values.push_back("'"+GetVersion()+"'");
206  fields.push_back((std::string)"nps_extralabel"); values.push_back("'"+extraLabel+"'");
207  fields.push_back((std::string)"channel"); values.push_back(GetChannel());
208  fields.push_back((std::string)"validity_kind"); values.push_back(validity_kind);
209  fields.push_back((std::string)"validity_start"); values.push_back(validity_start);
210  fields.push_back((std::string)"validity_end"); values.push_back(validity_end);
211  fields.push_back((std::string)"source_runs"); values.push_back(list_run.str());
212  fields.push_back((std::string)"noise_power_spectrum"); values.push_back(list_av.str());
213  fields.push_back((std::string)"num_events"); values.push_back(av.GetNumEvents());
214 
215  try {
216  QDianaDb *db = QDianaDb::Get();
217  db->Insert("noise_power_spectra",fields,values);
218  }
219 
220  catch(const QError& cerr) {
221  err = cerr;
222  }
223 
224 
225  return err;
226 }
err
Definition: CheckOF.C:114
dm Get("DAQ",&rHandle,"DB")
hvec Set(vec)
#define Q_INT_DEFAULT
Definition: QDiana.hh:26
@ QERR_DB_INCONSISTENCY
Definition: QError.hh:101
@ QERR_SUCCESS
Definition: QError.hh:27
average vector object
std::vector< int > & GetSourceRuns()
Get the list of source runs.
int GetNumEvents() const
Get the number of events in the average.
void SetNumEvents(int nEvents)
Set the number of events in the average.
void Clear()
Clear the attributes.
std::vector< double > DoQueryVectorDouble(const std::string &query)
Definition: QDb.cc:260
std::vector< int > DoQueryVectorInt(const std::string &query)
Definition: QDb.cc:245
int DoQueryInt(const std::string &query)
Definition: QDb.cc:197
int Insert(const std::string &tableName, const column &fields, const column &values)
Definition: QDb.cc:320
static QDianaDb * Get()
Definition: QDianaDb.cc:21
error class with error type and description
Definition: QError.hh:115
std::vector< QVdt > QVdt_vector
Definition: QVdt.hh:38
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...