StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFgtPedMaker.cxx
1 
6 /***************************************************************************
7  *
8  * $Id: StFgtPedMaker.cxx,v 1.2 2012/01/31 16:47:47 wwitzke Exp $
9  * Author: S. Gliske, Sept 2011
10  *
11  ***************************************************************************
12  *
13  * Description: See header
14  *
15  ***************************************************************************
16  *
17  * $Log: StFgtPedMaker.cxx,v $
18  * Revision 1.2 2012/01/31 16:47:47 wwitzke
19  * Changed for cosmic test stand change.
20  *
21  * Revision 1.1 2012/01/31 08:52:51 sgliske
22  * StFgtPedMaker moved to StFgtPool
23  *
24  * Revision 1.12 2012/01/30 10:42:22 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.11 2012/01/26 13:13:11 sgliske
30  * Updated to use StFgtConsts, which
31  * replaces StFgtEnums and StFgtGeomDefs
32  *
33  * Revision 1.10 2012/01/18 18:53:01 sgliske
34  * minor bug fix
35  *
36  * Revision 1.9 2012/01/18 18:07:28 sgliske
37  * directly use elec coord domian--no geoIds
38  *
39  * Revision 1.8 2012/01/17 21:56:26 sgliske
40  * Short_t geoId -> Int_t geoId
41  *
42  * Revision 1.7 2012/01/17 20:08:20 sgliske
43  * Many updates
44  *
45  * Revision 1.6 2011/11/01 18:54:55 sgliske
46  * Updated to correspond with StEvent containers, take 2.
47  *
48  * Revision 1.5 2011/09/30 19:08:45 sgliske
49  * general update
50  *
51  * Revision 1.4 2011/09/27 00:49:00 sgliske
52  * cosmic QA update
53  *
54  * Revision 1.3 2011/09/26 16:55:52 sgliske
55  * Continued work on cosmic QA plots
56  *
57  * Revision 1.2 2011/09/22 21:21:05 sgliske
58  * first working version
59  *
60  * Revision 1.1 2011/09/22 14:10:13 sgliske
61  * minor update
62  *
63  *
64  **************************************************************************/
65 
66 #include <string>
67 #include "StFgtPedMaker.h"
68 #include "StRoot/StEvent/StFgtCollection.h"
69 #include "StRoot/StEvent/StFgtStrip.h"
70 #include "StRoot/StEvent/StEvent.h"
71 #include "StRoot/StFgtDbMaker/StFgtDbMaker.h"
72 #include "StRoot/StFgtPool/StFgtCosmicTestStandGeom/StFgtCosmicTestStandGeom.h"
73 #include "StRoot/StFgtUtil/StFgtConsts.h"
74 
75 // constructor
76 StFgtPedMaker::StFgtPedMaker( const Char_t* name ) : StMaker( name ), mHasFinished(0) {
77  // set to all zeros
78  mDataVec.resize( kFgtNumTimeBins * kFgtNumElecIds );
79 };
80 
81 // initialize
82 Int_t StFgtPedMaker::Init(){
83  Int_t ierr = kStOk;
84 
85  if( !mFilename.empty() ){
86  // make sure the file can be opened
87  std::ofstream fout( mFilename.data() );
88  if( !fout ){
89  LOG_ERROR << "Error opening file '" << mFilename << "'" << endl;
90  ierr = kStFatal;
91  };
92  };
93 
94  // set db pointer, if needed
95  if( !ierr && !mDbMkrName.empty() ){
96  // get the maker pointer
97  mFgtDbMkr = static_cast< StFgtDbMaker* >( GetMaker( mDbMkrName.data() ) );
98 
99  if( !ierr && !mFgtDbMkr ){
100  LOG_FATAL << "Error finding FgtDbMkr" << endm;
101  ierr = kStFatal;
102  };
103 
104  if( !mFgtDbMkr->InheritsFrom("StFgtDbMaker") ){
105  LOG_FATAL << "StFgtDbMkr does not inherit from StFgtDbMaker" << endm;
106  LOG_FATAL << "Name is '" << mFgtDbMkr->GetName() << "', class is '" << mFgtDbMkr->ClassName() << endm;
107  ierr = kStFatal;
108  };
109  };
110 
111  return ierr;
112 };
113 
114 // make, i.e. compute sums.
115 // actual pedistals are computed in ::Finish()
117  Int_t ierr = kStOk;
118 
119  StEvent* eventPtr = 0;
120  eventPtr = (StEvent*)GetInputDS("StEvent");
121 
122  if( !eventPtr ) {
123  LOG_ERROR << "Error getting pointer to StEvent from '" << ClassName() << "'" << endm;
124  ierr = kStErr;
125  };
126 
127  StFgtCollection* fgtCollectionPtr = 0;
128 
129  if( eventPtr ) {
130  fgtCollectionPtr=eventPtr->fgtCollection();
131  };
132 
133  if( !fgtCollectionPtr) {
134  LOG_ERROR << "Error getting pointer to StFgtCollection from '" << ClassName() << "'" << endm;
135  ierr = kStErr;
136  };
137 
138  if( !ierr ){
139  for( UInt_t discIdx=0; discIdx<fgtCollectionPtr->getNumDiscs(); ++discIdx ){
140  StFgtStripCollection *stripCollectionPtr = fgtCollectionPtr->getStripCollection( discIdx );
141  if( stripCollectionPtr ){
142  StSPtrVecFgtStrip& stripVec = stripCollectionPtr->getStripVec();
143  StSPtrVecFgtStripIterator stripIter;
144 
145  for( stripIter = stripVec.begin(); stripIter != stripVec.end(); ++stripIter ){
146  for( Int_t timeBin = 0; timeBin < kFgtNumTimeBins; ++timeBin ){
147  Bool_t pass = (( 1<<timeBin & mTimeBinMask ) && timeBin > 0 && timeBin < kFgtNumTimeBins);
148 
149  if( pass ){
150  Int_t rdo, arm, apv, channel;
151  (*stripIter)->getElecCoords( rdo, arm, apv, channel );
152  Int_t elecId = StFgtGeom::getElectIdFromElecCoord( rdo, arm, apv, channel );
153  Short_t adc = (*stripIter)->getAdc( timeBin );
154 
155  if( adc ){
156  Int_t code = kFgtNumTimeBins * elecId + timeBin;
157 
158  pedData_t &data = mDataVec[ code ];
159  ++data.n;
160  data.sum += adc;
161  data.sumsq += adc*adc;
162  };
163  };
164  };
165  };
166  };
167  };
168  };
169 
170  return ierr;
171 };
172 
173 // save as needed
175  Int_t ierr = kStOk;
176 
177  if( !mHasFinished ){
178  mHasFinished = 1;
179  cout << "StFgtPedMaker::Finish()" << endl;
180 
181  // first thing is to finalize the sum;
182  pedDataVec_t::iterator dataVecIter;
183  for( dataVecIter = mDataVec.begin(); dataVecIter != mDataVec.end(); ++dataVecIter ){
184  //Int_t timebin = dataVecIter->first % kFgtNumTimeBins;
185  Int_t n = dataVecIter->n;
186 
187  if( n ){
188  // take average
189  dataVecIter->sum /= n;
190 
191  // compute st. dev.
192  dataVecIter->sumsq /= n;
193  dataVecIter->sumsq -= dataVecIter->sum * dataVecIter->sum;
194  dataVecIter->sumsq = sqrt( dataVecIter->sumsq );
195 
196  // Note: now sumsq is st. dev. and sum is average
197 
198  // check for nan
199  if( dataVecIter->ped != dataVecIter->ped ){
200  dataVecIter->ped = 0;
201  dataVecIter->RMS = 10000;
202  } else if ( dataVecIter->RMS != dataVecIter->RMS ){
203  dataVecIter->RMS = 10000;
204  };
205  };
206  };
207 
208  ierr = saveToFile();
209  };
210 
211  return ierr;
212 };
213 
214 // functions that actually do the saving
215 Int_t StFgtPedMaker::saveToFile(){
216  Int_t ierr = kStOk;
217 
218  if( !mFilename.empty() ){
219  std::ofstream fout( mFilename.data(), std::ios_base::out & std::ios_base::trunc );
220 
221  StFgtDb *fgtTables = 0;
222  if( !mDbMkrName.empty() ){
223  if( !mFgtDbMkr ){
224  LOG_FATAL << "Pointer to Fgt DB Maker is null" << endm;
225  ierr = kStFatal;
226  };
227  if( !ierr ){
228  fgtTables = mFgtDbMkr->getDbTables();
229 
230  if( !fgtTables ){
231  LOG_FATAL << "Pointer to Fgt DB Tables is null" << endm;
232  ierr = kStFatal;
233  };
234  };
235  };
236 
237  if( !fout ){
238  LOG_ERROR << "Error opening file '" << mFilename << "'" << endm;
239  //cerr << "Error opening file '" << mFilename << "'" << endl;
240  ierr = kStFatal;
241  };
242 
243  pedDataVec_t::iterator dataVecIter;
244  Int_t idx = 0;
245  for( dataVecIter = mDataVec.begin(); dataVecIter != mDataVec.end() && !ierr; ++dataVecIter, ++idx ){
246  Int_t timebin = idx % kFgtNumTimeBins;
247  Int_t elecId = idx / kFgtNumTimeBins;
248 
249  if( dataVecIter->n && ( dataVecIter->ped || dataVecIter->RMS ) )
250  fout << elecId << ' ' << timebin << ' ' << dataVecIter->ped << ' ' << dataVecIter->RMS << endl;
251  };
252  };
253 
254  return ierr;
255 };
256 
257 ClassImp( StFgtPedMaker );
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
Definition: Stypes.h:44
Definition: Stypes.h:41