StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFmsHitMaker.cxx
1 // ROOT
2 #include "TFile.h"
3 #include "TH1.h"
4 #include "TH2.h"
5 #include "TTree.h"
6 
7 #include "StMessMgr.h"
8 #include "StEventTypes.h"
9 #include "StMuDSTMaker/COMMON/StMuTypes.hh"
10 #include "StMuDSTMaker/COMMON/StMuFmsUtil.h"
11 
12 #include "StFmsDbMaker/StFmsDbMaker.h"
13 #include "StFmsCollection.h"
14 #include "StFmsHit.h"
15 #include "StFmsHitMaker.h"
16 #include "StTriggerData2009.h"
17 
18 // PSU-FMS package
19 //#include "StFmsPointMaker/StFmsClusterFitter.h"
20 
21 #include <iostream>
22 #include <fstream>
23 #include <stdlib.h>
24 using namespace std;
25 
26 ClassImp(StFmsHitMaker);
27 
28 StFmsHitMaker::StFmsHitMaker(const char* name) : StMaker(name) {
29  mFmsDbMaker = NULL;
30  mFmsCollection = NULL;
31  mMuFmsColl = NULL;
32  LOG_DEBUG << "StFmsHitMaker::constructor." << endm;
33 }
34 
35 StFmsHitMaker::~StFmsHitMaker(){
36  LOG_DEBUG << "StFmsHitMaker::destructor." << endm;
37 }
38 
39 void StFmsHitMaker::Clear(Option_t* option){
40  LOG_DEBUG << "StFmsHitMaker::Clear()" << endm;
41  StMaker::Clear(option);
42 }
43 
44 int StFmsHitMaker::Init() {
45  LOG_DEBUG<<"StFmsHitMaker::Init() "<<endm;
46  return StMaker::Init();
47 }
48 
49 int StFmsHitMaker::InitRun(Int_t runNumber) {
50  LOG_INFO << "StFmsHitMaker::InitRun --run# changed to " << runNumber << endm;
51  mFmsDbMaker = static_cast<StFmsDbMaker*>(GetMaker("fmsDb"));
52  if(!mFmsDbMaker){
53  LOG_ERROR << "StFmsHitMaker::InitRun Failed to get StFmsDbMaker" << endm;
54  return kStFatal;
55  }
56  mCurrentRunNumber = runNumber; //called by maker's method :InitRun(run); when the run# changes
57  return kStOK;
58 }
59 
62  LOG_DEBUG<<"StFmsHitMaker::Make start"<<endm;
63  int flag = 0;
64  StTriggerData* triggerData = 0;
65  Float_t mCurrentEventNumber=0;
66 
67  if(mReadMuDst>0) return readMuDst();
68 
69  //first try to get StTriggerData from StTriggerDataMaker (works for proudction) and create StFmsCollection
70  TObjectSet *os = (TObjectSet*)GetDataSet("StTriggerData");
71  if (os) {
72  triggerData = (StTriggerData*)os->GetObject();
73  if(triggerData){
74  flag=1;
75  mCurrentEventNumber=triggerData->eventNumber();
76  // mFmsCollection = new StFmsCollection();
77  LOG_DEBUG<<"StFmsHitMaker::Make Found StTriggerData from StTriggerDataMaker"<<endm;
78  }
79  }
80 
81  //2nd try to get StTriggerData from StEvent
82  //but once FMS data is killed in StEvent, this will not work and all you see is empty data
83  StEvent* stEvent = (StEvent*) GetInputDS("StEvent");
84  if(flag==0){
85  if(stEvent){
86  mCurrentEventNumber=stEvent->id();
87  triggerData = stEvent->triggerData();
88  if(triggerData) {
89  flag=2;
90  LOG_DEBUG<<"StFmsHitMaker::Make Found StTriggerData from StEvent"<<endm;
91  }
92  else{
93  mFmsCollection = stEvent->fmsCollection();
94  if(mFmsCollection){
95  flag=3;
96  LOG_DEBUG<<"StFmsHitMaker::Make Found StFmsCollection from StEvent"<<endm;
97  }
98  }
99  } //found StEvent
100  }
101 
102  //3rd try to get StTriggerData from StMuEvent, works for produced data (.MuDst.root) --Yuxi
103  StMuDst* muDst = (StMuDst*)GetInputDS("MuDst");
104  if(flag==0){
105  if(muDst && muDst->event()){
106  mCurrentRunNumber = muDst->event()->runNumber();
107  mCurrentEventNumber = muDst->event()->eventNumber();
108  triggerData = (StTriggerData*)StMuDst::event()->triggerData();
109  if(triggerData){
110  flag = 4; //Yuxi
111  LOG_DEBUG<<"StFmsHitMaker::Make Found StFmsTriggerData in MuDst"<<endm;
112  }
113  else LOG_ERROR << "Finally, no StFmsTriggerData in MuDst " <<endm;
114  }
115  }
116  LOG_DEBUG<<"Flag="<<flag<<" (0=Not found, 1=StTriggerDataMaker, 2=StEvent, 3=FmsCollection 4=Mudst)"<<endm;
117  //after this step triggerData is pointing to StTriggerData block of StEvent
118 
119  if(flag>0){
120  mFmsCollection = new StFmsCollection();
121  //create StFmsHit and add it to StFmsCollection
122  for(unsigned short crt=1; crt<=4; crt++){
123  for(unsigned short slot=1; slot<=16; slot++){
124  for(unsigned short ch=0; ch<32; ch++){
125  unsigned short adc=0;
126  unsigned short tdc=0;
127  if(flag<=4 && triggerData){ //wont work when flag=3
128  adc=triggerData->fmsADC(crt,slot-1,ch);
129  tdc=triggerData->fmsTDC(crt,slot-1,ch);
130  }
131 
132  if(adc>0 || tdc>0){
133  // LOG_INFO<<"adc of crt "<<crt<<", slot "<<slot<<", channel "<<ch<<" is: "<<adc<<endm;
134  // LOG_INFO<<"tdc=====================================================is: "<<tdc<<endm;
135  StFmsHit* hit = new StFmsHit();
136  if(!hit){
137  LOG_ERROR <<"Failed to create FMS hit, skip this hit."<<endm;
138  continue;
139  }
140  hit->setDetectorId(0);
141  hit->setChannel(0);
142  hit->setQtCrate(crt);
143  hit->setQtSlot(slot);
144  hit->setQtChannel(ch);
145  hit->setAdc(adc);
146  hit->setTdc(tdc);
147  hit->setEnergy(0.0);
148  mFmsCollection->addHit(hit);
149  if(GetDebug()>0) hit->print();
150  }
151  }
152  }
153  }
154 
156  for(unsigned int i=0; i<mFmsCollection->numberOfHits(); i++){
157  int d,c;
158  StFmsHit* fmsHit = (mFmsCollection->hits())[i];
159  int crt =fmsHit->qtCrate();
160  int slot =fmsHit->qtSlot();
161  int ch =fmsHit->qtChannel();
162  unsigned short adc =fmsHit->adc();
163  mFmsDbMaker->getReverseMap(crt,slot,ch,&d,&c);
164  float e=0.0;
165  if(d>0 || c>0){
166  //unsigned short rawadc=adc;
167  short bitshift=0;
168  if(mCorrectAdcOffByOne){
169  bitshift = mFmsDbMaker->getBitShiftGain(d,c);
170  if(bitshift>0){
171  int check=adc % (1<<bitshift);
172  if(check!=0){
173  LOG_ERROR << Form("Bitshift in DB is not consistent with data! det=%2d ch=%3d adc=%4d bitshift=%2d adc%(1<<bitshift)=%d",
174  d,c,adc,bitshift,check) << endm;
175  }
176  }else if(bitshift<0){
177  int check=adc / (1<< (12+bitshift));
178  if(check!=0){
179  LOG_ERROR << Form("Bitshift in DB is not consistent with data! det=%2d ch=%3d adc=%4d bitshift=%2d adc/(1<<(12+bitshift))=%d",
180  d,c,adc,bitshift,check) << endm;
181  }
182  }
183  //Leaving ADC value in StFmsHit as it was recorded, so that when we read from MuDST, we don't double correct!!!!
184  // if(bitshift>=0) {
185  // adc += (0x1<<bitshift);
186  // fmsHit->setAdc(adc);
187  // }
188  //LOG_INFO << Form("RawADC=%4d NewADC=%4d Bitshift=%d",rawadc,adc,bitshift) << endm;
189  }
190  float g1=mFmsDbMaker->getGain(d,c);
191  float g2=mFmsDbMaker->getGainCorrection(d,c);
192  float gt=1.0;
193  if(mTimeDepCorr==1){ // time dep. Correction
194  gt = mFmsDbMaker->getTimeDepCorr(mCurrentEventNumber,d-8,c);
195  if(gt<0){
196  if(mTowerRej==1){
197  gt = 0; // making -ve(tower to be rej) to zero
198  }else{
199  gt = -gt; // making +ve : doing time dep corr. for all towers
200  }
201  }
202  // cout<<d<<" "<<ch<<" "<<gt<<endl;
203  }
204 
205  if(mCorrectAdcOffByOne){
206  e=(adc+pow(2.0,bitshift))*g1*g2*gt;
207  }else{
208  e=adc*g1*g2*gt;
209  }
210  }
211  fmsHit->setDetectorId(d);
212  fmsHit->setChannel(c);
213  fmsHit->setEnergy(e);
214  if(GetDebug()>0) fmsHit->print();
215  }
216  LOG_INFO<<"StFmsHitMaker::Make(): flag = "<<flag<<", got "<<mFmsCollection->numberOfHits()<<" hits in StFmsCollection"<<endm;
217  }else{
218  LOG_INFO<<"StFmsHitMaker::Make(): flag = "<<flag<<", no StTrigger data found"<<endm;
219  }
220 
221  if(stEvent) {
222  //Adding StFmsCollection to StEvent
223  LOG_DEBUG<<"StFmsHitMaker::Make Adding StFmsCollection to StEvent"<<endm;
224  stEvent->setFmsCollection(mFmsCollection);
225  }else{
226  LOG_INFO << "StEvent is empty" << endm;
227  }
228 
229  return kStOk;
230 }
231 
233  LOG_DEBUG << "StFmsHitMaker::Finish() " << endm;
234  return kStOk;
235 }
236 
237 Int_t StFmsHitMaker::readMuDst(){
238  StEvent* event = (StEvent*)GetInputDS("StEvent");
239  if(!event){LOG_ERROR<<"StFmsHitMaker::readMuDst found no StEvent"<<endm; return kStErr;}
240  StFmsCollection* fmsColl = event->fmsCollection();
241  if(!fmsColl){
242  fmsColl=new StFmsCollection;
243  event->setFmsCollection(fmsColl);
244  }
245  StMuDst* mudst = (StMuDst*)GetInputDS("MuDst");
246  if(!mudst){LOG_ERROR<<"StFmsHitMaker::readMuDst found no MuDst"<<endm; return kStErr;}
247  StMuFmsCollection* mufmsColl= mudst->muFmsCollection();
248  if(!mufmsColl){LOG_ERROR<<"StFmsHitMaker::readMuDst found no MuFmsCollection"<<endm; return kStErr;}
249  StMuFmsUtil util;
250  util.fillFms(fmsColl,mufmsColl);
251  return kStOk;
252 }
static StMuFmsCollection * muFmsCollection()
returns pointer to current StMuFmsCollection
Definition: StMuDst.h:391
Float_t getGainCorrection(Int_t detectorId, Int_t ch) const
get the gain for the channel
virtual void Clear(Option_t *option="")
User defined functions.
Definition: StMaker.cxx:634
float getTimeDepCorr(int event, int det, int ch)
force bit shift to be read from FmsBitShiftGain.txt
void Clear(Option_t *option="")
User defined functions.
Short_t getBitShiftGain(Int_t detectorId, Int_t ch) const
get the gain correction for the channel
StFmsHitMaker.
Definition: StFmsHitMaker.h:27
static StMuEvent * event()
returns pointer to current StMuEvent (class holding the event wise information, e.g. event number, run number)
Definition: StMuDst.h:320
Definition: Stypes.h:40
virtual TObject * GetObject() const
The depricated method (left here for the sake of the backward compatibility)
Definition: TObjectSet.h:56
Definition: Stypes.h:44
Definition: Stypes.h:41