AlcapDAQ  1
MMuSCAnalysisC.cpp
Go to the documentation of this file.
1 /********************************************************************\
2 
3  Name: MMuSCAnalysisC.cpp
4  Created by: Tom Banks
5 
6  Contents: Generates muSC histograms, and extracts important
7  offset information from the ODB.
8 
9 \********************************************************************/
10 
11 /*-- Include files -------------------------------------------------*/
12 
13 /* Standard includes */
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 /* MIDAS includes */
18 #include "midas.h"
19 
20 /* ROOT includes */
21 #include "TH1.h"
22 #include "TDirectory.h"
23 
24 /* MuCap includes */
25 #include "common.h"
26 #include "ucb_common.h"
27 #include "uiuc_utils.h"
28 
29 extern HNDLE hDB;
30 extern TDirectory *gManaHistsDir;
31 
32 /*-- Parameters ----------------------------------------------------*/
33 
37 
38 /*-- Static parameters ---------------------------------------------*/
39 
40 // Declare histograms
41 static TH1 *muSC_time;
46 
47 /*-- Module declaration --------------------------------------------*/
48 
49 INT MMuSCAnalysisC_init(void);
50 INT MMuSCAnalysisC(EVENT_HEADER*, void*);
51 
52 ANA_MODULE MMuSCAnalysisC_module = {
53  "MMuSCAnalysisC", /* module name */
54  "Tom Banks", /* author */
55  MMuSCAnalysisC, /* event routine */
56  NULL, /* BOR routine */
57  NULL, /* EOR routine */
58  MMuSCAnalysisC_init, /* init routine */
59  NULL, /* exit routine */
60  NULL, /* parameter structure */
61  0, /* structure size */
62  NULL, /* initial parameters */
63 };
64 
65 /*-- init routine --------------------------------------------------*/
66 
68 {
69  /* Read time offsets for the various copies of the muSC from the ODB */
70  char key_name[80];
71  int size,runnumber;
72  sprintf(key_name, "Runinfo/Run number");
73  size=sizeof(int);
74  db_get_value(hDB,0,key_name, &runnumber, &size, TID_INT,1);
75  // printf("==================\n\n\n in MMuSCAnalysisC_init\n runnumber is %d\n\n=============\n",runnumber);
76  int PSI_runnumber=1;
77  char stem[200];
78  sprintf(stem,"/Experiment/Run%d",PSI_runnumber);
79  printf("==================\n\n\n in MMuSCAnalysisC_init\n PSI runnumber is %d\n\n=============\n",PSI_runnumber);
80  sprintf(key_name, "%s/muSC/muSCCopy2TimeOffset",stem);
81  size = sizeof(muSCCopy2TimeOffset);
82  db_get_value(hDB, 0, key_name, &muSCCopy2TimeOffset, &size, TID_DOUBLE, 1);
83 
84  sprintf(key_name, "%s/muSC/muSCATimeOffset",stem);
85  size = sizeof(muSCATimeOffset);
86  db_get_value(hDB, 0, key_name, &muSCATimeOffset, &size, TID_DOUBLE, 1);
87 
88  sprintf(key_name, "%s/muSC/muSCRoutedTimeOffset",stem);
89  size = sizeof(muSCRoutedTimeOffset);
90  db_get_value(hDB, 0, key_name, &muSCRoutedTimeOffset, &size, TID_DOUBLE, 1);
91 
92  /****** Construct histograms ******/
93  char hist_name[256];
94  char hist_title[256];
95 
96  // muSC successive hits tdiff histogram
97  sprintf(hist_name, "muSC_copy1_successive_hit_tdiffs");
98  sprintf(hist_title, "#muSC tdiffs between successive hits");
99  muSC_copy1_successive_hit_tdiffs = new TH1D(hist_name, hist_title, 44201, -5000.625, 50250.625);
100 
101  // muSC time histogram
102  sprintf(hist_name, "muSC_time");
103  sprintf(hist_title, "muSC time");
104  muSC_time = new TH1D(hist_name, hist_title, 44201, -5000.625, 50250.625);
105 
106  // muSC successive hits tdiff histogram, after artificial deadtime (AD)
107  sprintf(hist_name, "muSC_copy1_AD_successive_hit_tdiffs");
108  sprintf(hist_title, "#muSC tdiffs between successive AD hits");
109  muSC_copy1_AD_successive_hit_tdiffs = new TH1D(hist_name, hist_title, 44201, -5000.625, 50250.625);
110 
111  // muSC router sequence error tracking
112  sprintf(hist_name, "muSC_router_seq_errors");
113  sprintf(hist_title, "#muSC router sequence errors");
114  muSC_router_seq_errors = new TH1D(hist_name, hist_title, 4, -0.5, 3.5);
115 
116  // Blocks skipped due to muSC copy1/copy2 count discrepancies
117  sprintf(hist_name, "blocks_skipped_muSC_matching_errors");
118  sprintf(hist_title, "blocks skipped due to #muSC matching errors");
119  muSC_error_blocks_skipped = new TH1D(hist_name, hist_title, 3, -0.5, 2.5);
120 
121  return SUCCESS;
122 }
123 
124 /*-- event routine -------------------------------------------------*/
125 
126 INT MMuSCAnalysisC(EVENT_HEADER *pheader, void *pevent)
127 {
128  /* ############################################### */
129  // Open up the "HITS" bank
130  channel_hit *hit_bank;
131  int hit_bank_size = bk_locate(pevent, "HITS", (DWORD *) &hit_bank);
132  hit_bank_size = hit_bank_size * sizeof(DWORD) / sizeof(channel_hit);
133  if (hit_bank == NULL) {
134  fprintf(stderr, "MMuSCAnalysisC: Could not find bank HITS.\n");
135  }
136 
137  /* ############################################### */
138  // MuSC arrival histograms, with and w/o AD.
139  int prevMuSChitindex = -1;
140  int prevMuSCADhitindex = -1;
141  for (int i=0; i<hit_bank_size; i++) {
142  if (hit_bank[i].parameter == 6001) {
143  muSC_time->Fill(hit_bank[i].time);
144  // Successive tdiffs between muSC copy1 (parameter 6001) hits
145  if (prevMuSChitindex >= 0) {
146  double tdiff = hit_bank[i].time -
147  hit_bank[prevMuSChitindex].time;
149  }
150  prevMuSChitindex = i;
151 
152  // Successive tdiffs between AD muSC hits
153  if (prevMuSCADhitindex >= 0) {
154  double tdiff = hit_bank[i].time -
155  hit_bank[prevMuSCADhitindex].time;
156  if (tdiff > kMuSCADInterval) {
158  prevMuSCADhitindex = i;
159  }
160  }
161  if (prevMuSCADhitindex == -1) {
162  prevMuSCADhitindex = i;
163  }
164 
165  }
166  }
167 
168  /* ############################################### */
169 
170  // check that the routed muSC always alternates 1-2-3-4
171  int last_route_port = -1;
172  double last_route_time = -1;
173  for (int i=0; i<hit_bank_size; i++) {
174  if (hit_bank[i].parameter >= 6006 && hit_bank[i].parameter <= 6009) {
175  int port = hit_bank[i].parameter - 6006;
176  if(last_route_port != -1 && port != (last_route_port+1)%4) {
177  muSC_router_seq_errors->Fill(port);
178  }
179  last_route_port = port;
180  last_route_time = hit_bank[i].time;
181  }
182  }
183 
184  /* ############################################### */
185 
186  // look for too many mismatched muSC copy 1 vs. copy 2 hits
187  bool skip = false;
188  for(int i = 1; i <= 2; i++) {
189  char name[80];
190  sprintf(name, "muSC_copy%d_only_block", i);
191  TH1 *only = (TH1 *) gManaHistsDir->Get(name);
192  if(only == NULL) {
193  fprintf(stderr, "Warning: Unable to find histogram %s\n", name);
194  continue;
195  }
196  int bin = only->GetXaxis()->FindBin(pheader->serial_number);
197  if(only->GetBinContent(bin) > kMuSCMismatchThreshold+0.5) {
198  fprintf(stderr, "Skipping block %d: too many hits in muSC copy %d only (%f)\n",
199  pheader->serial_number, i, only->GetBinContent(bin));
200  skip = true;
201  }
202  }
203 
204  if(skip) {
205  muSC_error_blocks_skipped->Fill(1);
206  return ANA_SKIP;
207  } else {
208  return SUCCESS;
209  }
210 }