StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StSvtEmbeddingMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StSvtEmbeddingMaker.cxx,v 1.15 2009/01/22 23:19:21 fine Exp $
4  *
5  * Author: Selemon Bekele
6  ***************************************************************************
7  *
8  * Description: Svt Embedding Maker class
9  *
10  ***************************************************************************
11  *
12  * $Log: StSvtEmbeddingMaker.cxx,v $
13  * Revision 1.15 2009/01/22 23:19:21 fine
14  * Prptection against fo the crash
15  *
16  * Revision 1.14 2009/01/22 22:45:45 fine
17  * Add the fatal message to stop the chain
18  *
19  * Revision 1.13 2007/12/24 17:38:59 fisyak
20  * spelling error StSvtRmsPedestal => StSvtRMSPedestal
21  *
22  * Revision 1.12 2007/07/12 20:18:18 fisyak
23  * read Db by deman
24  *
25  * Revision 1.11 2007/04/28 17:57:11 perev
26  * Redundant StChain.h removed
27  *
28  * Revision 1.10 2005/07/11 19:20:56 caines
29  * Add in shift due to pasa response that is accounted for in the real data t0 calc.
30  *
31  * Revision 1.9 2005/02/09 14:33:35 caines
32  * New electron expansion routine
33  *
34  * Revision 1.5 2004/02/24 15:53:21 caines
35  * Read all params from database
36  *
37  * Revision 1.4 2004/01/22 16:30:47 caines
38  * Getting closer to a final simulation
39  *
40  * Revision 1.3 2003/11/30 20:51:46 caines
41  * New version of embedding maker and make OnlSeqAdj a stand alone maker
42  *
43  * Revision 1.2 2003/09/07 03:49:06 perev
44  * gcc 3.2 + WarnOff
45  *
46  * Revision 1.1 2003/07/31 19:18:09 caines
47  * Petrs improved simulation code
48  *
49  **************************************************************************/
50 
51 #include "Stiostream.h"
52 #include <cmath>
53 using namespace std;
54 
55 #include "TH2.h"
56 #include "TFile.h"
57 
58 #include "St_DataSetIter.h"
59 #include "St_ObjectSet.h"
60 #include "StSequence.hh"
61 #include "StSvtEmbeddingMaker.h"
62 #include "StMessMgr.h"
63 
64 #include "StSvtClassLibrary/StSvtHybridCollection.hh"
65 #include "StSvtClassLibrary/StSvtHybridData.hh"
66 #include "StSvtClassLibrary/StSvtData.hh"
67 #include "StSvtClassLibrary/StSvtHybridPixelsD.hh"
68 #include "StSvtClassLibrary/StSvtHybridPixels.hh"
69 #include "StSvtClassLibrary/StSvtConfig.hh"
70 #include "StSvtClassLibrary/StSvtHybridPed.hh"
71 #include "StDAQMaker/StSVTReader.h"
72 
73 ClassImp(StSvtEmbeddingMaker)
79 #define cDefaultBckgRMS 1.8
80 
81 
82 //____________________________________________________________________________
84 {
86  mDoEmbedding=kTRUE;
87  mPlainSimIfNoSVT=kFALSE;
88  setBackGround(kTRUE,cDefaultBckgRMS);//sets to default true and sigma 1.8
89  SetPedRmsPreferences(kTRUE, kTRUE); //read database
90 
91 
92  mSimPixelColl = NULL;
93  mRealDataColl = NULL;
94  mPedColl = NULL;
95  mPedRMSColl = NULL;
96 }
97 
98 //____________________________________________________________________________
99 StSvtEmbeddingMaker::~StSvtEmbeddingMaker()
100 {
101 
102 }
103 
104 //____________________________________________________________________________
105 Int_t StSvtEmbeddingMaker::Init()
106 {
107  return StMaker::Init();
108 }
109 
110 //____________________________________________________________________________
112 Int_t StSvtEmbeddingMaker::InitRun(int runumber)
113 {
114  if (mUsePixelRMS){
115  gMessMgr->Warning()<<"StSvtEmbeddingMaker - reading individual pixel RMS values from database "<<endm;
116  mPedRMSColl= (StSvtHybridCollection*) ((TObjectSet *) GetDataSet("StSvtRMSPedestal"))->GetObject();
117  if (mPedRMSColl) {gMessMgr->Warning()<<"StSvtEmbeddingMaker: Found RMS values for individual pixels."<<endm;}
118  else {gMessMgr->Warning()<<"StSvtEmbeddingMaker: NO RMS values for individual pixels."<<endm;}
119  }
120 
121  if (mUseHybridRMS){
122  gMessMgr->Warning()<<"StSvtEmbeddingMaker - reading individual hybrid RMS values from database "<<endm;
123  mPedColl= (StSvtHybridCollection*) ((TObjectSet *) GetDataSet("StSvtRMSPedestal"))->GetObject();
124  if (mPedColl) {gMessMgr->Warning()<<"StSvtEmbeddingMaker: Found RMS values for individual hybrids."<<endm;}
125  else {gMessMgr->Warning()<<"StSvtEmbeddingMaker: NO RMS values for individual hybrids."<<endm;}
126  }
127  if ((!mPedRMSColl)&&(!mPedColl))
128  {gMessMgr->Warning()<<"Warning: no SVT RMS information available from Chain - using default backgroung:"<<mBackGSigma<<endm;}
129 
130  return StMaker::InitRun(runumber);
131 }
132 
133 //____________________________________________________________________________
135 {
136  //now decide in which mode to realy run, based on options
137  //this could be done in InitRun, but could there be mixed event in one run with SVT and without SVT, ie.. some trigger mix
138  mRunningEmbedding=mDoEmbedding;
139 
140  if (mDoEmbedding &&NoSvt() ){//check if SVT is present if not skip simulation
141 
142  if (mPlainSimIfNoSVT){//run plain simulation instead of embedding
143  mRunningEmbedding=kFALSE; //run plain simulation
144  }
145  else
146  { //clear data and get out
147  ClearOutputData();
148  gMessMgr->Info()<<"SVT SlowSimulation: SKIPPING THIS EVENT - no SVT in real data!!"<<endm;
149  return kStOk;
150  }
151  }
152 
153  //write out the state of simulation
154  char st[20];
155  if (mRunningEmbedding) sprintf(st,"EMBEDDING");
156  else sprintf(st,"PLAIN SIMULATION");
157  gMessMgr->Info()<<"SVT SlowSimulation is running in the state of :"<<st<<endm;
158 
159  // if (mRunningEmbedding)
160  //{
161  Int_t res;
162  res=GetSvtData();
163  if (res!=kStOk) return res;
164  //}
165 
166  ClearMask(); //it has to be cleared here - needed for plain simulation
167  for(int Barrel = 1;Barrel <= mSimPixelColl->getNumberOfBarrels();Barrel++) {
168  for (int Ladder = 1;Ladder <= mSimPixelColl->getNumberOfLadders(Barrel);Ladder++) {
169  for (int Wafer = 1;Wafer <= mSimPixelColl->getNumberOfWafers(Barrel);Wafer++) {
170  for( int Hybrid = 1;Hybrid <= mSimPixelColl->getNumberOfHybrids();Hybrid++){
171  mCurrentIndex = mSimPixelColl->getHybridIndex(Barrel,Ladder,Wafer,Hybrid);
172  if( mCurrentIndex < 0) continue;
173 
174  mCurrentPixelData = (StSvtHybridPixelsD*)mSimPixelColl->at(mCurrentIndex);
175 
176  if(!mCurrentPixelData) { //no data from simulation Maker
177  gMessMgr->Error()<<"Error in StSvtEmbeddingMaker::Make(): Something is wrong, no data from simulator for hybrid index:"<<mCurrentIndex<<endm;
178  mCurrentPixelData = new StSvtHybridPixelsD(Barrel, Ladder, Wafer, Hybrid);
179  mSimPixelColl->put_at(mCurrentPixelData,mCurrentIndex);
180  }
181 
182  if (mRunningEmbedding){ //do the embedding
183  ClearMask(); //just in case..
184  AddRawData();
185  }
186 
187  if (mBackGrOption) CreateBackground();
188 
189  }
190  }
191  }
192  }
193 
194  return kStOK;
195 }
196 
197 //____________________________________________________________________________
198 Int_t StSvtEmbeddingMaker::GetSvtData()
199 {
200  //EmbeddingMaker requires some data(at least empty) from the SimulationMaker
201  mSimPixelColl=NULL;
202  mRealDataColl=NULL;
203 
204  St_DataSet* dataSet = GetDataSet("StSvtPixelData");
205  if (dataSet==NULL){
206  LOG_FATAL <<"BIG TROUBLE:No data from simulator to work with!!!!"<<endm;
207  return kStErr;
208  }
209 
210  mSimPixelColl= (StSvtData*)(dataSet->GetObject());
211  if ( mSimPixelColl==NULL){
212  gMessMgr->Error()<<"BIG TROUBLE:Data from simulator is empty!!!!"<<endm;
213  return kStErr;
214  }
215 
216  if (!mRunningEmbedding) return kStOk; //dont read real data if you don't need them
217  dataSet = GetDataSet("StSvtRawData");
218  if (dataSet) mRealDataColl= (StSvtData*)(dataSet->GetObject());
219  else gMessMgr->Error()<<"No StSvtRawData in the chain"<<endm;
220  if (!mRealDataColl) //switching to plain simulation, because there is no raw data
221  {
222  gMessMgr->Error()<<"Note: StSvtEmbeddingMaker is set to do embbeding, but found no raw data- embedding into empty event!!!"<<endm;
223  return kStErr;
224  }
225 
226  return kStOk;
227 }
228 
229 //____________________________________________________________________________
231 void StSvtEmbeddingMaker::AddRawData()
232 {
233 
235 
236  int numOfSeq;
237  int* anolist;
238 
239  StSvtHybridData* realData = (StSvtHybridData *)mRealDataColl->at(mCurrentIndex);
240 
241  double *adcArray=mCurrentPixelData->GetArray();
242  double offset = mCurrentPixelData->getPedOffset(); //this could be problem if offset differs between real and simulated data!!!
243 
244  anolist = NULL;
245  if (realData!=NULL)
246  for (int iAnode= 0; iAnode<realData->getAnodeList(anolist); iAnode++){
247 
248  int Anode = anolist[iAnode]; //goes from 1
249  realData->getSequences(Anode,numOfSeq,Sequence);
250 
251  for (int nSeq=0; nSeq< numOfSeq ; nSeq++){
252  unsigned char* adc=Sequence[nSeq].firstAdc;
253  int length = Sequence[nSeq].length;
254  int startTimeBin=Sequence[nSeq].startTimeBin;
255 
256  for(int t = 0; t<length; t++){
257  int time = startTimeBin + t;
258  unsigned char adcVal = adc[t];
259  //pixelIndex = mSimPixelData->getPixelIndex(Anode,time);
260  //less clean but faster
261  int pixelIndex=(Anode-1)*128+time;
262  //there already is pedestal offset
263  adcArray[pixelIndex]=adcArray[pixelIndex]+(double)adcVal-offset;
264 
265  //don't do noise here
266  mMask[pixelIndex]=kFALSE;
267  }
268  }
269  }
270 
271  //now clear rest of the mask
272  for(int an = 0; an < 240; an++){
273  for(int tim = 0; tim < 128; tim++){
274  int pIndex=an*128 + tim;
275  if (adcArray[pIndex]==offset) mMask[pIndex]=kFALSE; //don't make extra noise outside of hits
276  }
277  }
278 
279 }
280 
281 //____________________________________________________________________________
282 double StSvtEmbeddingMaker::MakeGaussDev(double sigma)
283 {
284 
285  double fac,rsq,v1,v2;
286 
287  do {
288  v1 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
289  v2 = 2.0*((float)rand()/(float)RAND_MAX) - 1.0;
290  rsq = v1*v1 + v2*v2;
291  } while(rsq >= 1.0 );
292 
293  fac = sigma*::sqrt(-2.0*::log(rsq)/rsq);
294 
295  // gset = v1*fac; // gset = 3.0*::sqrt(-2.0*::log(rsq))*(v1/::sqrt(rsq))
296  return v2*fac;
297 
298 }
299 
300 //____________________________________________________________________________
302 void StSvtEmbeddingMaker::CreateBackground()
303 {
304  const double rmsScale=16.;
305  double *adcArray=mCurrentPixelData->GetArray(); // array of [128*240]
306 
307  //find out what background to use
308  StSvtHybridPixels* pedRms=NULL;
309  if (mPedRMSColl)
310  {
311  pedRms = (StSvtHybridPixels*)mPedRMSColl->at(mCurrentIndex);
312  if (pedRms == NULL) {gMessMgr->Warning()<<"Warning: Individual pixel RMS info is empty for hybrid "<<mCurrentIndex<<" =>have to use other method "<<endm;}
313  }
314 
315  if(pedRms)
316  {// I have rms for each pixel
317  for(int an = 0; an < 240; an++) for(int tim = 0; tim < 128; tim++){
318  //cout<<pedRms<<"indiv rms="<<pedRms->At(pedRms->getPixelIndex(an+1,tim))/rmsScale<<endl;
319  int pIndex=an*128 + tim;
320  if (mMask[pIndex]) adcArray[pIndex]+=MakeGaussDev(pedRms->At(pedRms->getPixelIndex(an+1,tim))/rmsScale);// !! mAdcArray already contains PedOffset
321  }
322  }
323  else {
324  //one value for hybrid
325  double backgsigma;
326  StSvtHybridPed *ped=NULL;
327  if (mPedColl){
328  ped=(StSvtHybridPed *)mPedColl->at(mCurrentIndex);
329  if (ped == NULL) {gMessMgr->Warning()<<"Warning: hybrid RMS info is empty for hybrid "<<mCurrentIndex<<" =>using default value "<<mBackGSigma<<endm;}
330  }
331  if (ped) backgsigma=ped->getRMS(); else backgsigma=mBackGSigma; //the default value
332  if ((backgsigma<=0.)||(backgsigma>=6.)){ //check for obviously bad values
333  {gMessMgr->Warning()<<"Warnig for index "<<mCurrentIndex<<" pedestal RMS is:"<<backgsigma<<" => seting to default "<<mBackGSigma<<endm;}
334  backgsigma=mBackGSigma;
335  }
336  if (Debug()) {gMessMgr->Debug()<<"for index "<<mCurrentIndex<<" pedestal RMS is:"<< backgsigma<<endm;}
337 
338  for(int an = 0; an < 240; an++){
339  for(int tim = 0; tim < 128; tim++){
340  int pIndex=an*128 + tim;
341  if (mMask[pIndex]) adcArray[pIndex]+=MakeGaussDev(backgsigma);// !! mAdcArray already contains PedOffset
342  }
343  }
344  }
345 }
346 
347 //____________________________________________________________________________
349 {
350  return kStOK;
351 }
352 
353 
354 //____________________________________________________________________________
355 void StSvtEmbeddingMaker::SetPedRmsPreferences(Bool_t usePixelRMS, Bool_t useHybridRMS)
356 { // allows to disable reading RMS from database
357  mUsePixelRMS=usePixelRMS;
358  mUseHybridRMS=useHybridRMS;
359 }
360 
361 
362 //____________________________________________________________________________
363 void StSvtEmbeddingMaker::setDoEmbedding(Bool_t doIt){
364  mDoEmbedding = doIt;
365 }
366 
367 //____________________________________________________________________________
368 void StSvtEmbeddingMaker::setPlainSimEvenIfNoSVT(Bool_t doIt){
369  mPlainSimIfNoSVT= doIt;
370 }
371 //____________________________________________________________________________
372 void StSvtEmbeddingMaker::setBackGround(Bool_t backgr,double backgSigma){
373  mBackGrOption = backgr;
374  mBackGSigma = backgSigma;
375 }
376 
377 //____________________________________________________________________________
378 void StSvtEmbeddingMaker::ClearMask()
379 {
380  for (int i=0;i<128*240;i++)mMask[i]=kTRUE;
381 }
382 
383 //_____________________________________________________________
384 Int_t StSvtEmbeddingMaker::NoSvt()
385 {
386  St_DataSet *dataSet;
387 
388  dataSet = GetDataSet("StDAQReader");
389  if(!dataSet){
390  gMessMgr->Error()<<("BIG TROUBLE: cannot find StDAQReader in the chain and you want to run embedding")<<endm;
391  return kTRUE;
392  }
393  StDAQReader* daqReader = (StDAQReader*)(dataSet->GetObject());
394  if (!daqReader){
395  gMessMgr->Error()<<("BIG TROUBLE: StDAQReader is empty and you want to run embedding")<<endm;
396  return kTRUE;
397  }
398 
399  if (!daqReader->SVTPresent ())
400  {
401  gMessMgr->Info()<<("NO SVT in DAQ")<<endm;
402  return kTRUE; //No SVT in the datastream
403  }
404 
405  gMessMgr->Info()<<("SVT found in DAQ")<<endm;
406  return kFALSE;
407 }
408 
409 //_____________________________________________________________
410 //this resets mSvtSimPixelColl
411 void StSvtEmbeddingMaker::ClearOutputData()
412 {
413  StSvtHybridPixelsD* tmpPixels;
414  if (mSimPixelColl) {
415  for(int Barrel = 1;Barrel <= mSimPixelColl->getNumberOfBarrels();Barrel++) {
416  for (int Ladder = 1;Ladder <= mSimPixelColl->getNumberOfLadders(Barrel);Ladder++) {
417  for (int Wafer = 1;Wafer <= mSimPixelColl->getNumberOfWafers(Barrel);Wafer++) {
418  for( int Hybrid = 1;Hybrid <= mSimPixelColl->getNumberOfHybrids();Hybrid++){
419 
420  int index = mSimPixelColl->getHybridIndex(Barrel,Ladder,Wafer,Hybrid);
421  if( index < 0) continue;
422 
423  tmpPixels = (StSvtHybridPixelsD*)mSimPixelColl->at(index);
424 
425  if(!tmpPixels) {
426  tmpPixels = new StSvtHybridPixelsD(Barrel, Ladder, Wafer, Hybrid);
427  mSimPixelColl->put_at(tmpPixels,index);
428  }
429  tmpPixels->setPedOffset(0);
430  tmpPixels->reset();
431  }
432  }
433  }
434  }
435  } else {
436  LOG_ERROR << "StSvtEmbeddingMaker::ClearOutputData(): There is no SVT data to clear" << endm;
437  }
438 }
virtual Int_t InitRun(int runumber)
All database dependent data are read here.
This maker is resposible for embedding SVT response simulation into the SVT real data and/or creation...
virtual TObject * GetObject() const
The depricated method (left here for the sake of the backward compatibility)
Definition: TDataSet.cxx:428
Definition: Stypes.h:40
StSvtEmbeddingMaker(const char *name="SvtEmbedding")
Definition: Stypes.h:44
Definition: Stypes.h:41