Low-Energy Muon (LEM) Experiment  0.5.1
mcp1_ana_module.c
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: mcp1_ana_module.c
4  Created by: Thomas Prokscha
5  Date: 07-Oct-2005
6 
7  Contents: Analyzer module for LEM experiment BC-MCP1-(e+) TOF
8 
9 \********************************************************************/
10 
11 /*-- Include files -------------------------------------------------*/
12 
13 /* standard includes */
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include <string.h>
18 #include <math.h>
19 
20 /* midas includes */
21 #include "midas.h"
22 #include "rmidas.h"
23 #include "v1190.h"
24 #include "experim.h"
25 #include "nemu_experim.h"
26 
27 #ifdef HAVE_HBOOK
28 #include <cfortran.h>
29 #include <hbook.h>
30 #else
31 /* root includes */
32 #include <TH1F.h>
33 #include <TH2F.h>
34 #include <TTree.h>
35 #endif
36 
37 /*-- ODB Parameters ------------------------------------------------*/
38 //extern RUNINFO runinfo;
39 //extern EXP_PARAM exp_param;
40 extern RUNINFO runinfo;
43 MCP1ANAMODULE_PARAM_STR(mcp1_ana_param_str);
44 
45 /*-- Module declaration --------------------------------------------*/
46 INT mcp1_init(void);
47 INT mcp1_ana(EVENT_HEADER*,void*);
48 INT mcp1_bor(INT run_number);
49 INT mcp1_eor(INT run_number);
50 
51 ANA_MODULE mcp1_ana_module = {
52  "MCP1AnaModule", /* module name */
53  "Thomas Prokscha", /* author */
54  mcp1_ana, /* event routine */
55  mcp1_bor, /* BOR routine */
56  mcp1_eor, /* EOR routine */
57  mcp1_init, /* init routine */
58  NULL, /* exit routine */
59  &mcp1_ana_param, /* parameter structure */
60  sizeof(mcp1_ana_param), /* structure size */
61  (const char **)mcp1_ana_param_str, /* initial parameters */
62 };
63 
64 /*-- other global variables ----------------------------------------*/
65 #define N_MCP1HIST 3
66 #ifndef HAVE_HBOOK
67 static TH1F* hMCP1Hist[N_MCP1HIST];
68 //the following objects are from mana.c
69 extern TFolder *gManaHistosFolder;
70 extern TObjArray *gHistoFolderStack;
71 #endif
72 
73 static DWORD dataWindowMuonDecay; //needed for post-pileup rejection
74 
75 /*-- INIT routine --------------------------------------------------*/
76 INT mcp1_init(void)
77 {
78 HNDLE hDB;
79 extern char runname[256];
80 char title[256], name[256];
81 int nbin, i, j;
82 float xlow, xhigh;
83 
84  for (i=0; i<N_MCP1HIST; i++){
85  sprintf(name, "hMCP1%02d", i);
86  sprintf(title, "%s Run %s_%04d", mcp1_ana_param.histotitles.toftitles[i], runname, runinfo.run_number);
87  nbin = mcp1_ana_param.histobinning.histonbin[i];
88  xlow = mcp1_ana_param.histobinning.histoxlow[i];
89  xhigh = mcp1_ana_param.histobinning.histoxup[i];
90  hMCP1Hist[i] = H1_BOOK(name, title, nbin, xlow, xhigh);
91  }
92 
94 
95  return SUCCESS;
96 }
97 
98 /*-- BOR routine ---------------------------------------------------*/
99 
100 INT mcp1_bor(INT run_number)
101 {
102 int i;
103 char title[256];
104 extern char runname[256];
105 
106  for (i=0; i<N_MCP1HIST; i++){
107  sprintf(title, "%s Run %s_%04d", mcp1_ana_param.histotitles.toftitles[i], runname, run_number);
108  hMCP1Hist[i]->SetTitle(title);
109  }
110 
112 
113  return SUCCESS;
114 }
115 
116 /*-- EOR routine ---------------------------------------------------*/
117 
118 INT mcp1_eor(INT run_number)
119 {
120 
121  return SUCCESS;
122 }
123 
124 /*-- event routine -------------------------------------------------*/
131 INT mcp1_ana(EVENT_HEADER *pheader, void *pevent)
132 {
133 INT n, i, j, counter[N_TDC_CHANNELS];
134 WORD channel;
135 DWORD *ptdc;
136 DWORD tdc_data[N_TDC_CHANNELS][DATA_N_HITS];
137 INT difftime_last, difftime_now;
138 BOOL foundBC, foundPOS;
139 LEM_DATA *lem_data;
140 
141  /* look for TDC0 bank */
142  n = bk_locate(pevent, "TDC0", &ptdc);
143  if (n == 0 ) return 1;
144 
145  // return if we don't have the NEW_EVENT_FLAG present
146  if ( !(ptdc[0] & NEW_EVENT_MASK)) return 1;
147 
148  // return if not EVENT_0_TYPE (BC-MCP1)
149  if ( !(ptdc[0] & (EVENT_0_TYPE<<16)) ) return 1;
150 
151  memset(&counter, 0x00, sizeof(counter));
152  memset(&tdc_data, 0x00, sizeof(tdc_data));
153  foundBC = foundPOS = FALSE;
154 
155  for (i = 1; i < n; i++){ // i=0 is a control word, including the event length
156  lem_data = (LEM_DATA *) &ptdc[i];
157  channel = lem_data->channel;
158  if ( channel > N_TDC_CHANNELS-1 ) continue; //not understood how this can happen, but it can happen
159  //after a HV break at the moderator (all channel get "many" hits, like
160  //a "flash" in the detectors
161  tdc_data[channel][counter[channel]] = lem_data->data;
162  counter[channel]++;
163 
164  switch ( channel ){
165  case BCL1: case BCL2: case BCL3: case BCL4: case BCR1: case BCR2: case BCR3: case BCR4:
166  foundBC = TRUE;
167  break;
168 
169  case POSMCP1I: case POSMCP1O:
170  foundPOS = TRUE;
171  break;
172 
173  default:
174  break;
175  }
176  }
177 
178  if ( !foundBC && !foundPOS ) return 1; // no BeamCounter or e+ data found
179 
180 
181  if ( foundBC ){
182 // find the next most distant BC hit with respect to MCP1 and fill it in the TOF histogram
183 /* difftime_now = difftime_last = 0;
184  for ( i=BCL1; i<=BCR4; i++){
185  if ( counter[i] == 0 ) continue;
186  for (j = 0; j<counter[i]; j++){
187  difftime_now = tdc_data[MCP1][0] - tdc_data[i][j];
188  if ( (difftime_now > 0) &&
189  (difftime_now < trigger_settings.tof_bc_window) &&
190  (difftime_now > difftime_last))
191  difftime_last = difftime_now;
192  }
193  }
194 */
195 // find the nearest BC hit with respect to MCP1 and fill it in the TOF histogram
196  difftime_now = difftime_last = -2*trigger_settings.tof_bc_window;
197  for ( i=BCL1; i<=BCR4; i++){
198  if ( counter[i] == 0 ) continue;
199  for (j = 0; j<counter[i]; j++){
200  difftime_now = tdc_data[MCP1][0] - tdc_data[i][j];
201  if ( (difftime_now < 0) && (fabs(difftime_now) < trigger_settings.tof_bc_window) &&
202  (difftime_now > difftime_last)) //we have negative differences
203  difftime_last = difftime_now;
204  }
205  }
206  difftime_last += trigger_settings.tof_bc_window; //to have "forward" timing
207 
208  hMCP1Hist[0]->Fill( (float) difftime_last, 1.);
209  }
210 
211  if ( tdc_data[POSMCP1I][0] > 0 && tdc_data[POSMCP1O][0] > 0) {
212  difftime_last = tdc_data[POSMCP1I][0] - tdc_data[MCP1][0];
213  hMCP1Hist[1]->Fill( (float) difftime_last, 1.);
214 
215  // check for post-pileup in MCP1: if post-pileup, return
216  if ( tdc_data[MCP1][1] != 0 &&
217  (tdc_data[MCP1][1] - tdc_data[MCP1][0]) < dataWindowMuonDecay) return SUCCESS;
218 
219  // check, if there was no 2nd e+ during data window
220  if ( tdc_data[POSMCP1I][1] != 0 ){
221  if ( (tdc_data[POSMCP1I][1] - tdc_data[MCP1][0]) < dataWindowMuonDecay &&
222  abs(tdc_data[POSMCP1I][1]-tdc_data[POSMCP1O][1]) < 50 ) return SUCCESS; //2nd e+
223  }
224  hMCP1Hist[2]->Fill( (float) tdc_data[POSMCP1I][0] - tdc_data[MCP1][0], 1.);
225  }
226  return SUCCESS;
227 }
228 /* ----------------------------------------------------------------------------*\
229  EOF mcp1_ana_module.c
230 \* ----------------------------------------------------------------------------*/
#define BCR1
Definition: nemu_experim.h:22
WORD channel
Definition: nemu_experim.h:93
static TH1F * hMCP1Hist[N_MCP1HIST]
float histoxlow[3]
Definition: experim.h:416
ANA_MODULE mcp1_ana_module
INT mcp1_eor(INT run_number)
#define BCR4
Definition: nemu_experim.h:25
#define BCL4
Definition: nemu_experim.h:21
struct MCP1ANAMODULE_PARAM::@14 histotitles
#define POSMCP1O
Definition: nemu_experim.h:27
char runname[256]
Definition: analyzer.c:92
char toftitles[3][32]
Definition: experim.h:412
RUNINFO runinfo
/Runinfo ODB key, defined in from midas.h
Definition: vme_fe.c:193
float histoxup[3]
Definition: experim.h:417
#define BCL1
Definition: nemu_experim.h:18
#define BCL3
Definition: nemu_experim.h:20
#define N_MCP1HIST
HNDLE hDB
Definition: write_summary.c:97
MCP1ANAMODULE_PARAM mcp1_ana_param
DWORD tof_bc_window
Definition: experim.h:692
INT mcp1_init(void)
#define BCR2
Definition: nemu_experim.h:23
INT mcp1_bor(INT run_number)
struct MCP1ANAMODULE_PARAM::@15 histobinning
#define POSMCP1I
Definition: nemu_experim.h:26
#define DATA_N_HITS
max. of DATA_N_HITS in data file
Definition: nemu_experim.h:66
TObjArray * gHistoFolderStack
#define BCR3
Definition: nemu_experim.h:24
#define NEW_EVENT_MASK
Definition: nemu_experim.h:67
TRIGGER_SETTINGS trigger_settings
/Equipment/Trigger/Settings
Definition: vme_fe.c:188
#define N_TDC_CHANNELS
number of TDC channels
Definition: nemu_experim.h:64
#define BCL2
Definition: nemu_experim.h:19
DWORD data_window
Definition: experim.h:664
DWORD data
Definition: nemu_experim.h:92
#define MCP1
Definition: nemu_experim.h:16
MCP1ANAMODULE_PARAM_STR(mcp1_ana_param_str)
struct TRIGGER_SETTINGS::@18 event_0_settings
static DWORD dataWindowMuonDecay
#define EVENT_0_TYPE
EVENT type 0 = BC-MCP1-(e+)
Definition: nemu_experim.h:75
INT mcp1_ana(EVENT_HEADER *, void *)
TFolder * gManaHistosFolder