Low-Energy Muon (LEM) Experiment  0.5.2
sc_ana_module.cxx
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: sc_ana_module.cxx
4  Created by: Thomas Prokscha
5  Date: 26-Apr-2006
6 
7  Contents: Analyzer module for slow control histograms
8 
9 \********************************************************************/
10 
11 /*-- Include files -------------------------------------------------*/
12 
13 /* standard includes */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <time.h>
18 #include <string.h>
19 #include <math.h>
20 
21 #include <vector>
22 
23 /* midas includes */
24 #include "midas.h"
25 #include "experim.h"
26 #include "nemu_experim.h"
27 
28 #ifdef HAVE_HBOOK
29 #include <cfortran.h>
30 #include <hbook.h>
31 #else
32 /* root includes */
33 #include <TFolder.h>
34 #include <TH1F.h>
35 #include <TH2F.h>
36 #include <TTree.h>
37 #endif
38 
39 #define SC_MOD_CF 0
40 #define SC_MOD_XTC 1
41 #define SC_MOD_HV 2
42 #define SC_SAMPLE_CF 3
43 #define SC_PRESSURE_SC_GJ 4
44 #define SC_SAMPLE_HV 5
45 #define SC_SAMPLE_ZERO_FLUX 6
46 
47 /*-- ODB Parameters ------------------------------------------------*/
48 extern INFO info;
49 extern RUNINFO runinfo;
53 SCANAMODULE_PARAM_STR(sc_ana_param_str);
54 extern float get_magnetic_field(float current);
55 
56 /*-- Module declaration --------------------------------------------*/
57 INT sc_init(void);
58 INT sc_exit();
59 INT sc_ana(EVENT_HEADER*,void*);
60 INT sc_bor(INT run_number);
61 INT sc_eor(INT run_number);
62 
63 ANA_MODULE sc_ana_module = {
64  "SCAnaModule", /* module name */
65  "T. Prokscha, A. Suter", /* author */
66  sc_ana, /* event routine */
67  sc_bor, /* BOR routine */
68  sc_eor, /* EOR routine */
69  sc_init, /* init routine */
70  sc_exit, /* exit routine */
71  &sc_ana_param, /* parameter structure */
72  sizeof(sc_ana_param), /* structure size */
73  (const char **)sc_ana_param_str, /* initial parameters */
74 };
75 
76 /*-- other global variables ----------------------------------------*/
77 
78 std::vector< std::vector<double> > sc_histo_vec;
79 
80 #define N_SC_HIST 8
81 #ifndef HAVE_HBOOK
82 static TH1F* hSC_Hist[N_SC_HIST];
83 //the following objects are from mana.c
84 extern TFolder *gManaHistosFolder;
85 extern TObjArray *gHistoFolderStack;
86 #endif
87 
88 static INT offset;
89 
90 //-- INIT routine --------------------------------------------------
91 INT sc_init(void)
92 {
93  sc_histo_vec.clear(); // clean up - paranoia
94  sc_histo_vec.resize(N_SC_HIST); // resize it to the proper number of SC histograms
95 
96  return SUCCESS;
97 }
98 
99 //-- EXIT routine ---------------------------------------------------
100 
101 INT sc_exit()
102 {
103  // clean up
104  sc_histo_vec.clear();
105 
106  return SUCCESS;
107 }
108 
109 //-- BOR routine ---------------------------------------------------
110 
111 INT sc_bor(INT run_number)
112 {
113  HNDLE hDB, hKey;
114  INT status, size, ival, success;
115  char str[32];
116 
117  sc_histo_vec.clear(); // clean up - paranoia
118  sc_histo_vec.resize(N_SC_HIST); // resize it to the proper number of SC histograms
119 
120  // check the ctrl channel of the sample cryostat and set the offset for the analyzer module
121  cm_get_experiment_database(&hDB, NULL);
122  status = db_find_key(hDB, 0, "/Equipment/SampleCryo/Settings/Devices/Lake336_Sample_0/DD/Loop1/CTRL_CH", &hKey);
123  if (status != DB_SUCCESS) {
124  cm_msg(MINFO, "sc_bor", "analyzer/sc_ana_module/sc_bor: couldn't obtain sample cryo ctrl channel key.");
125  } else {
126  size = sizeof(str);
127  success = db_get_data(hDB, hKey, str, &size, TID_STRING);
128  if (status != DB_SUCCESS) {
129  cm_msg(MINFO, "sc_bor", "analyzer/sc_ana_module/sc_bor: couldn't obtain sample cryo ctrl channel value");
130  } else {
131  success = db_find_key(hDB, 0, "/Analyzer/Parameters/SCAnaModule/sample_CF_CTRL_channel", &hKey);
132  if (strstr(str, "A")) {
133  ival = 0;
134  size = sizeof(ival);
135  db_set_data(hDB, hKey, &ival, size, 1, TID_INT);
136  } else {
137  ival = 1;
138  size = sizeof(ival);
139  db_set_data(hDB, hKey, &ival, size, 1, TID_INT);
140  }
141  }
142  }
143 
144  return SUCCESS;
145 }
146 
147 /*-- EOR routine ---------------------------------------------------*/
152 INT sc_eor(INT run_number)
153 {
154  INT nbin;
155  INT odb_period;
156  double period;
157  char name[256], title[256];
158  extern char runname[256];
159  TH1F *hist;
160  HNDLE hDB, hKey;
161  INT status, size;
162 
163  // check if histo already exists, and if so remove them: (i) from the folder, and (ii) from the memory
164  for (int i=0; i<N_SC_HIST; i++){
165  sprintf(name, "%s", sc_ana_param.histotitles.titles[i]);
166  if (!gHistoFolderStack->Last())
167  hist = (TH1F *) gManaHistosFolder->FindObjectAny(name);
168  else
169  hist = (TH1F *) ((TFolder *) gHistoFolderStack->Last())->FindObjectAny(name);
170 
171  if (hist != NULL) {
172  if (!gHistoFolderStack->Last())
173  gManaHistosFolder->Remove(hist);
174  else
175  ((TFolder *) gHistoFolderStack->Last())->Remove(hist);
176 
177  // since object is now removed, it can be deleted
178  delete hist;
179  }
180  }
181 
182  // get reading period
183  cm_get_experiment_database(&hDB, NULL);
184  status = db_find_key(hDB, 0, "/Equipment/SlowControl/Common/Period", &hKey);
185  if (status != DB_SUCCESS) {
186  cm_msg(MERROR, "sc_eor", "sc_eor: couldn't obtain the reading period, will assume 30 sec.");
187  period = 30.0;
188  } else {
189  size = sizeof(INT);
190  status = db_get_data(hDB, hKey, &odb_period, &size, TID_INT);
191  if (status == DB_SUCCESS) {
192  period = (double) odb_period / 1.0e3;
193  } else {
194  cm_msg(MERROR, "sc_eor", "sc_eor: couldn't obtain the reading period, will assume 30 sec.");
195  period = 30.0;
196  }
197  }
198 
199  // create all the necessary slow control histograms
200  for (int i=0; i<N_SC_HIST; i++) {
201  sprintf(name, "%s", sc_ana_param.histotitles.titles[i]);
202  sprintf(title, "%s Run %s_%04d", sc_ana_param.histotitles.titles[i], runname, runinfo.run_number);
203  nbin = (INT) sc_histo_vec[i].size();
204  hSC_Hist[i] = new TH1F(name, title, nbin, -period*0.5, ((double)nbin+0.5)*period);
205  if (hSC_Hist[i] == 0) {
206  cm_msg(MERROR, "sc_eor", "sc_eor: hSC_Hist[%d]==0!! Cannot book the slow control histograms.", i);
207  return SUCCESS;
208  }
209 
210  // set the title
211  hSC_Hist[i]->GetXaxis()->SetTitle("time (sec since SOR)");
212 
213  // fill the histos
214  for (int j=0; j<sc_histo_vec[i].size(); j++) {
215  hSC_Hist[i]->SetBinContent(j+1, sc_histo_vec[i][j]);
216  }
217 
218  // add the slow control histograms to the SCAnaModule folder
219  TFolder *scFolder = (TFolder *) gManaHistosFolder->FindObject("SCAnaModule");
220  scFolder->Add(hSC_Hist[i]);
221  }
222 
223  return SUCCESS;
224 }
225 /*-- event routine -------------------------------------------------*/
235 INT sc_ana(EVENT_HEADER *pheader, void *pevent)
236 {
237  INT n, ind;
238  float *ptdc, field, current;
239  HNDLE hDB, hKey;
240  double meanModeratorHV, meanSampleHV, meanSampleT, meanSampleB;
241  double errorModeratorHV, errorSampleHV, errorSampleT, errorSampleB;
242  unsigned int i;
243 
244  // look for MMOD bank, ModCryo data
245  n = bk_locate(pevent, "MMOD", &ptdc);
246  if (n != 0 ){
247  ind = sc_ana_param.mod_cf1_channel;
248  sc_histo_vec[SC_MOD_CF].push_back(ptdc[ind]);
249  ind = sc_ana_param.mod_xtc_channel;
250  sc_histo_vec[SC_MOD_XTC].push_back(ptdc[ind]);
251  }
252 
253  // look for MSAM bank, Sample Temperature data
254  n = bk_locate(pevent, "MSAM", &ptdc);
255  if (n != 0 ){
256  ind = sc_ana_param.sample_cf_ctrl_channel;
257  sc_histo_vec[SC_SAMPLE_CF].push_back(ptdc[ind]);
258  }
259 
260  // look for MVAC bank, vacuum data
261  n = bk_locate(pevent, "MVAC", &ptdc);
262  if (n != 0 ){
263  ind = sc_ana_param.sample_sc_gj_channel;
264  sc_histo_vec[SC_PRESSURE_SC_GJ].push_back(ptdc[ind]);
265  }
266 
267  // look for MHVT bank, transport HV data
268  n = bk_locate(pevent, "MHVT", &ptdc);
269  if (n != 0 ){
270  ind = sc_ana_param.mod_hv_channel;
271  sc_histo_vec[SC_MOD_HV].push_back(ptdc[ind]);
272  ind = sc_ana_param.sample_hv_channel;
273  sc_histo_vec[SC_SAMPLE_HV].push_back(ptdc[ind]);
274  }
275 
276  // look for MVAC bank, vacuum data
277  n = bk_locate(pevent, "M900", &ptdc);
278  if (n != 0 ){
279  ind = sc_ana_param.sample_zeroflux_channel;
280  current = ptdc[ind]/10.*500.;
281  field = get_magnetic_field(current);
282  sc_histo_vec[SC_SAMPLE_ZERO_FLUX].push_back(field);
283  }
284 
285  // ----------------------------------------------------------------
286  // Calculate mean values and errors of mean values for selected
287  // slow control parameter.
288  // Write mean and errors to /Equipment/SlowControl/Variables/MEAN.
289 
290  // calculate mean value for the moderator HV
291  meanModeratorHV = 0.0;
292  for (i=0; i<sc_histo_vec[SC_MOD_HV].size(); i++)
293  meanModeratorHV += sc_histo_vec[SC_MOD_HV][i];
294  meanModeratorHV /= sc_histo_vec[SC_MOD_HV].size();
295  // calculate standard deviation for the moderator HV
296  errorModeratorHV = 0.0;
297  for (i=0; i<sc_histo_vec[SC_MOD_HV].size(); i++)
298  errorModeratorHV += (meanModeratorHV - sc_histo_vec[SC_MOD_HV][i]) * (meanModeratorHV - sc_histo_vec[SC_MOD_HV][i]);
299  errorModeratorHV = sqrt(errorModeratorHV/(float)(sc_histo_vec[SC_MOD_HV].size()-1));
300 
301  // calculate mean value for the sample HV
302  meanSampleHV = 0.0;
303  for (i=0; i<sc_histo_vec[SC_SAMPLE_HV].size(); i++)
304  meanSampleHV += sc_histo_vec[SC_SAMPLE_HV][i];
305  meanSampleHV /= sc_histo_vec[SC_SAMPLE_HV].size();
306  // calculate standard deviation for the sample HV
307  errorSampleHV = 0.0;
308  for (i=0; i<sc_histo_vec[SC_SAMPLE_HV].size(); i++)
309  errorSampleHV += (meanSampleHV - sc_histo_vec[SC_SAMPLE_HV][i]) * (meanSampleHV - sc_histo_vec[SC_SAMPLE_HV][i]);
310  errorSampleHV = sqrt(errorSampleHV/(float)(sc_histo_vec[SC_SAMPLE_HV].size()-1));
311 
312  // calculate mean value for the sample temperature
313  meanSampleT = 0.0;
314  for (i=0; i<sc_histo_vec[SC_SAMPLE_CF].size(); i++)
315  meanSampleT += sc_histo_vec[SC_SAMPLE_CF][i];
316  meanSampleT /= sc_histo_vec[SC_SAMPLE_CF].size();
317  // calculate standard deviation for the sample temperature
318  errorSampleT = 0.0;
319  for (i=0; i<sc_histo_vec[SC_SAMPLE_CF].size(); i++)
320  errorSampleT += (meanSampleT - sc_histo_vec[SC_SAMPLE_CF][i]) * (meanSampleT - sc_histo_vec[SC_SAMPLE_CF][i]);
321  errorSampleT = sqrt(errorSampleT/(float)(sc_histo_vec[SC_SAMPLE_CF].size()-1));
322 
323  // calculate mean value for the sample field
324  meanSampleB = 0.0;
325  for (i=0; i<sc_histo_vec[SC_SAMPLE_ZERO_FLUX].size(); i++)
326  meanSampleB += sc_histo_vec[SC_SAMPLE_ZERO_FLUX][i];
327  meanSampleB /= sc_histo_vec[SC_SAMPLE_ZERO_FLUX].size();
328  // calculate standard deviation for the sample field
329  errorSampleB = 0.0;
330  for (i=0; i<sc_histo_vec[SC_SAMPLE_ZERO_FLUX].size(); i++)
331  errorSampleB += (meanSampleB - sc_histo_vec[SC_SAMPLE_ZERO_FLUX][i]) * (meanSampleB - sc_histo_vec[SC_SAMPLE_ZERO_FLUX][i]);
332  errorSampleB = sqrt(errorSampleB/(float)(sc_histo_vec[SC_SAMPLE_ZERO_FLUX].size()-1));
333 
334  mean.moderator_hv = (float) meanModeratorHV;
335  mean.sample_hv = (float) meanSampleHV;
336  mean.sample_t = (float) meanSampleT;
337  mean.sample_b = (float) meanSampleB;
338 
339  mean.var_moderator_hv = (float) errorModeratorHV;
340  mean.var_sample_hv = (float) errorSampleHV;
341  mean.var_sample_t = (float) errorSampleT;
342  mean.var_sample_b = (float) errorSampleB;
343 
344  cm_get_experiment_database(&hDB, NULL);
345  db_find_key(hDB, 0, "/Equipment/SlowControl/Variables/MEAN", &hKey);
346  db_set_record(hDB, hKey, &mean, sizeof(mean), 0);
347 
348  return SUCCESS;
349 }
350 /* ----------------------------------------------------------------------------*\
351  EOF sc_ana_module.c
352 \* ----------------------------------------------------------------------------*/
INT32 mod_cf1_channel
Definition: experim.h:475
INT32 sample_zeroflux_channel
Definition: experim.h:481
#define SC_SAMPLE_CF
float moderator_hv
Definition: experim.h:1343
float var_sample_hv
Definition: experim.h:1348
float var_moderator_hv
Definition: experim.h:1347
struct SCANAMODULE_PARAM::@16 histotitles
#define SC_SAMPLE_HV
std::vector< std::vector< double > > sc_histo_vec
INT sc_ana(EVENT_HEADER *, void *)
#define SC_MOD_HV
INT32 sample_cf_ctrl_channel
Definition: experim.h:478
float sample_b
Definition: experim.h:1346
static INT offset
float var_sample_b
Definition: experim.h:1350
char runname[256]
Definition: analyzer.cxx:79
float sample_t
Definition: experim.h:1345
SCANAMODULE_PARAM sc_ana_param
#define SC_SAMPLE_ZERO_FLUX
MEAN_BANK mean
TObjArray * gHistoFolderStack
INFO info
Definition: analyzer.cxx:94
TRIGGER_SETTINGS trigger_settings
Definition: analyzer.cxx:93
INT sc_exit()
INT sc_bor(INT run_number)
RUNINFO runinfo
Definition: analyzer.cxx:83
float sample_hv
Definition: experim.h:1344
INT32 mod_xtc_channel
Definition: experim.h:476
SCANAMODULE_PARAM_STR(sc_ana_param_str)
INT32 sample_hv_channel
Definition: experim.h:480
#define N_SC_HIST
#define SC_PRESSURE_SC_GJ
TFolder * gManaHistosFolder
HNDLE hKey
INT sc_eor(INT run_number)
ANA_MODULE sc_ana_module
INT sc_init(void)
char titles[8][32]
Definition: experim.h:484
INT32 mod_hv_channel
Definition: experim.h:477
static TH1F * hSC_Hist[N_SC_HIST]
float var_sample_t
Definition: experim.h:1349
HNDLE hDB
#define SC_MOD_CF
#define SC_MOD_XTC
INT32 sample_sc_gj_channel
Definition: experim.h:479
float get_magnetic_field(float current)
Definition: analyzer.cxx:998