13 #include "../../compress/mucap_compress.h"
23 void process_file(
char *input_filename,
int addr,
int fadc);
29 int event_size, size_read;
32 size_read = fread(pevent, 1,
sizeof(EVENT_HEADER), fp);
33 if (size_read !=
sizeof(EVENT_HEADER))
36 header = (EVENT_HEADER *) pevent;
38 if (event_number == 0 && header->trigger_mask != MIDAS_MAGIC) {
39 fprintf(stderr,
"Input is not a valid MIDAS file\n");
43 event_size = header->data_size;
45 if ((
unsigned int) event_size >= MAX_EVENT_SIZE -
sizeof(EVENT_HEADER)) {
46 fprintf(stderr,
"Invalid event size %d\n", event_size);
50 size_read = fread(pevent +
sizeof(EVENT_HEADER), 1, event_size, fp);
51 if (size_read != event_size)
69 void process_file(
char *input_filename,
int addr,
int fadc)
75 hSamples =
new TH2D(
"samples",
"Sample profile", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
76 hPed0 =
new TH2D(
"ped0",
"Pedestal from first samples", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
77 hPulseIF =
new TH1D(
"PulseIF",
"Pulse initial and final samples", 1000, -0.5, 1000.5);
78 hPulseIvsEnergy =
new TH2D(
"PulseIvsEnergy",
"Pulse start position vs Energy", 4000, 0.5, 40000.5,1000, -0.5, 1000.5);
79 hPulseFvsEnergy =
new TH2D(
"PulseFvsEnergy",
"Pulse end position vs Energy", 4000, 0.5, 40000.5,1000, -0.5, 1000.5);
80 hPulseAvsEnergy =
new TH2D(
"PulseAvsEnergy",
"Pulse Amplitude vs Energy", 4000, 0.5, 40000.5,4096, -0.5, 4095.5);
81 hChannelNSample =
new TH1D(
"channelNSample",
"Number of samples vs. channel", 8, -0.5, 7.5);
82 hChannelNIsland =
new TH1D(
"channelNIsland",
"Number of islands vs. channel", 8, -0.5, 7.5);
83 hMinSample =
new TH2D(
"minSample",
"Minimum sample in island", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
84 hMaxSample =
new TH2D(
"maxSample",
"Maximum sample in island", 8, -0.5, 7.5, 4096, -0.5, 4095.5);
85 hPulseEnergy =
new TH2D(
"pulseEnergy",
"Reconstructed energy of pulse", 8, -0.5, 7.5, 4000, 0.5, 40000.5);
89 cm_connect_experiment(host_name, exp_name,
"show_pulses", NULL);
90 cm_get_experiment_database(&
hDB, NULL);
94 input_fp = fopen(input_filename,
"rb");
96 fprintf(stderr,
"Unable to open %s: ", input_filename);
102 input_event = (
char *) malloc(MAX_EVENT_SIZE*2);
108 EVENT_HEADER *header = (EVENT_HEADER *) input_event;
110 if(header->event_id == 1) {
123 int sig_length_counter = 0;
124 double sig_max_height = 0.;
125 int sig_first_index = 0;
126 double sig_integral = 0.;
127 int sig_over_thr = 0;
129 bool hasOvershoot =
false;
130 bool hasUndershoot =
false;
131 const int kLengthThreshold = 4;
135 if(nsamples < 40 )
return 0.;
143 const int pedSamples = 10;
144 double pedestalOffset = 0;
145 double sumpedestal2 = 0;
148 if(pedSamples > nsamples)
return 0.;
150 for(
int i = 0;
i < pedSamples;
i++) {
151 pedestalOffset += samples[
i];
153 pedestalOffset /= pedSamples;
155 for(
int i = 0;
i < pedSamples;
i++) {
156 sumpedestal2 += (pedestalOffset-samples[
i])*(pedestalOffset-samples[
i]);
158 pedestalsigma= sqrt(sumpedestal2/pedSamples);
169 double bin_content = -1.*((double)samples[
i] - pedestalOffset);
171 double next_bin_content = 0.;
172 if(
i<nsamples-1) next_bin_content = -1.*((double)samples[
i+1] - pedestalOffset);
174 if(bin_content >= kThreshold || next_bin_content >= kThreshold){
175 sig_length_counter++;
178 if (sig_length_counter == 1){
182 for(back=
i-1; back>=0; back--){
183 double cont = -1.*((double)samples[back] - pedestalOffset);
188 if(cont > 3*kThreshold/4 && (
i-back)<4){
190 sig_length_counter++;
191 sig_first_index = back;
198 if (((bin_content < kThreshold) || (
i == nsamples - 1)) &&
199 (sig_over_thr >= kLengthThreshold)){
204 double cont = -1.*((double)samples[forw] - pedestalOffset);
205 if(cont > 3*kThreshold/4 && (forw-
i)<4){
207 sig_length_counter++;
216 for(
int i = sig_first_index;
i < sig_first_index+sig_length_counter;
i++){
217 double bin_content = -1.*((double)samples[
i] - pedestalOffset);
218 sig_integral += bin_content;
219 if (bin_content > sig_max_height){
220 sig_max_height = bin_content;
233 hPulseIF->Fill(sig_first_index+sig_length_counter);
236 hPulseFvsEnergy->Fill(sig_integral, sig_first_index+sig_length_counter);
244 if(nsamples < 40)
return 0;
248 int minSample = 4097;;
251 double energy =
findPulse(nsamples, samples);
256 if(samples[i] > maxSample) {
258 maxSample = samples[
i];
260 if(samples[i] < minSample) {
262 minSample = samples[
i];
265 hPed0->Fill(channel, samples[i]);
278 for(
int i = 0;
i < 8;
i++) {
280 if(
i != fadc && fadc >= 0)
continue;
284 sprintf(name,
"N%c%02x",
'a' +
i, addr);
286 int bank_size = bk_locate(pevent, name, &raw);
287 bank_size = bank_size / 10;
290 printf(
"-------------------------------------------------------------------------------\n");
293 int islandSamples[8192];
294 int islandNsamples = 0;
296 bool firstIsland =
true;
299 int lastTimestamp = 0;
300 for(
int j = 0; j < bank_size; j++) {
311 int timestamp = (raw[j*10+0] << 20) |
312 (raw[j*10+1] << 12) |
315 bool overflowB0 = ((raw[j*10+3] & 0x08) != 0);
316 bool overflowA0 = ((raw[j*10+3] & 0x04) != 0);
317 bool overflowB1 = ((raw[j*10+3] & 0x02) != 0);
318 bool overflowA1 = ((raw[j*10+3] & 0x01) != 0);
319 int sampleB0 = (overflowB0 << 12) |
322 int sampleA0 = (overflowA0 << 12) |
323 ((raw[j*10+5] & 0xf) << 8) |
325 int sampleB1 = (overflowB1 << 12) |
328 int sampleA1 = (overflowA1 << 12) |
329 ((raw[j*10+8] & 0xf) << 8) |
332 if(timestamp < lastTimestamp) {
333 printf(
"Time ordering error!\n");
336 if(timestamp != lastTimestamp + 1) {
338 int stop =
processIsland(islandT0*2, islandNsamples, islandSamples,
i);
347 islandT0 = timestamp;
351 lastTimestamp = timestamp;
354 printf(
"fadc=%d t=%d %d %d %d %d\n",
i, timestamp,
355 sampleA1, sampleB1, sampleA0,sampleB0);
358 islandSamples[islandNsamples++] = sampleA1;
359 islandSamples[islandNsamples++] = sampleB1;
360 islandSamples[islandNsamples++] = sampleA0;
361 islandSamples[islandNsamples++] = sampleB0;
364 if(islandNsamples != 0) {