1 #define _FILE_OFFSET_BITS 64
15 #include "../../compress/mucap_compress.h"
25 void process_file(
char *input_filename,
int addr,
int fadc);
31 int event_size, size_read;
34 size_read = fread(pevent, 1,
sizeof(EVENT_HEADER), fp);
35 if (size_read !=
sizeof(EVENT_HEADER))
38 header = (EVENT_HEADER *) pevent;
40 if (event_number == 0 && header->trigger_mask != MIDAS_MAGIC) {
41 fprintf(stderr,
"Input is not a valid MIDAS file\n");
45 event_size = header->data_size;
47 if ((
unsigned int) event_size >= MAX_EVENT_SIZE -
sizeof(EVENT_HEADER)) {
48 fprintf(stderr,
"Invalid event size %d\n", event_size);
52 size_read = fread(pevent +
sizeof(EVENT_HEADER), 1, event_size, fp);
53 if (size_read != event_size)
73 hSamples =
new TH2D(
"samples",
"Sample profile", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
74 hPed0 =
new TH2D(
"ped0",
"Pedestal from first samples", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
75 hChannelNSample =
new TH1D(
"channelNSample",
"Number of samples vs. channel", 8, -0.5, 7.5);
76 hChannelNIsland =
new TH1D(
"channelNIsland",
"Number of islands vs. channel", 8, -0.5, 7.5);
77 hMinSample =
new TH2D(
"minSample",
"Minimum sample in island", 8, -0.5, 7.5, 8192, -0.5, 8191.5);
78 hMaxSample =
new TH2D(
"maxSample",
"Maximum sample in island", 8, -0.5, 7.5, 8192, -0.5, 8191.5);
79 hPulseEnergy =
new TH2D(
"pulseEnergy",
"Reconstructed energy of pulse", 8, -0.5, 7.5, 4000, 0.5, 12000.5);
83 cm_connect_experiment(host_name, exp_name,
"show_pulses", NULL);
84 cm_get_experiment_database(&
hDB, NULL);
88 input_fp = fopen(input_filename,
"rb");
90 fprintf(stderr,
"Unable to open %s: ", input_filename);
96 input_event = (
char *) malloc(MAX_EVENT_SIZE*2);
102 EVENT_HEADER *header = (EVENT_HEADER *) input_event;
104 if(header->event_id == 1) {
117 int sig_length_counter = 0;
118 double sig_max_height = 0.;
119 int sig_first_index = 0;
120 double sig_integral = 0.;
121 int sig_over_thr = 0;
123 bool hasOvershoot =
false;
124 bool hasUndershoot =
false;
125 const int kLengthThreshold = 4;
129 if(nsamples < 40 )
return 0.;
137 const int pedSamples = 10;
138 double pedestalOffset = 0;
139 double sumpedestal2 = 0;
142 if(pedSamples > nsamples)
return 0.;
144 for(
int i = 0;
i < pedSamples;
i++) {
145 pedestalOffset += samples[
i];
147 pedestalOffset /= pedSamples;
149 for(
int i = 0;
i < pedSamples;
i++) {
150 sumpedestal2 += (pedestalOffset-samples[
i])*(pedestalOffset-samples[
i]);
152 pedestalsigma= sqrt(sumpedestal2/pedSamples);
163 double bin_content = -1.*((double)samples[
i] - pedestalOffset);
165 double next_bin_content = 0.;
166 if(
i<nsamples-1) next_bin_content = -1.*((double)samples[
i+1] - pedestalOffset);
168 if(bin_content >= kThreshold || next_bin_content >= kThreshold){
169 sig_length_counter++;
172 if (sig_length_counter == 1){
176 for(back=
i-1; back>=0; back--){
177 double cont = -1.*((double)samples[back] - pedestalOffset);
182 if(cont > 3*kThreshold/4 && (
i-back)<4){
184 sig_length_counter++;
185 sig_first_index = back;
192 if (((bin_content < kThreshold) || (
i == nsamples - 1)) &&
193 (sig_over_thr >= kLengthThreshold)){
198 double cont = -1.*((double)samples[forw] - pedestalOffset);
199 if(cont > kThreshold/4 && (forw-
i)<4){
201 sig_length_counter++;
210 for(
int i = sig_first_index;
i < sig_first_index+sig_length_counter;
i++){
211 double bin_content = -1.*((double)samples[
i] - pedestalOffset);
212 sig_integral += bin_content;
213 if (bin_content > sig_max_height){
214 sig_max_height = bin_content;
234 if(nsamples < 16)
return 0;
238 int minSample = 4097;;
241 double energy =
findPulse(nsamples, samples);
246 if(samples[i] > maxSample) {
248 maxSample = samples[
i];
250 if(samples[i] < minSample) {
252 minSample = samples[
i];
255 hPed0->Fill(channel, samples[i]);
268 for(
int i = 0;
i < 8;
i++) {
270 if(
i != fadc && fadc >= 0)
continue;
274 sprintf(name,
"N%c%02x",
'a' +
i, addr);
276 int bank_size = bk_locate(pevent, name, &raw);
277 bank_size = bank_size / 10;
280 printf(
"-------------------------------------------------------------------------------\n");
283 int islandSamples[8192];
284 int islandNsamples = 0;
286 bool firstIsland =
true;
289 int lastTimestamp = 0;
290 for(
int j = 0; j < bank_size; j++) {
301 int timestamp = (raw[j*10+0] << 20) |
302 (raw[j*10+1] << 12) |
305 bool overflowB0 = ((raw[j*10+3] & 0x08) != 0);
306 bool overflowA0 = ((raw[j*10+3] & 0x04) != 0);
307 bool overflowB1 = ((raw[j*10+3] & 0x02) != 0);
308 bool overflowA1 = ((raw[j*10+3] & 0x01) != 0);
309 int sampleB0 = (overflowB0 << 12) |
312 int sampleA0 = (overflowA0 << 12) |
313 ((raw[j*10+5] & 0xf) << 8) |
315 int sampleB1 = (overflowB1 << 12) |
318 int sampleA1 = (overflowA1 << 12) |
319 ((raw[j*10+8] & 0xf) << 8) |
322 if(timestamp < lastTimestamp) {
323 printf(
"Time ordering error!\n");
326 if(timestamp != lastTimestamp + 1) {
328 int stop =
processIsland(islandT0*2, islandNsamples, islandSamples,
i);
337 islandT0 = timestamp;
341 lastTimestamp = timestamp;
344 printf(
"fadc=%d t=%d %d %d %d %d\n",
i, timestamp,
345 sampleA1, sampleB1, sampleA0,sampleB0);
348 islandSamples[islandNsamples++] = sampleA1;
349 islandSamples[islandNsamples++] = sampleB1;
350 islandSamples[islandNsamples++] = sampleA0;
351 islandSamples[islandNsamples++] = sampleB0;
354 if(islandNsamples != 0) {