| 1 | /* | 
|---|
| 2 | GetDrsOffset.C - a ROOT macro calculating the DRS Offset calibration constants | 
|---|
| 3 |  | 
|---|
| 4 | -- TESTING -- | 
|---|
| 5 |  | 
|---|
| 6 | In case you have taken a file, which is good for calibrating the DRS Offset, | 
|---|
| 7 | then you can calculate the offset using this macro. | 
|---|
| 8 | It just calculates the mean value and the RMS for all DRS bins of all pixels | 
|---|
| 9 | in the file, for the number of events you like. | 
|---|
| 10 |  | 
|---|
| 11 | The output is two TH2Fs, which contain all requested data on the one hand and | 
|---|
| 12 | on the other hand are good for having an overview. | 
|---|
| 13 |  | 
|---|
| 14 | call it like this: | 
|---|
| 15 | GetDrsOffset("path/20111111_001.fits.gz") | 
|---|
| 16 |  | 
|---|
| 17 | GetDrsOffset("path/20111111_001.fits.gz", "foo/output.root") | 
|---|
| 18 | saves the output to a root file | 
|---|
| 19 |  | 
|---|
| 20 | GetDrsOffset("path/20111111_001.fits.gz", "", "bar/test.bin") | 
|---|
| 21 | saves the output to a binary file. | 
|---|
| 22 |  | 
|---|
| 23 | GetDrsOffset("path/20111111_001.fits.gz", "foo/out.root", "bar/test.bin", false, -1) | 
|---|
| 24 | produces all output, but does not open a ROOT Canvas. | 
|---|
| 25 | -1 : means process all events in the given file. | 
|---|
| 26 |  | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | #include <TROOT.h> | 
|---|
| 31 | #include <TCanvas.h> | 
|---|
| 32 | #include <TH2F.h> | 
|---|
| 33 | #include <Getline.h> | 
|---|
| 34 | #include <TFile.h> | 
|---|
| 35 | #include <TStyle.h> | 
|---|
| 36 |  | 
|---|
| 37 | #include <stdio.h> | 
|---|
| 38 | #include <stdint.h> | 
|---|
| 39 | #include <cstdio> | 
|---|
| 40 | #include <fstream> | 
|---|
| 41 | #include <string.h> | 
|---|
| 42 |  | 
|---|
| 43 | #define HAVE_ZLIB | 
|---|
| 44 | #include "fits.h" | 
|---|
| 45 |  | 
|---|
| 46 | #include "openFits.h" | 
|---|
| 47 | #include "openFits.c" | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 | int NEvents; | 
|---|
| 51 | vector<int16_t> AllPixelDataVector; | 
|---|
| 52 | vector<int16_t> StartCellVector; | 
|---|
| 53 | unsigned int CurrentEventID; | 
|---|
| 54 | size_t PXLxROI; | 
|---|
| 55 | UInt_t RegionOfInterest; | 
|---|
| 56 | UInt_t NumberOfPixels; | 
|---|
| 57 |  | 
|---|
| 58 | TH2F * hOffset; | 
|---|
| 59 | TH2F * hOffsetRMS; | 
|---|
| 60 | double mean[1440][1024]={{0}}; | 
|---|
| 61 | int n[1440][1024]={{0}}; | 
|---|
| 62 | double rms[1440][1024]={{0}}; | 
|---|
| 63 | TObjArray hList; | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 | int GetDrsOffset( | 
|---|
| 67 | char *datafilename              = "data/20111016_013.fits.gz", | 
|---|
| 68 | const char *RootFileName = "", | 
|---|
| 69 | const char *OutFileName = "", | 
|---|
| 70 | bool graphic = true, | 
|---|
| 71 | int nevents                     = -1, | 
|---|
| 72 | int     verbosityLevel = 1 | 
|---|
| 73 | ) | 
|---|
| 74 | { | 
|---|
| 75 | gStyle->SetPalette(1,0); | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | fits * datafile; | 
|---|
| 79 | NEvents = openDataFits( datafilename, &datafile, | 
|---|
| 80 | AllPixelDataVector, StartCellVector, CurrentEventID, | 
|---|
| 81 | RegionOfInterest, NumberOfPixels, PXLxROI, verbosityLevel); | 
|---|
| 82 | if (NEvents == 0){ | 
|---|
| 83 | cout << "return code of OpenDataFile:" << datafilename<< endl; | 
|---|
| 84 | cout << "is zero -> aborting." << endl; | 
|---|
| 85 | return 1; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | if (verbosityLevel > 0) | 
|---|
| 89 | cout <<"number of events in file: "<< NEvents << "\t"; | 
|---|
| 90 | if ( nevents == -1 || nevents > NEvents ) nevents = NEvents; // -1 means all! | 
|---|
| 91 | if (verbosityLevel > 0) | 
|---|
| 92 | cout <<"of, which "<< nevents << " will be processed"<< endl; | 
|---|
| 93 |  | 
|---|
| 94 | int npixel = -1; | 
|---|
| 95 | if (verbosityLevel > 0) | 
|---|
| 96 | cout <<"Total # of Pixel: "<< NumberOfPixels << "\t"; | 
|---|
| 97 | if ( npixel == -1 || npixel > (int)NumberOfPixels  ) npixel = (int)NumberOfPixels; // -1 means all! | 
|---|
| 98 | if (verbosityLevel > 0) | 
|---|
| 99 | cout <<"of, which "<< npixel << " will be processed"<< endl; | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | if (verbosityLevel > 0) | 
|---|
| 103 | cout <<" The ROI of this datafile is:" << RegionOfInterest << endl; | 
|---|
| 104 |  | 
|---|
| 105 | double cur; | 
|---|
| 106 | // -------------------- OFFSET -------------------------------------------- | 
|---|
| 107 | for ( int ev = 0; ev < nevents; ev++) { | 
|---|
| 108 | printf("%5d / %5d\b\b\b\b\b\b\b\b\b\b\b\b\b",ev,nevents); | 
|---|
| 109 | datafile->GetRow( ev ); | 
|---|
| 110 | for ( int pix = 0 ; pix < npixel; pix++ ){ | 
|---|
| 111 | for (unsigned int sl=0; sl< RegionOfInterest; sl++){ | 
|---|
| 112 | cur = AllPixelDataVector[pix * RegionOfInterest + sl]; | 
|---|
| 113 | mean[pix][(sl+StartCellVector[pix])%1024] += cur; | 
|---|
| 114 | ++n[pix][(sl+StartCellVector[pix])%1024]; | 
|---|
| 115 | rms[pix][(sl+StartCellVector[pix])%1024] += cur * cur; | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 | for (int p=0; p<1440; p++) | 
|---|
| 120 | for (int s=0; s<1024; s++){ | 
|---|
| 121 | if (n[p][s]==0){ | 
|---|
| 122 | cout << "Error: no data for pixel:" << p <<" slice:" << s << endl; | 
|---|
| 123 | return 1; | 
|---|
| 124 | } | 
|---|
| 125 | mean[p][s]/=n[p][s]; | 
|---|
| 126 | rms[p][s]/=n[p][s]; | 
|---|
| 127 | rms[p][s]-=mean[p][s]*mean[p][s]; | 
|---|
| 128 | rms[p][s]=sqrt(rms[p][s]); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | // ----------------- For plotting only ------------------- | 
|---|
| 133 | if (graphic){ | 
|---|
| 134 |  | 
|---|
| 135 | TCanvas *cOffset = NULL; | 
|---|
| 136 | cOffset = new TCanvas("cOffset","Offset",10,10,800,400); | 
|---|
| 137 | cOffset->Divide(1, 2); | 
|---|
| 138 |  | 
|---|
| 139 | hOffset = new TH2F("hOffset","Offset",1440,-0.5,1439.5, 1024, -0.5, 1023.5); | 
|---|
| 140 | hOffset->GetXaxis()->SetTitle( "pixel" ); | 
|---|
| 141 | hOffset->GetYaxis()->SetTitle( "slice" ); | 
|---|
| 142 | hList.Add( hOffset ); | 
|---|
| 143 |  | 
|---|
| 144 | hOffsetRMS = new TH2F("hOffsetRMS","Offset RMS",1440,-0.5,1439.5, 1024, -0.5, 1023.5); | 
|---|
| 145 | hOffsetRMS->GetXaxis()->SetTitle( "pixel" ); | 
|---|
| 146 | hOffsetRMS->GetYaxis()->SetTitle( "slice" ); | 
|---|
| 147 | hList.Add( hOffsetRMS ); | 
|---|
| 148 |  | 
|---|
| 149 | for ( int pix = 0 ; pix < 1440; pix++ ){ | 
|---|
| 150 | for (unsigned int sl=0; sl< 1024; sl++){ | 
|---|
| 151 | hOffset->Fill(pix,sl,mean[pix][sl]); | 
|---|
| 152 | hOffsetRMS->Fill(pix,sl,rms[pix][sl]); | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 | cOffset->cd(1); | 
|---|
| 156 | hOffset->Draw("COLZ"); | 
|---|
| 157 | cOffset->cd(2); | 
|---|
| 158 | hOffsetRMS->Draw("COLZ"); | 
|---|
| 159 | cOffset->Modified(); | 
|---|
| 160 | cOffset->Update(); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | if ( strlen(OutFileName) > 0 ) { | 
|---|
| 164 | ofstream out(OutFileName, ios::out | ios::binary); | 
|---|
| 165 | if(!out) { | 
|---|
| 166 | cout << "Cannot open file." << OutFileName << endl; | 
|---|
| 167 | return 1; | 
|---|
| 168 | } | 
|---|
| 169 | out.write((char *) &mean, sizeof mean); | 
|---|
| 170 | out.write((char *) &rms, sizeof rms); | 
|---|
| 171 | out.close(); | 
|---|
| 172 | cout << "binary output written to \"" << OutFileName << "\"" << endl; | 
|---|
| 173 | }else | 
|---|
| 174 | cout << "no output generated" << endl; | 
|---|
| 175 |  | 
|---|
| 176 | /* | 
|---|
| 177 | double offset[1440][1024]; | 
|---|
| 178 | ifstream in(OutFileName, ios::in | ios::binary); | 
|---|
| 179 | in.read((char*) &offset, sizeof offset); | 
|---|
| 180 | in.close(); | 
|---|
| 181 |  | 
|---|
| 182 |  | 
|---|
| 183 | for ( int pix = 0 ; pix < 1440; pix++ ){ | 
|---|
| 184 | for (unsigned int sl=0; sl< 1024; sl++){ | 
|---|
| 185 | cout << offset[pix][sl] << endl; | 
|---|
| 186 | } | 
|---|
| 187 | } | 
|---|
| 188 | */ | 
|---|
| 189 |  | 
|---|
| 190 | TFile tf( RootFileName, "RECREATE"); | 
|---|
| 191 | hList.Write(); // write the major histograms into the top level directory | 
|---|
| 192 | tf.Close(); // close the file | 
|---|
| 193 | return( 0 ); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|