Low-Energy Muon (LEM) Experiment  0.5.2
mcp1_ana_module.cxx
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;
41 MCP1ANAMODULE_PARAM_STR(mcp1_ana_param_str);
42 
43 /*-- Module declaration --------------------------------------------*/
44 INT mcp1_init(void);
45 INT mcp1_ana(EVENT_HEADER*,void*);
46 INT mcp1_bor(INT run_number);
47 INT mcp1_eor(INT run_number);
48 
49 ANA_MODULE mcp1_ana_module = {
50  "MCP1AnaModule", /* module name */
51  "Thomas Prokscha", /* author */
52  mcp1_ana, /* event routine */
53  mcp1_bor, /* BOR routine */
54  mcp1_eor, /* EOR routine */
55  mcp1_init, /* init routine */
56  NULL, /* exit routine */
57  &mcp1_ana_param, /* parameter structure */
58  sizeof(mcp1_ana_param), /* structure size */
59  (const char **)mcp1_ana_param_str, /* initial parameters */
60 };
61 
62 /*-- other global variables ----------------------------------------*/
63 #define N_MCP1HIST 3
64 #ifndef HAVE_HBOOK
65 static TH1F* hMCP1Hist[N_MCP1HIST];
66 //the following objects are from mana.c
67 extern TFolder *gManaHistosFolder;
68 extern TObjArray *gHistoFolderStack;
69 #endif
70 
71 static DWORD dataWindowMuonDecay; //needed for post-pileup rejection
72 
73 /*-- INIT routine --------------------------------------------------*/
74 INT mcp1_init(void)
75 {
76  HNDLE hDB;
77  extern char runname[256];
78  char title[256], name[256];
79  int nbin;
80  float xlow, xhigh;
81 
82  for (int i=0; i<N_MCP1HIST; i++){
83  sprintf(name, "hMCP1%02d", i);
84  sprintf(title, "%s Run %s_%04d", mcp1_ana_param.histotitles.toftitles[i], runname, runinfo.run_number);
85  nbin = mcp1_ana_param.histobinning.histonbin[i];
86  xlow = mcp1_ana_param.histobinning.histoxlow[i];
87  xhigh = mcp1_ana_param.histobinning.histoxup[i];
88  hMCP1Hist[i] = H1_BOOK(name, title, nbin, xlow, xhigh);
89  }
90 
92 
93  return SUCCESS;
94 }
95 
96 /*-- BOR routine ---------------------------------------------------*/
97 
98 INT mcp1_bor(INT run_number)
99 {
100  char title[256];
101  extern char runname[256];
102 
103  for (int i=0; i<N_MCP1HIST; i++){
104  sprintf(title, "%s Run %s_%04d", mcp1_ana_param.histotitles.toftitles[i], runname, run_number);
105  hMCP1Hist[i]->SetTitle(title);
106  }
107 
109 
110  return SUCCESS;
111 }
112 
113 /*-- EOR routine ---------------------------------------------------*/
114 
115 INT mcp1_eor(INT run_number)
116 {
117  return SUCCESS;
118 }
119 
120 /*-- event routine -------------------------------------------------*/
127 INT mcp1_ana(EVENT_HEADER *pheader, void *pevent)
128 {
129  INT n, counter[N_TDC_CHANNELS];
130  WORD channel;
131  DWORD *ptdc;
132  DWORD tdc_data[N_TDC_CHANNELS][DATA_N_HITS];
133  INT difftime_last, difftime_now;
134  BOOL foundBC, foundPOS;
135  LEM_DATA *lem_data;
136 
137  /* look for TDC0 bank */
138  n = bk_locate(pevent, "TDC0", &ptdc);
139  if (n == 0 ) return 1;
140 
141  // return if we don't have the NEW_EVENT_FLAG present
142  if ( !(ptdc[0] & NEW_EVENT_MASK)) return 1;
143 
144  // return if not EVENT_0_TYPE (BC-MCP1)
145  if ( !(ptdc[0] & (EVENT_0_TYPE<<16)) ) return 1;
146 
147  memset(&counter, 0x00, sizeof(counter));
148  memset(&tdc_data, 0x00, sizeof(tdc_data));
149  foundBC = foundPOS = FALSE;
150 
151  for (int i=1; i<n; i++){ // i=0 is a control word, including the event length
152  lem_data = (LEM_DATA *) &ptdc[i];
153  channel = lem_data->channel;
154  if ( channel > N_TDC_CHANNELS-1 ) continue; //not understood how this can happen, but it can happen
155  //after a HV break at the moderator (all channel get "many" hits, like
156  //a "flash" in the detectors
157  tdc_data[channel][counter[channel]] = lem_data->data;
158  counter[channel]++;
159 
160  switch ( channel ){
161  case BCL1: case BCL2: case BCL3: case BCL4: case BCR1: case BCR2: case BCR3: case BCR4:
162  foundBC = TRUE;
163  break;
164 
165  case POSMCP1I: case POSMCP1O:
166  foundPOS = TRUE;
167  break;
168 
169  default:
170  break;
171  }
172  }
173 
174  if ( !foundBC && !foundPOS ) return 1; // no BeamCounter or e+ data found
175 
176 
177  if ( foundBC ){
178  // find the nearest BC hit with respect to MCP1 and fill it in the TOF histogram
179  difftime_now = difftime_last = -2*trigger_settings.tof_bc_window;
180  for (int i=BCL1; i<=BCR4; i++){
181  if ( counter[i] == 0 ) continue;
182  for (int j=0; j<counter[i]; j++){
183  difftime_now = tdc_data[MCP1][0] - tdc_data[i][j];
184  if ( (difftime_now < 0) && (fabs(difftime_now) < trigger_settings.tof_bc_window) &&
185  (difftime_now > difftime_last)) //we have negative differences
186  difftime_last = difftime_now;
187  }
188  }
189  difftime_last += trigger_settings.tof_bc_window; //to have "forward" timing
190 
191  hMCP1Hist[0]->Fill( (float) difftime_last, 1.);
192  }
193 
194  if ( tdc_data[POSMCP1I][0] > 0 && tdc_data[POSMCP1O][0] > 0) {
195  difftime_last = tdc_data[POSMCP1I][0] - tdc_data[MCP1][0];
196  hMCP1Hist[1]->Fill( (float) difftime_last, 1.);
197 
198  // check for post-pileup in MCP1: if post-pileup, return
199  if ( tdc_data[MCP1][1] != 0 &&
200  (tdc_data[MCP1][1] - tdc_data[MCP1][0]) < dataWindowMuonDecay) return SUCCESS;
201 
202  // check, if there was no 2nd e+ during data window
203  if ( tdc_data[POSMCP1I][1] != 0 ){
204  if ( (tdc_data[POSMCP1I][1] - tdc_data[MCP1][0]) < dataWindowMuonDecay &&
205  abs(tdc_data[POSMCP1I][1]-tdc_data[POSMCP1O][1]) < 50 ) return SUCCESS; //2nd e+
206  }
207  hMCP1Hist[2]->Fill( (float) tdc_data[POSMCP1I][0] - tdc_data[MCP1][0], 1.);
208  }
209  return SUCCESS;
210 }
211 /* ----------------------------------------------------------------------------*\
212  EOF mcp1_ana_module.c
213 \* ----------------------------------------------------------------------------*/
#define BCR1
Definition: nemu_experim.h:22
INT mcp1_init(void)
WORD channel
Definition: nemu_experim.h:93
float histoxlow[3]
Definition: experim.h:416
UINT32 tof_bc_window
Definition: experim.h:694
#define BCR4
Definition: nemu_experim.h:25
#define BCL4
Definition: nemu_experim.h:21
ANA_MODULE mcp1_ana_module
struct MCP1ANAMODULE_PARAM::@14 histotitles
INT mcp1_bor(INT run_number)
#define POSMCP1O
Definition: nemu_experim.h:27
char runname[256]
Definition: analyzer.cxx:79
MCP1ANAMODULE_PARAM_STR(mcp1_ana_param_str)
#define N_MCP1HIST
char toftitles[3][32]
Definition: experim.h:412
static DWORD dataWindowMuonDecay
float histoxup[3]
Definition: experim.h:417
#define BCL1
Definition: nemu_experim.h:18
INT mcp1_eor(INT run_number)
#define BCL3
Definition: nemu_experim.h:20
TObjArray * gHistoFolderStack
MCP1ANAMODULE_PARAM mcp1_ana_param
TRIGGER_SETTINGS trigger_settings
Definition: analyzer.cxx:93
INT mcp1_ana(EVENT_HEADER *, void *)
RUNINFO runinfo
Definition: analyzer.cxx:83
#define BCR2
Definition: nemu_experim.h:23
struct MCP1ANAMODULE_PARAM::@15 histobinning
UINT32 data_window
Definition: experim.h:666
#define POSMCP1I
Definition: nemu_experim.h:26
#define DATA_N_HITS
max. of DATA_N_HITS in data file
Definition: nemu_experim.h:66
static TH1F * hMCP1Hist[N_MCP1HIST]
TFolder * gManaHistosFolder
#define BCR3
Definition: nemu_experim.h:24
INT32 histonbin[3]
Definition: experim.h:415
#define NEW_EVENT_MASK
Definition: nemu_experim.h:67
#define N_TDC_CHANNELS
number of TDC channels
Definition: nemu_experim.h:64
#define BCL2
Definition: nemu_experim.h:19
DWORD data
Definition: nemu_experim.h:92
HNDLE hDB
#define MCP1
Definition: nemu_experim.h:16
struct TRIGGER_SETTINGS::@18 event_0_settings
#define EVENT_0_TYPE
EVENT type 0 = BC-MCP1-(e+)
Definition: nemu_experim.h:75