StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFgtQaCorrelationPlotMaker.cxx
1 
6 /***************************************************************************
7  *
8  * $Id: StFgtQaCorrelationPlotMaker.cxx,v 1.2 2012/01/31 16:48:34 wwitzke Exp $
9  * Author: S. Gliske, Sept 2011
10  *
11  ***************************************************************************
12  *
13  * Description: See header.
14  *
15  ***************************************************************************
16  *
17  * $Log: StFgtQaCorrelationPlotMaker.cxx,v $
18  * Revision 1.2 2012/01/31 16:48:34 wwitzke
19  * Changed for cosmic test stand.
20  *
21  * Revision 1.1 2012/01/31 09:26:17 sgliske
22  * StFgtQaMakers moved to StFgtPool
23  *
24  * Revision 1.9 2012/01/30 10:42:23 sgliske
25  * strip containers now contain adc values for
26  * all time bins. Also fixed bug where setType modified the timebin
27  * rather than the type.
28  *
29  * Revision 1.8 2012/01/26 13:13:11 sgliske
30  * Updated to use StFgtConsts, which
31  * replaces StFgtEnums and StFgtGeomDefs
32  *
33  * Revision 1.7 2012/01/17 21:56:26 sgliske
34  * Short_t geoId -> Int_t geoId
35  *
36  * Revision 1.6 2011/11/01 19:07:24 sgliske
37  * Updated to correspond with StEvent containers, take 2.
38  *
39  * Revision 1.5 2011/10/10 17:41:26 sgliske
40  * Debugged option for vs. r or phi strips
41  *
42  * Revision 1.4 2011/10/07 20:56:18 sgliske
43  * adding StFgtQaClusterChargePerAPV
44  *
45  * Revision 1.3 2011/09/30 19:05:31 sgliske
46  * general update
47  *
48  * Revision 1.2 2011/09/30 17:08:34 sgliske
49  * Update for geoId->elecCoord function now in StFgtCosmicTestStandGeom
50  *
51  * Revision 1.1 2011/09/28 17:48:50 sgliske
52  * minor updates
53  *
54  *
55  **************************************************************************/
56 
57 #include "StFgtQaCorrelationPlotMaker.h"
58 
59 #include <string>
60 #include <vector>
61 #include <map>
62 #include <TH2F.h>
63 #include <TROOT.h>
64 #include <TStyle.h>
65 
66 #include "StRoot/StFgtPool/StFgtCosmicTestStandGeom/StFgtCosmicTestStandGeom.h"
67 
68 
69 #include "StRoot/StEvent/StFgtCollection.h"
70 #include "StRoot/StEvent/StFgtHitCollection.h"
71 #include "StRoot/StEvent/StFgtHit.h"
72 
73 // constructors
74 StFgtQaCorrelationPlotMaker::StFgtQaCorrelationPlotMaker( const Char_t* name,
75  Short_t discId,
76  Short_t quadId,
77  const Char_t* quadName ) :
78  StFgtQaMaker( name, discId, quadId, quadName ),
79  mComputeCor(1), mCovHist(0), mCorHist(0), mMatrix(0), mSum(0), mN(0) {
80 
81  // flag that will ignore peds from external sources
82  mDoSubtrPeds = 0;
83 };
84 
85 StFgtQaCorrelationPlotMaker::StFgtQaCorrelationPlotMaker(const StFgtQaCorrelationPlotMaker&){
86  std::cerr << "TODO" << endl;
87  throw 0;
88 };
89 
90 // deconstructor
91 StFgtQaCorrelationPlotMaker::~StFgtQaCorrelationPlotMaker(){
92  if( mCovHist )
93  delete mCovHist;
94  if( mCorHist )
95  delete mCorHist;
96  if( mMatrix ){
97  for( Int_t i=0; i<mNbins; ++i )
98  delete[] mMatrix[i];
99  delete[] mMatrix;
100  };
101  if( mSum )
102  delete mSum;
103  if( mN )
104  delete mN;
105 };
106 
107 // equals operator
108 StFgtQaCorrelationPlotMaker& StFgtQaCorrelationPlotMaker::operator=(const StFgtQaCorrelationPlotMaker&){
109  std::cerr << "TODO" << endl;
110  throw 0;
111 };
112 
113 Int_t StFgtQaCorrelationPlotMaker::Init(){
114  Int_t ierr = StFgtQaMaker::Init();
115 
116  if( mDoVsStrip == 'r' )
117  mDoVsStrip = 'R'; // combine 'r' and 'R'
118 
119  if( !ierr ){
120  // Take care of the histogram
121 
122  if( mCovHist )
123  delete mCovHist;
124  if( mCorHist )
125  delete mCorHist;
126  if( mMatrix ){
127  for( Int_t i=0; i<mNbins; ++i )
128  delete[] mMatrix[i];
129  delete[] mMatrix;
130  };
131  if( mSum )
132  delete[] mSum;
133  if( mN )
134  delete[] mN;
135 
136  std::string name;
137  std::stringstream ss;
138  ss << GetName() << "_" << mDiscId << "_" << mQuadId << "_" << mDoVsStrip << mComputeCor;
139  ss >> name;
140 
141  //cout << "title = " << title << endl;
142 
143  mXmin = 0;
144  mXmax = 1280;
145  mNbins = 1280;
146  if( mDoVsStrip == 'R' || mDoVsStrip == 'P' ){
147  mXmin = 0;
148  mXmax = 720;
149  };
150 
151  mXbins = mXmax;
152 
153  mNbins = mXbins/mBinFactorX;
154  mCovHist = new TH2F( name.data(), "", mNbins, mXmin, mXmax, mNbins, mXmin, mXmax );
155 
156  cout << "Size of TH2F is " << mNbins << ' ' << mXmin << ' ' << mXmax << endl;
157 
158  std::string title;
159  getTitle( 0, title );
160  mCovHist->SetTitle( title.data() );
161 
162  if( mComputeCor ){
163  mCorHist = new TH2F( (name + "_cor").data(), "", mNbins, mXmin, mXmax, mNbins, mXmin, mXmax );
164  getTitle( 1, title );
165  mCorHist->SetTitle( title.data() );
166  };
167 
168  mN = new Int_t [mNbins];
169  mSum = new Float_t [mNbins];
170  mMatrix = new Float_t* [mNbins];
171  for( Int_t i=0; i<mNbins; ++i ){
172  mN[i] = 0;
173  mSum[i] = 0;
174  mMatrix[i] = new Float_t [mNbins];
175  for( Int_t j=0; j<mNbins; ++j )
176  mMatrix[i][j] = 0;
177  };
178  };
179 
180  return ierr;
181 };
182 
184  Int_t ierr = StFgtQaMaker::Make();
185 
186  // faster(?) and cleaner if copy over pointers to a vector
187  std::map< Int_t, Float_t > hitMap;
188  std::map< Int_t, Int_t > countMap;
189 
190  StFgtStripCollection *stripCollectionPtr = 0;
191  if( !ierr ){
192  stripCollectionPtr = mFgtCollectionPtr->getStripCollection( mDiscId );
193  };
194 
195  if( stripCollectionPtr ){
196  const StSPtrVecFgtStrip &stripVec = stripCollectionPtr->getStripVec();
197  StSPtrVecFgtStripConstIterator stripIter;
198  stripWeightMap_t::iterator stripMapIter;
199 
200  for( stripIter = stripVec.begin(); stripIter != stripVec.end(); ++stripIter ){
201  Int_t geoId = (*stripIter)->getGeoId();
202  Short_t adc = (*stripIter)->getAdc( mTimeBin );
203 
204  // to store position data
205  Short_t disc, quad, strip;
206  Char_t layer;
207 
208  StFgtGeom::decodeGeoId( geoId, disc, quad, layer, strip );
209 
210  if( disc == mDiscId && quad == mQuadId ){
211  Int_t idx = 0;
212  Bool_t pass = 0;
213 
214  if( mDoVsStrip == 'R' || mDoVsStrip == 'P' ){
215  pass = ( layer == mDoVsStrip );
216  idx = strip;
217  } else {
218  pass = 1;
219  Int_t rdo, arm, apv, channel;
220  StFgtCosmicTestStandGeom::getNaiveElecCoordFromGeoId(geoId,rdo,arm,apv,channel);
221  idx = 128*(apv%12) + channel;
222  };
223 
224  if( pass ){
225  idx /= mBinFactorX;
226  hitMap[idx] += adc;
227  countMap[idx] += 1;
228  };
229  };
230  };
231  };
232 
233 // if( mDoVsStrip == 'R' || mDoVsStrip == 'P' )
234 // cout << mDoVsStrip << " Number of hits = " << hitMap.size() << endl;
235 
236  // now contribute to sum
237  if( !hitMap.empty() ){
238 
239  std::map< Int_t, Float_t >::iterator iter1, iter2;
240 
241  for( iter1 = hitMap.begin(); iter1 != hitMap.end(); ++iter1 ){
242  iter1->second /= countMap[ iter1->first ];
243 
244  // count
245  ++(mN[ iter1->first ]);
246 
247  // the sum
248  // cout << "bin " << iter1->first << endl;
249  mSum[ iter1->first ] += iter1->second;
250 
251  // diagonal
252  mMatrix[ iter1->first ][ iter1->first ] += iter1->second * iter1->second;
253 
254  // lower diagonal
255  for( iter2 = hitMap.begin(); iter2 != iter1; ++iter2 ){
256  Int_t idx1 = iter1->first;
257  Int_t idx2 = iter2->first;
258  if( idx2 > idx1 ){
259  Int_t temp = idx2;
260  idx2 = idx1;
261  idx1 = temp;
262  };
263  mMatrix[ idx1 ][ idx2 ] += iter1->second * iter2->second;
264  };
265  };
266  };
267 
268  return ierr;
269 };
270 
271 // scale the histogram, fill the other triangle, and possibly convert covariance to correlation
273  Int_t ierr = kStOK;
274 
275  // expect all mN's to be the same
276  Int_t N = mN[0];
277  Int_t i;
278  for( i = 0; i < mNbins && !ierr; ++i )
279  ierr = ( mN[i] != N && mN[i] );
280 
281  if( ierr ){
282  cerr << "ERROR: number of counts per channel not consistant" << endl;
283  cerr << "Bin[0] = " << mN[0] << endl;
284  cerr << "Bin[" << i << "] = " << mN[i] << endl;
285  };
286 
287  // scale by N (faster to do in later loops, but cleaner to have it
288  // in its own loop)
289  for( Int_t i = 0; i < mNbins && !ierr; ++i ){
290  if( !N )
291  N = 1;
292 
293  mSum[i] /= N;
294 
295  for( Int_t j = 0; j <= i && !ierr; ++j )
296  mMatrix[i][j] /= N;
297  };
298 
299  // n/(n-1) factor
300  Float_t nFactor = ( N>1 ? N/((Float_t)(N-1)) : 1 );
301 
302  // copy to the histogram
303  for( Int_t i = 0; i < mNbins && !ierr; ++i ){
304  for( Int_t j = 0; j <= i && !ierr; ++j ){
305 
306  Float_t cov = mMatrix[i][j] - mSum[i]*mSum[j];
307  cov *= nFactor;
308  mCovHist->SetBinContent( i+1, j+1, cov );
309  mCovHist->SetBinContent( j+1, i+1, cov );
310  };
311  };
312 
313  if( mComputeCor ){
314  for( Int_t i = 0; i < mNbins && !ierr; ++i ){
315  Float_t sigmaSq1 = mCovHist->GetBinContent( i+1, i+1 );
316  for( Int_t j = 0; j <= i && !ierr; ++j ){
317  Float_t sigmaSq2 = mCovHist->GetBinContent( j+1, j+1 );
318  Float_t cov = mCovHist->GetBinContent( i+1, j+1 );
319  Float_t denom = sigmaSq1 * sigmaSq2;
320  denom = (denom > 0 ? sqrt(denom) : 0 );
321  Float_t cor = (denom ? cov/denom : 0);
322 
323  if( cor > 1 || cor < -1 ){
324  if( i == j ){
325  cor = 1;
326  } else {
327  ierr = 127;
328  cerr << "FATAL ERROR: abs. val. of correlation coefficient is greater than 1" << endl;
329  cerr << cor << ' ' << cov << ' ' << sigmaSq1 << ' ' << sigmaSq2 << endl;
330  cerr << mDoVsStrip << endl;
331  };
332  };
333 
334  mCorHist->SetBinContent( j+1, i+1, cor );
335  mCorHist->SetBinContent( i+1, j+1, cor );
336  };
337  };
338  };
339 
340  return ierr;
341 };
342 
343 void StFgtQaCorrelationPlotMaker::getTitle( Bool_t isCor, std::string& title ){
344  std::stringstream ss;
345 
346  if( mDoVsStrip == 'R' )
347  ss << "r-strips: ";
348  else if( mDoVsStrip == 'P' )
349  ss << "#phi-strips: ";
350 
351  if( isCor )
352  ss << "Correlation Coefficients ";
353  else
354  ss << "Covariance ";
355 
356  if( mDoVsStrip == 'R' || mDoVsStrip == 'P' )
357  ss << "vs. Strip";
358  else
359  ss << "vs. Channel";
360 
361  ss << ", Quad " << mQuadName;
362 
363  if( mDoVsStrip == 'R' )
364  ss << "; r strip id.";
365  else if( mDoVsStrip == 'P' )
366  ss << "; #phi strip id.";
367  else
368  ss << "; 128x(APV Num) + Channel Id.";
369 
370  title = ss.str();
371 };
372 
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
Definition: Stypes.h:40
virtual Int_t Make()