00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <stdint.h>
00016 #include <string>
00017 #include <map>
00018 #include <utility>
00019 #include <algorithm>
00020
00021
00022 #include "midas.h"
00023
00024
00025 #include <TH1.h>
00026 #include <TH2.h>
00027 #include "TRandom3.h"
00028
00029
00030
00031 #include "TGlobalData.h"
00032 #include "TSetupData.h"
00033
00034 using std::string;
00035 using std::map;
00036 using std::vector;
00037 using std::pair;
00038
00039
00040 static INT module_init(void);
00041 static INT module_event_caen(EVENT_HEADER*, void*);
00042
00043 extern HNDLE hDB;
00044 extern TGlobalData* gData;
00045 extern TSetupData* gSetup;
00046
00049 static vector<string> caen_houston_bank_names;
00056 static unsigned int nPreSamples;
00059 static const int NCHAN = 8;
00060
00061
00062 ANA_MODULE MV1724ProcessRaw_module =
00063 {
00064 "MV1724ProcessRaw",
00065 "Vladimir Tishchenko",
00066 module_event_caen,
00067 NULL,
00068 NULL,
00069 module_init,
00070 NULL,
00071 NULL,
00072 0,
00073 NULL,
00074 };
00075
00076
00077 INT module_init()
00078 {
00079
00080 std::map<std::string, std::string> bank_to_detector_map = gSetup->fBankToDetectorMap;
00081 for(std::map<std::string, std::string>::iterator mapIter = bank_to_detector_map.begin();
00082 mapIter != bank_to_detector_map.end(); mapIter++) {
00083
00084 std::string bankname = mapIter->first;
00085
00086
00087 if (TSetupData::IsHoustonCAEN(bankname))
00088 caen_houston_bank_names.push_back(bankname);
00089 }
00090
00091
00092 char key[80];
00093 int size;
00094 unsigned int post_trigger_percentage, nSamples;
00095
00096
00097
00098 sprintf(key, "/Equipment/Crate 4/Settings/CAEN0/waveform length");
00099 size = sizeof(nSamples);
00100 db_get_value(hDB, 0, key, &nSamples, &size, TID_DWORD, 1);
00101 post_trigger_percentage = 80;
00102 nPreSamples = (int) (0.01 * ((100 - post_trigger_percentage) * nSamples));
00103
00104 return SUCCESS;
00105 }
00106
00107
00108 INT module_event_caen(EVENT_HEADER *pheader, void *pevent)
00109 {
00110
00111
00112 typedef map<string, vector<TPulseIsland*> > TStringPulseIslandMap;
00113 typedef pair<string, vector<TPulseIsland*> > TStringPulseIslandPair;
00114 typedef map<string, vector<TPulseIsland*> >::iterator map_iterator;
00115
00116
00117
00118
00119
00120 TStringPulseIslandMap& pulse_islands_map =
00121 gData->fPulseIslandToChannelMap;
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 int midas_event_number = pheader->serial_number;
00140
00141 BYTE *pdata;
00142
00143
00144
00145 char bank_name[8];
00146 sprintf(bank_name,"CDG%i",0);
00147 unsigned int bank_len = bk_locate(pevent, bank_name, &pdata);
00148
00149
00150 uint32_t *p32 = (uint32_t*)pdata;
00151 uint32_t *p32_0 = (uint32_t*)pdata;
00152
00153
00154 while ( (p32 - p32_0)*4 < bank_len )
00155 {
00156
00157 uint32_t caen_event_cw = p32[0]>>28;
00158
00159 if ( caen_event_cw != 0xA )
00160 {
00161 printf("***ERROR UH CAEN! Wrong data format: incorrect control word 0x%08x\n", caen_event_cw);
00162 return SUCCESS;
00163 }
00164
00165 uint32_t caen_event_size = p32[0] & 0x0FFFFFFF;
00166
00167
00168 uint32_t caen_channel_mask = p32[1] & 0x000000FF;
00169
00170 int nchannels = 0;
00171 for (int ichannel=0; ichannel<NCHAN; ichannel++ )
00172 {
00173 if ( caen_channel_mask & (1<<ichannel) ) nchannels++;
00174 }
00175
00176 uint32_t caen_event_counter = p32[2] & 0x00FFFFFF;
00177
00178
00179 uint32_t caen_trigger_time = p32[3];
00180
00181
00182
00183 unsigned int nsamples = ((caen_event_size-4)*2) / nchannels;
00184
00185
00186
00187 for (std::vector<std::string>::iterator bankNameIter = caen_houston_bank_names.begin();
00188 bankNameIter != caen_houston_bank_names.end(); bankNameIter++) {
00189
00190 vector<TPulseIsland*>& pulse_islands = pulse_islands_map[*(bankNameIter)];
00191 std::vector<int> sample_vector;
00192 int ichannel = bankNameIter - caen_houston_bank_names.begin();
00193
00194
00195
00196
00197
00198 if ( caen_channel_mask & (1<<ichannel) )
00199 {
00200
00201 unsigned int isample = 0;
00202 unsigned int nwords = nsamples/2;
00203
00204 for (int iword=0; iword<nwords; iword++)
00205 {
00206
00207 for (int isubword=0; isubword<2; isubword++)
00208 {
00209 uint32_t adc;
00210
00211 if (isubword == 0 )
00212 adc = (p32[4+iword+ichannel*nwords] & 0x3fff);
00213 else
00214 adc = ((p32[4+iword+ichannel*nwords] >> 16) & 0x3fff);
00215
00216
00217
00218 isample++;
00219
00220 sample_vector.push_back(adc);
00221
00222 }
00223 }
00224
00225 pulse_islands.push_back(new TPulseIsland(caen_trigger_time - nPreSamples, sample_vector, *bankNameIter));
00226 }
00227 }
00228
00229
00230 p32 += caen_event_size + (caen_event_size%2);
00231
00232
00233 }
00234
00235
00236 if(midas_event_number == 1) {
00237
00238 for (std::vector<std::string>::iterator bankNameIter = caen_houston_bank_names.begin();
00239 bankNameIter != caen_houston_bank_names.end(); bankNameIter++) {
00240
00241 vector<TPulseIsland*>& pulse_islands = pulse_islands_map[*(bankNameIter)];
00242 printf("TEST MESSAGE: Read %d events from bank %s in event %d\n",
00243 pulse_islands.size(),
00244 (*(bankNameIter)).c_str(),
00245 midas_event_number);
00246 }
00247 }
00248
00249 return SUCCESS;
00250 }
00251