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