source: fact/tools/rootmacros/fbsl.C@ 12515

Last change on this file since 12515 was 12514, checked in by neise, 14 years ago
try with new calibration
File size: 8.4 KB
Line 
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
19#include "openFits.h"
20#include "openFits.c"
21
22#include "DrsCalibration.h"
23#include "DrsCalibration.C"
24
25#include "SpikeRemoval.h"
26#include "SpikeRemoval.C"
27
28//#include "zerosearch.C"
29#include "factfir.C"
30
31#define NPIX 1440
32#define NCELL 1024
33
34// data access and treatment
35#define FAD_MAX_SAMPLES 1024
36
37int NEvents;
38vector<int16_t> Data; // vector, which will be filled with raw data
39vector<int16_t> StartCells; // vector, which will be filled with DRS start positions
40unsigned int EventID; // index of the current event
41UInt_t RegionOfInterest; // Width of the Region, read out of the DRS
42UInt_t NumberOfPixels; // Total number of pixel, read out of the camera
43size_t PXLxROI; // Size of column "Data" = #Pixel x ROI
44
45int NBSLeve = 1000;
46
47size_t TriggerOffsetROI, RC;
48vector<float> Offset, Gain, TriggerOffset;
49
50
51vector<float> Ameas(FAD_MAX_SAMPLES); // copy of the data (measured amplitude
52vector<float> N1mean(FAD_MAX_SAMPLES); // mean of the +1 -1 ch neighbors
53vector<float> N2mean(FAD_MAX_SAMPLES); // mean of the +2 -2 ch neighbors
54vector<float> Vcorr(FAD_MAX_SAMPLES); // corrected Values
55vector<float> Vdiff(FAD_MAX_SAMPLES); // numerical derivative
56
57vector<float> Vslide(FAD_MAX_SAMPLES); // sliding average result
58vector<float> Vcfd(FAD_MAX_SAMPLES); // CDF result
59vector<float> Vcfd2(FAD_MAX_SAMPLES); // CDF result + 2nd sliding average
60
61
62
63// histograms
64const int Ntypes = 7;
65const unsigned int // arranged by Dominik
66 tAmeas = 0,
67 tN1mean = 1,
68 tVcorr = 2,
69 tVtest = 3,
70 tVslide = 4,
71 tVcfd = 5,
72 tVcfd2 = 6;
73
74TH1F* h;
75TH2F hPixelCellData(
76 "PixelPedestal", "PixelPedestal", NCELL, 0., NCELL, 200, -50., 150.);
77TH1F *hBaseline[ NPIX ]; // histograms for baseline extraction
78TH1F *hMeanBsl, *hpltMeanBsl;
79TH1F *hRmsBsl, *hpltRmsBsl;
80TObjArray hList;
81TObjArray hListBaseline;
82
83void BookHistos( int , int);
84void SaveHistograms( const char * );
85
86// Create a canvas
87TCanvas* CW;
88TCanvas* cFilter;
89
90int spikeDebug = 0;
91int searchSinglesPeaks = 0;
92
93int fbsl(
94 const char *datafilename = "path-to-datafile.fits.gz",
95 const char *drsfilename = "path-to-calibfile.drs.fits.gz",
96 const char *TextOutFileName = "./appendfile.txt",
97 const char *RootOutFileName = "./datafile.root",
98 int firstevent = 0,
99 int nevents = -1,
100 int firstpixel = 0,
101 int npixel = -1,
102 bool produceGraphic = true
103){
104 fits * datafile = NULL;
105
106 NEvents = openDataFits(
107 datafilename,
108 &datafile,
109 Data,
110 StartCells,
111 EventID,
112 RegionOfInterest,
113 NumberOfPixels,
114 PXLxROI);
115
116 printf("number of events in file: %d\n", NEvents);
117 if (NEvents == 0){
118 cout << "return code of openDataFits:" << datafilename<< endl;
119 cout << "is zero -> aborting." << endl;
120 return 1;
121 }
122
123
124 // compare the number of events in the data file with the nevents the
125 // the user would like to read. nevents = -1 means: read all
126 if ( nevents == -1 || nevents > NEvents ) nevents = NEvents;
127 if ( npixel == -1 || npixel > (int)NumberOfPixels) npixel = NumberOfPixels;
128
129 RC = openCalibFits( drsfilename, Offset, Gain, TriggerOffset, TriggerOffsetROI);
130 if (RC == 0){
131 cout << "return code of openCalibFits:" << drsfilename << endl;
132 cout << "is zero -> aborting." << endl;
133 return 1;
134 }
135
136
137 BookHistos( RegionOfInterest, npixel );
138
139 // loop over events
140 for ( int ev = firstevent; ev < firstevent + nevents; ev++) {
141
142 datafile->GetRow( ev );
143
144 if ( ev % 100 == 0 ){
145 cout << "Event ID: " << EventID << endl;
146 }
147
148 // loop over pixel
149 for ( int pix = firstpixel ; pix < npixel+firstpixel ; pix++ ){
150
151
152 // get the data of this pixel from the Data vector
153 // apply the Drs Calibration and cut off 12 slices at the beginning
154 // and at the end.
155 applyDrsCalibration( Ameas,pix,12,12,
156 Offset, Gain, TriggerOffset,
157 RegionOfInterest, Data, StartCells);
158
159 // finds spikes in the raw data, and interpolates the value
160 // spikes are: 1 or 2 slice wide, positive non physical artifacts
161 removeSpikes (Ameas, Vcorr);
162
163 // filter Vcorr with sliding average using FIR filter function
164 // 8 is here the HalfWidth of the sliding average window.
165 sliding_avg(Vcorr, Vslide, 8);
166
167 for ( unsigned int sl = 0; sl <RegionOfInterest ; sl++){
168 // hPixelCellData.Fill( sl, Vcorr[sl] );
169 hBaseline[pix-firstpixel]->Fill( Vslide[sl] ) ;
170 }
171 }
172 }
173
174 FILE *fp;
175 TString fName;
176 fName = TextOutFileName;
177
178 fp = fopen(fName, "a+");
179 fprintf( fp, "FILE: %s\n", datafilename );
180 fprintf( fp, "NEVENTS: %d\n", nevents);
181 NBSLeve = nevents; // this has to be changed when computation on a subset of a run is implemented
182 fprintf( fp, "NBSLEVE: %d\n", NBSLeve );
183
184 for (int pix = firstpixel; pix < firstpixel+npixel; pix++) {
185
186 float vmaxprob = hBaseline[pix]->GetXaxis()->GetBinCenter(
187 hBaseline[pix-firstpixel]->GetMaximumBin() );
188
189 fprintf( fp, "%8.3f", vmaxprob );
190
191 hMeanBsl->Fill( vmaxprob );
192 hpltMeanBsl->SetBinContent(pix+1, vmaxprob );
193
194 hRmsBsl->Fill(hBaseline[pix-firstpixel]->GetRMS() );
195 hpltRmsBsl->SetBinContent( pix+1, hBaseline[pix]->GetRMS() );
196 }
197 fprintf( fp, "\n" );
198
199 fclose( fp );
200
201 SaveHistograms( RootOutFileName );
202 if (produceGraphic){
203 TCanvas * cMeanBsl = new TCanvas();
204 cMeanBsl->cd();
205 hMeanBsl->Draw();
206 cMeanBsl->Update();
207
208 TCanvas * cRmsBsl = new TCanvas();
209 cRmsBsl->cd();
210 hRmsBsl->Draw();
211 cMeanBsl->Update();
212 }
213 return( 0 );
214}
215
216void BookHistos( int Samples , int NumberOfPixel){
217// booking and parameter settings for all histos
218
219 // histograms for baseline extraction
220 char hName[500];
221 char hTitle[500];
222
223 TH1F *h;
224
225 printf("inside BookHistos\n");
226
227 for( int i = 0; i < NumberOfPixel; i++ ) {
228
229 // printf("call sprintf [%d]\n", i );
230 sprintf(&hTitle[0],"all events all slices of pixel %d", i);
231 sprintf(&hName[0],"base%d", i);
232 // printf("call sprintf [%d] done\n", i );
233
234 h = new TH1F( hName, hTitle, 400, -99.5 ,100.5 );
235
236 // printf("histo booked\n");
237 h->GetXaxis()->SetTitle( "Sample value (mV)" );
238 h->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
239 // printf("histo title set\n");
240 hListBaseline.Add( h );
241 // printf("histo added to List\n");
242 hBaseline[i] = h;
243 // printf("histo assigned to array\n");
244 }
245
246 printf("made HBaseline * 1440\n");
247
248 hMeanBsl = new TH1F("histo_mean","Value of maximal probability",400,-99.5,100.5);
249 hMeanBsl->GetXaxis()->SetTitle( "max value (mV)" );
250 hMeanBsl->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
251 hList.Add( hMeanBsl );
252
253 hpltMeanBsl = new TH1F("hplt_mean","Value of maximal probability",1440,-0.5,1439.5);
254 hpltMeanBsl->GetXaxis()->SetTitle( "pixel" );
255 hpltMeanBsl->GetYaxis()->SetTitle( "max value in mV" );
256 hList.Add( hpltMeanBsl );
257
258 hRmsBsl = new TH1F("histo_rms","RMS in mV",2000,-99.5,100.5);
259 hRmsBsl->GetXaxis()->SetTitle( "RMS (mV)" );
260 hRmsBsl->GetYaxis()->SetTitle( "Entries / 0.5 mV" );
261 hList.Add( hRmsBsl );
262
263 hpltRmsBsl = new TH1F("hplt_rms","Value of maximal probability",1440,-0.5,1439.5);
264 hpltRmsBsl->GetXaxis()->SetTitle( "pixel" );
265 hpltRmsBsl->GetYaxis()->SetTitle( "RMS in mV" );
266 hList.Add( hpltRmsBsl );
267}
268
269
270void SaveHistograms( const char * loc_fname ){
271
272 TString fName; // name of the histogram file
273
274 /* create the filename for the histogram file */
275 fName = loc_fname; // use the name of the tree file
276 //fName.Remove(fName.Length() - 5); // remove the extension .root
277 //fName += "_histo.root"; // add the new extension
278 //fName += ".root";
279
280 TFile tf( fName, "RECREATE"); // create the histogram file (replace if already existing)
281
282 hList.Write(); // write the major histograms into the top level directory
283 tf.mkdir("BaselineHisto"); tf.cd("BaselineHisto"); // go to new subdirectory
284 hListBaseline.Write(); // write histos into subdirectory
285
286 tf.Close(); // close the file
287
288} // end of function: void ana::SaveHistograms( void )
289
Note: See TracBrowser for help on using the repository browser.