Diana Software
QAveragePulseHandle.cc
Go to the documentation of this file.
1 #include "QAveragePulseHandle.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> avgpulse;
47 
48  QDianaDb *db = QDianaDb::Get();
49 
50  // query average_pulse
51  std::stringstream squery;
52  squery<<"select average_pulse from average_pulses where ap_algo = '"<<bareOwner
53  <<"' and ap_version = '"<<GetVersion()
54  <<"' and ap_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  avgpulse = 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  av.Resize(avgpulse.size());
72  for(size_t i=0; i<avgpulse.size(); i++)
73  {
74  av[i] = avgpulse[i];
75  }
76 
77  squery.str("");
78 
79  // query source_runs
80  squery<<"select source_runs from average_pulses where ap_algo = '"<<bareOwner
81  <<"' and ap_version= '"<<GetVersion()
82  <<"' and ap_extralabel= '"<<extraLabel
83  <<"' and channel= "<< GetChannel()
84  <<" and validity_kind= "<<validity_kind
85  <<" and validity_start= "<<validity_start
86  <<" and validity_end= "<<validity_end;
87 
88  try {
89  sruns = db->DoQueryVectorInt(squery.str());
90  }
91 
92  catch(const QError& cerr) {
93  if(cerr!=QERR_SUCCESS) {
94  err = cerr;
95  return err;
96  }
97  }
98 
99  for(size_t i=0; i<sruns.size(); i++)
100  {
101  av.GetSourceRuns().push_back(sruns[i]);
102  }
103 
104  squery.str("");
105 
106  // query numevents
107  squery<<"select num_events from average_pulses where ap_algo = '"<<bareOwner
108  <<"' and ap_version= '"<<GetVersion()
109  <<"' and ap_extralabel= '"<<extraLabel
110  <<"' and channel= "<< GetChannel()
111  <<" and validity_kind= "<<validity_kind
112  <<" and validity_start= "<<validity_start
113  <<" and validity_end= "<<validity_end;
114 
115  try {
116  av.SetNumEvents(db->DoQueryInt(squery.str()));
117  }
118 
119  catch(const QError& cerr) {
120  if(cerr!=QERR_SUCCESS) {
121  err = cerr;
122  return err;
123  }
124  }
125 
126  Set(av);
127 
128  return err;
129 }
130 
131 
133 {
135 
136  const QAverageVector& av = Get();
137 
138  std::string bareOwner, extraLabel;
139  GetLabel().GetBareOwnerAndExtraLabel(bareOwner,extraLabel);
140  if (extraLabel=="") extraLabel="None";
141 
142  const int dataset = GetDataset();
143  const int run = GetRun();
144 
145  std::string validity_kind;
146  int validity_start = Q_INT_DEFAULT, validity_end = Q_INT_DEFAULT;
147 
148  // set validity_kind
149  if (dataset!=Q_INT_DEFAULT)
150  //validity kind set to dataset
151  {
152  validity_kind = "'data_set'";
153  validity_start = dataset;
154  validity_end = dataset;
155  }
156  else if (run!=Q_INT_DEFAULT)
157  //validity kind set to run
158  {
159  validity_kind = "'run'";
160  validity_start = run;
161  validity_end = run;
162  }
163  else {
165  err.SetDescription(__FILE__,__LINE__,"Must provide run (SetRun()), or DataSet (SetDataSet()))");
166  }
167  if(err != QERR_SUCCESS) return err;
168 
169  // creates a string containing source runs list
170  std::stringstream list_run;
171  list_run<< "'{";
172 
173  for(size_t i=0; i<av.GetSourceRuns().size(); i++)
174  {
175  if(i==av.GetSourceRuns().size()-1) {
176  list_run<<av.GetSourceRuns()[i]<<"}'";
177  } else{
178  list_run<<av.GetSourceRuns()[i]<<",";
179  }
180  }
181 
182 
183  // creates a string containing all components of the qvector
184 
185  std::stringstream list_av ;
186  list_av<<"'{";
187 
188  for(size_t i=0; i<av.Size(); i++)
189  {
190  if(i==av.Size()-1) {
191  list_av<<av[i]<<"}'";;
192  } else{
193  list_av<<av[i]<<",";
194  }
195  }
196 
197 
198  // DO QUERY
199 
200  QVdt::QVdt_vector fields,values;
201  fields.push_back((std::string)"ap_algo"); values.push_back("'"+bareOwner+"'");
202  fields.push_back((std::string)"ap_version"); values.push_back("'"+GetVersion()+"'");
203  fields.push_back((std::string)"ap_extralabel"); values.push_back("'"+extraLabel+"'");
204  fields.push_back((std::string)"channel"); values.push_back(GetChannel());
205  fields.push_back((std::string)"validity_kind"); values.push_back(validity_kind);
206  fields.push_back((std::string)"validity_start");values.push_back(validity_start);
207  fields.push_back((std::string)"validity_end"); values.push_back(validity_end);
208  fields.push_back((std::string)"source_runs"); values.push_back(list_run.str());
209  fields.push_back((std::string)"average_pulse"); values.push_back(list_av.str());
210  fields.push_back((std::string)"num_events"); values.push_back(av.GetNumEvents());
211 
212  try {
213  QDianaDb *db = QDianaDb::Get();
214  db->Insert("average_pulses",fields,values);
215  }
216 
217 
218 
219  catch(const QError& cerr) {
220  err = cerr;
221  }
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...