source: fact/tools/rootmacros/example.C@ 17963

Last change on this file since 17963 was 12719, checked in by neise, 13 years ago
at least it compiles
File size: 9.6 KB
Line 
1#warning I AM NOT YET TESTED ... DO NOT USE ME
2
3// example.C
4// ROOT macro to show, how FACT raw data can be read into ROOT
5// -> be DRS calibrated using a dedicated .drs.fits file
6// -> also the famous DRS spikes will be removed.
7
8#include <TROOT.h> // everybody needs the ROOT of everything
9#include <TStyle.h> // in order to set gStyle->SetPallette(1,0);
10#include <TCanvas.h> // well ..we are going to show some plots, right?
11#include <TTimer.h> // dunno
12#include <Getline.h> // well ... for Getline()
13#include <TFile.h> // for root file handling
14#include <TH1F.h> // we will use these Histograms
15
16#define HAVE_ZLIB // -> Info for fits.h
17#include "fits.h" // fits.h was written by Thomas Bretz
18
19// provides 2 functions to get easily the raw data
20// out of our fits files... there is more data in the fits files,
21// but it is not needed in this example.
22// For further Info, .. ask an expert, or wait for the next Example :-)
23#include "openFits.h" //
24#include "openFits.c" //
25
26// DrsCalibration provides a function, which can be used to retrieve
27// DRS-calibrated data of a certain pixel.
28#include "DrsCalibration.h"
29#include "DrsCalibration.C"
30#include "SpikeRemoval.h"
31#include "SpikeRemoval.C"
32
33// contains implementation of std FIR filters for vector<float> and
34// a simple sldig average filter, which is not FIR.
35#include "factfir.C"
36
37// These variables will be linked to the raw data file.
38// So when ever the Memberfunction "GetRow(int i)" of a fits file is called
39// These variables will contain the data of the i'th Event:
40int NEvents; // total number of events in file
41vector<int16_t> AllPixelDataVector; // All raw data of all pixel of the current Event
42vector<int16_t> StartCellVector; // All StartCells of all pixel of the current Event
43unsigned int CurrentEventID; // The id = upcounting number of the current Event
44size_t PXLxROI; // the width of data read out of the DRS chips x the number of Pixels
45 // so this will be the length of AllPixelDataVector
46UInt_t RegionOfInterest; // The region of Interest of all pixel in the current file
47UInt_t NumberOfPixels; // the number of pixels in the current file
48// please note:
49// Most of this information is not actually needed to plot or work with
50// the raw data. It is just needed to apply the DRS calibration.
51// So in principle the user of this script should not even see, that these
52// data exists somewhere, but I was not yet able to hide it...
53
54
55
56// these variables will contain the calibration constants read out of the
57// dedicated DRS calibration files.
58vector<float> Offset; // the offsets for all 1024 slices for all 1440 channels
59vector<float> Gain; // the gains for all 1024 slices for all 1440 channels
60vector<float> TriggerOffset; // a minor effect calibration constant. for details see DrsCalibration.h and .c
61size_t TriggerOffsetROI; // the 'width' of the TriggerOffset per channel
62size_t RC; // RC = return code of openCalibFits() -> should be number of rows = 1 !!!
63
64vector<float> Wavelet; // here we store the data of one single event of one single pixel
65TH1F* hWavelet = NULL; // this we use for plotting the Wavelet
66TCanvas *cExample = NULL;
67TObjArray hList;
68// The following two functions were introduced by Werner
69// BookHistos() creates histograms and takes care of settings like
70// Binning
71// Naming
72// Colors
73// SaveHistos() takes care of Saving all Histos created in BookHistos to a root file
74// this is a nice idea... makes 'sure' nothing is plotted, but forgotten to save...
75void BookHistos();
76void SaveHistograms(const char * );
77
78int example(
79 const char *datafilename = "path-to-data/20111016_013.fits.gz",
80 const char *drsfilename = "path-to-data/20111016_011.drs.fits.gz",
81 const char *OutRootFileName = "path-to-analysis/20111016_013_example.root",
82 int firstevent = 0,
83 int nevents = -1,
84 int firstpixel = 0,
85 int npixel = -1,
86 bool Debug = true,
87 int verbosityLevel = 1,
88 bool ProduceGraphic = true
89 )
90{
91bool breakout = false;
92
93cout << "I AM NOT YET TESTED ... DO NOT USE ME" << endl;
94
95
96gStyle->SetPalette(1,0); // nice color scheme
97gROOT->SetStyle("Plain"); // don't know
98
99 if ( Debug ){
100 cExample = new TCanvas("cFiltered","filtered DRS Waveforms",410,410,400,400);
101 cExample->Divide(1, 4);
102 }
103
104 fits * datafile;
105 // Opens the raw data file and 'binds' the variables given as
106 // Parameters to the data file. So they are filled with
107 // raw data as soon as datafile->GetRow(int) is called.
108 NEvents = openDataFits( datafilename, &datafile,
109 AllPixelDataVector, StartCellVector, CurrentEventID,
110 RegionOfInterest, NumberOfPixels, PXLxROI, verbosityLevel);
111 if (NEvents == 0){
112 cout << "return code of OpenDataFile:" << datafilename<< endl;
113 cout << "is zero -> aborting." << endl;
114 return 1;
115 }
116 if ( nevents == -1 || nevents > NEvents ) nevents = NEvents; // -1 means all!
117 if ( npixel == -1 || npixel > (int)NumberOfPixels ) npixel = (int)NumberOfPixels; // -1 means all!
118 if (verbosityLevel > 0) {
119 cout <<"number of events in file: "<< NEvents << "\t";
120 cout <<"of, which "<< nevents << "will be processed"<< endl;
121 cout <<"Total # of Pixel: "<< NumberOfPixels << "\t";
122 cout <<"of, which "<< npixel << "will be processed"<< endl;
123 }
124
125 RC = openCalibFits( drsfilename, Offset, Gain, TriggerOffset, TriggerOffsetROI);
126 if (RC == 0){
127 cout << "return code of openCalibFits:" << drsfilename << endl;
128 cout << "is zero -> aborting." << endl;
129 return 1;
130 }
131
132 BookHistos();
133
134//-----------------------------------------------------------------------------
135// Loops over Every Event and Pixel
136//-----------------------------------------------------------------------------
137 for ( int ev = firstevent; ev < firstevent + nevents; ev++) {
138 // Get an Event --> consists of 1440 Pixel ...erm....data
139 datafile->GetRow( ev );
140
141 for ( int pix = firstpixel; pix < firstpixel+npixel; pix++ ){
142 if (verbosityLevel > 0){
143 if (pix == firstpixel){
144 cout << "Processing Event: " << CurrentEventID << "/" << nevents << endl;
145 }
146 }
147
148 // this is the interesting point of this script...
149 // well actually it is not interesting, but beginning from this point on
150 // the user of this script has access to the pure data of:
151 // event number ev
152 // pixel number pix
153 // The vector Wavewlet contains now the data, while the first 12 slices
154 // and the last 12 slices were cut out of the raw data...
155 // this was decided once on a FACT meeting.
156 applyDrsCalibration( Wavelet, pix, 12, 12,
157 Offset, Gain, TriggerOffset,
158 RegionOfInterest, AllPixelDataVector, StartCellVector);
159
160 // HERE we could plot the Wavelet, but lets first remove the spikes.
161
162 // finds spikes in the raw data, and interpolates the value
163 // spikes are: 1 or 2 slice wide, positive non physical artifacts
164 // the data containing the spike is taken from 'Wavelet'
165 // and the cleaned result is stored back to 'Wavelet'
166 // synopsis: removeSpikes (input-vector, output-vector);
167 removeSpikes (Wavelet, Wavelet);
168
169 // Apply a sliding average Filter to the raw data ... it looks nicer for plotting
170 // :-) This filter is defined in fir.h and .c, even though this particular Filter is not
171 // a FIR filter, since it filters using past and future of a certain sample.
172 // The filter width is 2*4+1=9 slices.
173 sliding_avg(Wavelet, Wavelet, 4);
174
175
176 if ( Debug ){
177 hWavelet->GetXaxis()->Set(Wavelet.size() , -0.5 , Wavelet.size()-0.5);
178
179 for ( unsigned int sl = 0; sl < RegionOfInterest; sl++){
180 hWavelet->SetBinContent(sl, Wavelet[sl]);
181 }
182
183 cExample->cd(1);
184 gPad->SetGrid();
185 hWavelet->Draw();
186 cExample->Update();
187
188 //Process gui events asynchronously during input
189 TTimer timer("gSystem->ProcessEvents();", 50, kFALSE);
190 timer.TurnOn();
191 TString input = Getline("Type 'q' to exit, <return> to go on: ");
192 timer.TurnOff();
193 if (input=="q\n") {
194 breakout=true;
195 }
196 }// end of if(spikeDebug)
197
198 if (breakout)
199 break;
200 } // end of loop over pixels
201
202 if (breakout)
203 break;
204 } // end of loop over pixels
205
206if (ProduceGraphic){
207 cExample = new TCanvas("cExample ","An Example TCanvas",10,10,400,400);
208 hWavelet->Draw();
209 cExample->Modified();
210 cExample->Update();
211}
212 SaveHistograms( OutRootFileName );
213 return( 0 );
214}
215
216// booking and parameter settings for all histos
217void BookHistos(){
218
219 hWavelet = new TH1F("hWavelet", "to be changed", Wavelet.size(), -0.5, Wavelet.size()-0.5 );
220 hWavelet->GetXaxis()->SetTitle( "time in slices" );
221 hWavelet->GetYaxis()->SetTitle( "amplitude in mV" );
222 hList.Add(hWavelet);
223}
224
225void SaveHistograms( const char * loc_fname){
226 // create the histogram file (replace if already existing)
227 TFile tf( loc_fname, "RECREATE");
228
229 hList.Write(); // write the major histograms into the top level directory
230
231 tf.Close(); // close the file
232}
Note: See TracBrowser for help on using the repository browser.