1 | #include <TROOT.h>
|
---|
2 | #include <TCanvas.h>
|
---|
3 | #include <TProfile.h>
|
---|
4 | #include <TTimer.h>
|
---|
5 | #include <TH1F.h>
|
---|
6 | #include <TH2F.h>
|
---|
7 | #include <Getline.h>
|
---|
8 | #include <TLine.h>
|
---|
9 | #include <TMath.h>
|
---|
10 | #include <TFile.h>
|
---|
11 |
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <stdint.h>
|
---|
14 | #include <cstdio>
|
---|
15 |
|
---|
16 | #define HAVE_ZLIB
|
---|
17 | #include "fits.h"
|
---|
18 | #include "FOpenCalibFile.c"
|
---|
19 |
|
---|
20 | #include "zerosearch.C"
|
---|
21 | #include "factfir.C"
|
---|
22 |
|
---|
23 | #define NPIX 1440
|
---|
24 | #define NCELL 1024
|
---|
25 |
|
---|
26 | // data access and treatment
|
---|
27 | #define FAD_MAX_SAMPLES 1024
|
---|
28 |
|
---|
29 | //vector<int16_t> data;
|
---|
30 | //vector<int16_t> data_offset;
|
---|
31 | //unsigned int data_num;
|
---|
32 | //size_t data_n;
|
---|
33 | //UInt_t data_px;
|
---|
34 | //UInt_t data_roi;
|
---|
35 | vector<int16_t> Data; // vector, which will be filled with raw data
|
---|
36 | vector<int16_t> StartCells; // vector, which will be filled with DRS start positions
|
---|
37 | unsigned int EventID; // index of the current event
|
---|
38 | UInt_t RegionOfInterest; // Width of the Region, read out of the DRS
|
---|
39 | UInt_t NumberOfPixels; // Total number of pixel, read out of the camera
|
---|
40 | size_t PXLxROI; // Size of column "Data" = #Pixel x ROI
|
---|
41 |
|
---|
42 | int NEvents;
|
---|
43 | int NBSLeve = 1000;
|
---|
44 |
|
---|
45 | size_t drs_n;
|
---|
46 | vector<float> drs_basemean;
|
---|
47 | vector<float> drs_gainmean;
|
---|
48 | vector<float> drs_triggeroffsetmean;
|
---|
49 |
|
---|
50 | size_t FOpenDataFile(
|
---|
51 | const char *datafilename, // path to fits file containing FACT raw data
|
---|
52 | fits * * datafile, // pointer to pointer, where to return the fits object
|
---|
53 | vector<int16_t> &Data, // vector, which will be filled with raw data
|
---|
54 | vector<int16_t> &StartCells, // vector, which will be filled with DRS start positions
|
---|
55 | unsigned int &EventID, // index of the current event
|
---|
56 | UInt_t &RegionOfInterest, // Width of the Region, read out of the DRS
|
---|
57 | UInt_t &NumberOfPixels, // Total number of pixel, read out of the camera
|
---|
58 | size_t &PXLxROI, // Size of column "Data" = #Pixel x ROI
|
---|
59 | // this can be used, to x-check RegionOfInterest and NumberOfPixels
|
---|
60 | int VerbosityLevel=1
|
---|
61 | );
|
---|
62 |
|
---|
63 |
|
---|
64 | vector<float> Ameas(FAD_MAX_SAMPLES); // copy of the data (measured amplitude
|
---|
65 | vector<float> N1mean(FAD_MAX_SAMPLES); // mean of the +1 -1 ch neighbors
|
---|
66 | vector<float> N2mean(FAD_MAX_SAMPLES); // mean of the +2 -2 ch neighbors
|
---|
67 | vector<float> Vcorr(FAD_MAX_SAMPLES); // corrected Values
|
---|
68 | vector<float> Vdiff(FAD_MAX_SAMPLES); // numerical derivative
|
---|
69 |
|
---|
70 | vector<float> Vslide(FAD_MAX_SAMPLES); // sliding average result
|
---|
71 | vector<float> Vcfd(FAD_MAX_SAMPLES); // CDF result
|
---|
72 | vector<float> Vcfd2(FAD_MAX_SAMPLES); // CDF result + 2nd sliding average
|
---|
73 |
|
---|
74 |
|
---|
75 | float getValue( int, int );
|
---|
76 | void computeN1mean( int );
|
---|
77 | void removeSpikes( int );
|
---|
78 |
|
---|
79 | // histograms
|
---|
80 | const int Ntypes = 7;
|
---|
81 | const unsigned int // arranged by Dominik
|
---|
82 | tAmeas = 0,
|
---|
83 | tN1mean = 1,
|
---|
84 | tVcorr = 2,
|
---|
85 | tVtest = 3,
|
---|
86 | tVslide = 4,
|
---|
87 | tVcfd = 5,
|
---|
88 | tVcfd2 = 6;
|
---|
89 |
|
---|
90 | TH1F* h;
|
---|
91 | TH2F hPixelCellData(
|
---|
92 | "PixelPedestal", "PixelPedestal", NCELL, 0., NCELL, 200, -50., 150.);
|
---|
93 | TH1F *hBaseline[ NPIX ]; // histograms for baseline extraction
|
---|
94 | TH1F *hMeanBsl, *hpltMeanBsl;
|
---|
95 | TH1F *hRmsBsl, *hpltRmsBsl;
|
---|
96 | TObjArray hList;
|
---|
97 | TObjArray hListBaseline;
|
---|
98 |
|
---|
99 | void BookHistos( int , int);
|
---|
100 | void SaveHistograms( const char * );
|
---|
101 |
|
---|
102 | // Create a canvas
|
---|
103 | TCanvas* CW;
|
---|
104 | TCanvas* cFilter;
|
---|
105 |
|
---|
106 | int spikeDebug = 0;
|
---|
107 | int searchSinglesPeaks = 0;
|
---|
108 |
|
---|
109 |
|
---|
110 | int fbsl(
|
---|
111 | const char *datafilename = "path-to-datafile.fits.gz",
|
---|
112 | const char *drsfilename = "path-to-calibfile.drs.fits.gz",
|
---|
113 | const char *TextOutFileName = "./appendfile.txt",
|
---|
114 | const char *RootOutFileName = "./datafile.root",
|
---|
115 | int firstevent = 0,
|
---|
116 | int nevents = -1,
|
---|
117 | int firstpixel = 0,
|
---|
118 | int npixel = -1,
|
---|
119 | bool produceGraphic = false
|
---|
120 | ){
|
---|
121 | fits * datafile = NULL;
|
---|
122 | NEvents = FOpenDataFile(
|
---|
123 | datafilename,
|
---|
124 | &datafile,
|
---|
125 | Data,
|
---|
126 | StartCells,
|
---|
127 | EventID,
|
---|
128 | RegionOfInterest,
|
---|
129 | NumberOfPixels,
|
---|
130 | PXLxROI);
|
---|
131 |
|
---|
132 | printf("number of events in file: %d\n", NEvents);
|
---|
133 |
|
---|
134 | // compare the number of events in the data file with the nevents the
|
---|
135 | // the user would like to read. nevents = -1 means: read all
|
---|
136 | if ( nevents == -1 || nevents > NEvents ) nevents = NEvents;
|
---|
137 |
|
---|
138 | if ( npixel == -1 || npixel > (int)NumberOfPixels) npixel = NumberOfPixels;
|
---|
139 |
|
---|
140 | FOpenCalibFile(drsfilename, drs_basemean, drs_gainmean, drs_triggeroffsetmean, drs_n);
|
---|
141 |
|
---|
142 | BookHistos( RegionOfInterest, npixel );
|
---|
143 |
|
---|
144 | // loop over events
|
---|
145 | for ( int ev = firstevent; ev < firstevent + nevents; ev++) {
|
---|
146 |
|
---|
147 | datafile->GetRow( ev );
|
---|
148 |
|
---|
149 | if ( ev % 100 == 0 ){
|
---|
150 | cout << "Event ID: " << EventID << endl;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // loop over pixel
|
---|
154 | for ( int pix = firstpixel ; pix < npixel+firstpixel ; pix++ ){
|
---|
155 |
|
---|
156 | // loop over DRS slices
|
---|
157 | for ( unsigned int sl = 0; sl < RegionOfInterest; sl++){
|
---|
158 | Ameas[ sl ] = getValue(sl, pix);
|
---|
159 | }
|
---|
160 |
|
---|
161 | computeN1mean( RegionOfInterest ); // prepare spike removal
|
---|
162 | removeSpikes (RegionOfInterest ); // output in Vcorr
|
---|
163 |
|
---|
164 | // filter Vcorr with sliding average using FIR filter function
|
---|
165 | // 8 is here the HalfWidth of the sliding average window.
|
---|
166 | sliding_avg(Vcorr, Vslide, 8);
|
---|
167 |
|
---|
168 | // factfir(b_slide , a_slide, k_slide, Vcorr, Vslide);
|
---|
169 |
|
---|
170 | for ( unsigned int sl = 0; sl <RegionOfInterest ; sl++){
|
---|
171 | // hPixelCellData.Fill( sl, Vcorr[sl] );
|
---|
172 | hBaseline[pix-firstpixel]->Fill( Vslide[sl] ) ;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | FILE *fp;
|
---|
178 | TString fName;
|
---|
179 | fName = TextOutFileName;
|
---|
180 |
|
---|
181 | fp = fopen(fName, "a+");
|
---|
182 | fprintf( fp, "FILE: %s\n", datafilename );
|
---|
183 | fprintf( fp, "NEVENTS: %d\n", nevents);
|
---|
184 | NBSLeve = nevents; // this has to be changed when computation on a subset of a run is implemented
|
---|
185 | fprintf( fp, "NBSLEVE: %d\n", NBSLeve );
|
---|
186 |
|
---|
187 | for (int pix = firstpixel; pix < firstpixel+npixel; pix++) {
|
---|
188 |
|
---|
189 | float vmaxprob = hBaseline[pix]->GetXaxis()->GetBinCenter(
|
---|
190 | hBaseline[pix-firstpixel]->GetMaximumBin() );
|
---|
191 |
|
---|
192 | fprintf( fp, "%8.3f", vmaxprob );
|
---|
193 |
|
---|
194 | hMeanBsl->Fill( vmaxprob );
|
---|
195 | hpltMeanBsl->SetBinContent(pix+1, vmaxprob );
|
---|
196 |
|
---|
197 | hRmsBsl->Fill(hBaseline[pix-firstpixel]->GetRMS() );
|
---|
198 | hpltRmsBsl->SetBinContent( pix+1, hBaseline[pix]->GetRMS() );
|
---|
199 | }
|
---|
200 | fprintf( fp, "\n" );
|
---|
201 |
|
---|
202 | fclose( fp );
|
---|
203 |
|
---|
204 | SaveHistograms( RootOutFileName );
|
---|
205 | if (produceGraphic){
|
---|
206 | TCanvas * cMeanBsl = new TCanvas();
|
---|
207 | cMeanBsl->cd();
|
---|
208 | hMeanBsl->Draw();
|
---|
209 | cMeanBsl->Update();
|
---|
210 |
|
---|
211 | TCanvas * cRmsBsl = new TCanvas();
|
---|
212 | cRmsBsl->cd();
|
---|
213 | hRmsBsl->Draw();
|
---|
214 | cMeanBsl->Update();
|
---|
215 | }
|
---|
216 | return( 0 );
|
---|
217 | }
|
---|
218 |
|
---|
219 | void removeSpikes(int Samples){
|
---|
220 |
|
---|
221 | const float fract = 0.8;
|
---|
222 | float x, xp, xpp;
|
---|
223 |
|
---|
224 | // assume that there are no spikes
|
---|
225 | for ( int i = 0; i < Samples; i++) Vcorr[i] = Ameas[i];
|
---|
226 |
|
---|
227 | // find the spike and replace it by mean value of neighbors
|
---|
228 | for ( int i = 2; i < Samples-2 ; i++) {
|
---|
229 |
|
---|
230 | x = Ameas[i] - N1mean[i];
|
---|
231 |
|
---|
232 | if ( x < -5. ){ // a spike candidate
|
---|
233 | // check consistency with a single channel spike
|
---|
234 | xp = Ameas[i+1] - N1mean[i+1];
|
---|
235 | xpp = Ameas[i+2] - N1mean[i+2];
|
---|
236 |
|
---|
237 | if ( Ameas[i+2] - ( Ameas[i] + Ameas[i+3] )/2. > 10. ){
|
---|
238 | // printf("double spike candidate\n");
|
---|
239 | Vcorr[i+1] = ( Ameas[i] + Ameas[i+3] )/2.;
|
---|
240 | Vcorr[i+2] = ( Ameas[i] + Ameas[i+3] )/2.;
|
---|
241 | i = i + 3;
|
---|
242 | }
|
---|
243 | else{
|
---|
244 |
|
---|
245 | if ( ( xp > -2.*x*fract ) && ( xpp < -10. ) ){
|
---|
246 | Vcorr[i+1] = N1mean[i+1];
|
---|
247 | // printf("Vcorr[%d] = %f %f %f\n", i, Vcorr[i], Vcorr[i+1], Vcorr[i+2]);
|
---|
248 | // N1mean[i+1] = (Ameas[i] - Ameas[i+2] / 2.);
|
---|
249 | N1mean[i+2] = (Ameas[i+1] - Ameas[i+3] / 2.);
|
---|
250 | i = i + 2;//do not care about the next sample it was the spike
|
---|
251 | }
|
---|
252 | // treatment for the end of the pipeline must be added !!!
|
---|
253 | }
|
---|
254 | }
|
---|
255 | else{
|
---|
256 | // do nothing
|
---|
257 | }
|
---|
258 | } // end of spike search and correction
|
---|
259 | }
|
---|
260 |
|
---|
261 | void computeN1mean( int Samples ){
|
---|
262 | // compute the mean of the left and right neighbors of a channel
|
---|
263 |
|
---|
264 | for( int i = 2; i < Samples - 2; i++){
|
---|
265 | /* if (i == 0){ // use right sample es mean
|
---|
266 | N1mean[i] = Ameas[i+1];
|
---|
267 | }
|
---|
268 | else if ( i == Samples-1 ){ //use left sample as mean
|
---|
269 | N1mean[i] = Ameas[i-1];
|
---|
270 | }
|
---|
271 | else{
|
---|
272 | N1mean[i] = ( Ameas[i-1] + Ameas[i+1] ) / 2.;
|
---|
273 | }
|
---|
274 | */
|
---|
275 | N1mean[i] = ( Ameas[i-1] + Ameas[i+1] ) / 2.;
|
---|
276 | }
|
---|
277 | } // end of computeN1mean computation
|
---|
278 |
|
---|
279 | float getValue( int slice, int pixel ){
|
---|
280 |
|
---|
281 | const float dconv = 2000/4096.0;
|
---|
282 |
|
---|
283 | float vraw, vcal;
|
---|
284 |
|
---|
285 | unsigned int pixel_pt;
|
---|
286 | unsigned int slice_pt;
|
---|
287 | unsigned int cal_pt;
|
---|
288 | unsigned int drs_cal_offset;
|
---|
289 |
|
---|
290 | // printf("pixel = %d, slice = %d\n", slice, pixel);
|
---|
291 |
|
---|
292 | pixel_pt = pixel * RegionOfInterest;
|
---|
293 | slice_pt = pixel_pt + slice;
|
---|
294 | drs_cal_offset = ( slice + StartCells[ pixel ] )%RegionOfInterest;
|
---|
295 | cal_pt = pixel_pt + drs_cal_offset;
|
---|
296 |
|
---|
297 | vraw = Data[ slice_pt ] * dconv;
|
---|
298 | vcal = ( vraw - drs_basemean[ cal_pt ] - drs_triggeroffsetmean[ slice_pt ] ) / drs_gainmean[ cal_pt ]*1907.35;
|
---|
299 |
|
---|
300 | return( vcal );
|
---|
301 | }
|
---|
302 |
|
---|
303 | void BookHistos( int Samples , int NumberOfPixel){
|
---|
304 | // booking and parameter settings for all histos
|
---|
305 |
|
---|
306 | // histograms for baseline extraction
|
---|
307 | char hName[500];
|
---|
308 | char hTitle[500];
|
---|
309 |
|
---|
310 | TH1F *h;
|
---|
311 |
|
---|
312 | printf("inside BookHistos\n");
|
---|
313 |
|
---|
314 | for( int i = 0; i < NumberOfPixel; i++ ) {
|
---|
315 |
|
---|
316 | // printf("call sprintf [%d]\n", i );
|
---|
317 | sprintf(&hTitle[0],"all events all slices of pixel %d", i);
|
---|
318 | sprintf(&hName[0],"base%d", i);
|
---|
319 | // printf("call sprintf [%d] done\n", i );
|
---|
320 |
|
---|
321 | h = new TH1F( hName, hTitle, 400, -99.5 ,100.5 );
|
---|
322 |
|
---|
323 | // printf("histo booked\n");
|
---|
324 | h->GetXaxis()->SetTitle( "Sample value (mV)" );
|
---|
325 | h->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
|
---|
326 | // printf("histo title set\n");
|
---|
327 | hListBaseline.Add( h );
|
---|
328 | // printf("histo added to List\n");
|
---|
329 | hBaseline[i] = h;
|
---|
330 | // printf("histo assigned to array\n");
|
---|
331 | }
|
---|
332 |
|
---|
333 | printf("made HBaseline * 1440\n");
|
---|
334 |
|
---|
335 | hMeanBsl = new TH1F("histo_mean","Value of maximal probability",400,-99.5,100.5);
|
---|
336 | hMeanBsl->GetXaxis()->SetTitle( "max value (mV)" );
|
---|
337 | hMeanBsl->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
|
---|
338 | hList.Add( hMeanBsl );
|
---|
339 |
|
---|
340 | hpltMeanBsl = new TH1F("hplt_mean","Value of maximal probability",1440,-0.5,1439.5);
|
---|
341 | hpltMeanBsl->GetXaxis()->SetTitle( "pixel" );
|
---|
342 | hpltMeanBsl->GetYaxis()->SetTitle( "max value in mV" );
|
---|
343 | hList.Add( hpltMeanBsl );
|
---|
344 |
|
---|
345 | hRmsBsl = new TH1F("histo_rms","RMS in mV",2000,-99.5,100.5);
|
---|
346 | hRmsBsl->GetXaxis()->SetTitle( "RMS (mV)" );
|
---|
347 | hRmsBsl->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
|
---|
348 | hList.Add( hRmsBsl );
|
---|
349 |
|
---|
350 | hpltRmsBsl = new TH1F("hplt_rms","Value of maximal probability",1440,-0.5,1439.5);
|
---|
351 | hpltRmsBsl->GetXaxis()->SetTitle( "pixel" );
|
---|
352 | hpltRmsBsl->GetYaxis()->SetTitle( "RMS in mV" );
|
---|
353 | hList.Add( hpltRmsBsl );
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | void SaveHistograms( const char * loc_fname ){
|
---|
358 |
|
---|
359 | TString fName; // name of the histogram file
|
---|
360 |
|
---|
361 | /* create the filename for the histogram file */
|
---|
362 | fName = loc_fname; // use the name of the tree file
|
---|
363 | //fName.Remove(fName.Length() - 5); // remove the extension .root
|
---|
364 | //fName += "_histo.root"; // add the new extension
|
---|
365 | //fName += ".root";
|
---|
366 |
|
---|
367 | TFile tf( fName, "RECREATE"); // create the histogram file (replace if already existing)
|
---|
368 |
|
---|
369 | hList.Write(); // write the major histograms into the top level directory
|
---|
370 | tf.mkdir("BaselineHisto"); tf.cd("BaselineHisto"); // go to new subdirectory
|
---|
371 | hListBaseline.Write(); // write histos into subdirectory
|
---|
372 |
|
---|
373 | tf.Close(); // close the file
|
---|
374 |
|
---|
375 | } // end of function: void ana::SaveHistograms( void )
|
---|
376 |
|
---|
377 | size_t FOpenDataFile(
|
---|
378 | const char *datafilename, // path to fits file containing FACT raw data
|
---|
379 | fits * * datafile, // ptr to pointer, where to return the fits object
|
---|
380 | vector<int16_t> &Data, // vector, which will be filled with raw data
|
---|
381 | vector<int16_t> &StartCells, // vector, which will be filled with DRS start positions
|
---|
382 | unsigned int &EventID, // index of the current event
|
---|
383 | UInt_t &RegionOfInterest, // Width of the Region, read out of the DRS
|
---|
384 | UInt_t &NumberOfPixels, // Total number of pixel, read out of the camera
|
---|
385 | size_t &PXLxROI, // Size of column "Data" = #Pixel x ROI
|
---|
386 | // this can be used, to x-check RegionOfInterest and NumberOfPixels
|
---|
387 | int VerbosityLevel //
|
---|
388 | ) {
|
---|
389 | size_t NumberOfEvents;
|
---|
390 | *datafile = new fits(datafilename);
|
---|
391 | if (!(*(*datafile))) {
|
---|
392 | if (VerbosityLevel > 0)
|
---|
393 | cout << "Couldn't properly open the datafile: " << datafilename << endl;
|
---|
394 | return 0;
|
---|
395 | }
|
---|
396 |
|
---|
397 | NumberOfEvents = (*datafile)->GetNumRows();
|
---|
398 | if (NumberOfEvents < 1){
|
---|
399 | if (VerbosityLevel > 0){
|
---|
400 | cout << "Warning in FOpenDataFile of file: " << datafilename << endl;
|
---|
401 | cout << "the file contains no events." << endl;
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | RegionOfInterest = (*datafile)->GetUInt("NROI");
|
---|
406 | NumberOfPixels = (*datafile)->GetUInt("NPIX");
|
---|
407 | PXLxROI = (*datafile)->GetN("Data");
|
---|
408 |
|
---|
409 | if ( RegionOfInterest * NumberOfPixels != PXLxROI) // something in the file went wrong
|
---|
410 | {
|
---|
411 | if (VerbosityLevel > 0){
|
---|
412 | cout << "Warning in FOpenDataFile of file: " << datafilename << endl;
|
---|
413 | cout << "RegionOfInterest * NumberOfPixels != PXLxROI" << endl;
|
---|
414 | cout << "--> " << RegionOfInterest;
|
---|
415 | cout << " * " << NumberOfPixels;
|
---|
416 | cout << " = " << RegionOfInterest * NumberOfPixels;
|
---|
417 | cout << ", but PXLxROI =" << PXLxROI << endl;
|
---|
418 | }
|
---|
419 | return 0;
|
---|
420 | }
|
---|
421 |
|
---|
422 | //Set the sizes of the data vectors
|
---|
423 | Data.resize(PXLxROI, 0);
|
---|
424 | StartCells.resize(NumberOfPixels, 0);
|
---|
425 |
|
---|
426 | //Link the data to variables
|
---|
427 | (*datafile)->SetRefAddress("EventNum", EventID);
|
---|
428 | (*datafile)->SetVecAddress("Data", Data);
|
---|
429 | (*datafile)->SetVecAddress("StartCellData", StartCells);
|
---|
430 |
|
---|
431 | return NumberOfEvents;
|
---|
432 | }
|
---|
433 |
|
---|