StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEmcCalibMaker.cxx
1 #include "StEmcCalibMaker.h"
2 
3 #include <TFile.h>
4 #include <TROOT.h>
5 #include <TF1.h>
6 
7 #include <StEventTypes.h>
8 #include <StEvent.h>
9 #include <StEmcUtil/geometry/StEmcGeom.h>
10 #include <StEmcUtil/voltageCalib/VoltCalibrator.h>
11 #include <StDbLib/StDbManager.hh>
12 #include <StDbLib/StDbTable.h>
13 #include <StDbLib/StDbConfigNode.hh>
14 #include <tables/St_emcCalib_Table.h>
15 
16 ClassImp(StEmcCalibMaker)
17 
18 //_____________________________________________________________________________
19 StEmcCalibMaker::StEmcCalibMaker(const Char_t *name)
20  :StMaker(name)
21  {
22  mCalib = NULL;
23  mAccept = NULL;
24  mSpec = NULL;
25  mDate = 20330101;
26  mNMinTracks = 0;
27  mNMaxTracks = 100000;
28  mDebug = false;
29  setDetector(1);
30  setRange(200);
31  mZDCMin = -1000000;
32  mZDCMax = 1000000;
33  mCTBMin = -1000000;
34  mCTBMax = 1000000;
35  mSpecName = "mSpec";
36  mAcceptName = "mAccept";
37  setFile("spec.root");
38 }
39 //_____________________________________________________________________________
40 StEmcCalibMaker::~StEmcCalibMaker()
41 {
42 }
43 //_____________________________________________________________________________
44 Int_t StEmcCalibMaker::Init()
45 {
46  mSpec = new TH2F(mSpecName.Data(),"Equal Spectra",mNChannel,+0.5,(float)mNChannel+0.5,(int)mRange,-0.5,mRange-0.5);
47  mAccept = new TH1F(mAcceptName.Data(),"Accepted events",20,-0.5,19.5);
48  return StMaker::Init();
49 }
50 //_____________________________________________________________________________
51 void StEmcCalibMaker::Clear(Option_t *option)
52 {
53 }
54 //_____________________________________________________________________________
56 {
57  return kStOK;
58 }
59 //_____________________________________________________________________________
60 bool StEmcCalibMaker::accept()
61 {
62  mAccept->Fill(0);
63  if(!mCalib) mCalib = (StEmcCalibrationMaker*)GetMaker("Calib");
64  if(!mCalib) return false;
65  mAccept->Fill(1);
66 
67  if(!mCalib->hasDetector(getDetector())) return false;
68  mAccept->Fill(2);
69 
70  if(mCalib->getNTracks()<mNMinTracks) return false;
71  mAccept->Fill(3);
72 
73  if(mCalib->getNTracks()>mNMaxTracks) return false;
74  mAccept->Fill(4);
75 
76  if(GetDate()<mDate) { mDate = GetDate(); mTime = GetTime();}
77 
78  if(!mCalib->isTriggerOk()) return false;
79  mAccept->Fill(5);
80 
81  long CTB = mCalib->getCTBSum();
82  long ZDC = mCalib->getZDCSum();
83 
84  if(ZDC<mZDCMin || ZDC > mZDCMax) return false;
85  mAccept->Fill(6);
86 
87  if(CTB<mCTBMin || CTB > mCTBMax) return false;
88  mAccept->Fill(7);
89 
90 
91  mNEvents++;
92  if(mNEvents%100==0) saveHist(mFileName.Data());
93 
94  //cout <<"Processing event number "<<mNEvents<<" for maker "<<GetName()<<endl;
95  return true;
96 }
97 //_____________________________________________________________________________
99 {
100  saveHist(mFileName.Data());
101  return StMaker::Finish();
102 }
103 //_____________________________________________________________________________
104 void StEmcCalibMaker::saveHist(const Char_t *file)
105 {
106  cout << "Saving spec to file " << file << endl;
107  TFile *f = new TFile(file, "RECREATE");
108  mSpec->Write();
109  f->Close();
110  delete f;
111  return ;
112 }
113 //_____________________________________________________________________________
114 void StEmcCalibMaker::loadHist(const Char_t *file)
115 {
116  TFile *f = new TFile(file);
117  TString N = mSpec->GetName();
118  N+=";1";
119  TH2F *h = (TH2F*)f->Get(N.Data());
120  if(h){ mSpec->Reset(); mSpec->Add(h,1);}
121  f->Close();
122  delete f;
123  return ;
124 }
125 //_____________________________________________________________________________
126 void StEmcCalibMaker::addHist(const Char_t *file)
127 {
128  TFile *f = new TFile(file);
129  TString N = mSpec->GetName();
130  N+=";1";
131  TH2F *h = (TH2F*)f->Get(N.Data());
132  if(h){ mSpec->Add(h,1);}
133  f->Close();
134  delete f;
135  return ;
136 }
137 //_____________________________________________________________________________
138 float StEmcCalibMaker::getTimeInterval(int refDate, int refTime)
139 {
140  struct tm t0;
141  struct tm t1;
142 
143  Int_t year = (Int_t)(mDate/10000);
144  t0.tm_year=year-1900;
145  Int_t month = (Int_t)(mDate-year*10000)/100;
146  t0.tm_mon=month-1;
147  Int_t day = (Int_t)(mDate-year*10000-month*100);
148  t0.tm_mday=day;
149  Int_t hour = (Int_t)(mTime/10000);
150  t0.tm_hour=hour;
151  Int_t minute= (Int_t)(mTime-hour*10000)/100;
152  t0.tm_min=minute;
153  Int_t second= (Int_t)(mTime-hour*10000-minute*100);
154  t0.tm_sec=second;
155  t0.tm_isdst = -1;
156 
157  year = (Int_t)(refDate/10000);
158  t1.tm_year=year-1900;
159  month = (Int_t)(refDate-year*10000)/100;
160  t1.tm_mon=month-1;
161  day = (Int_t)(refDate-year*10000-month*100);
162  t1.tm_mday=day;
163  hour = (Int_t)(refTime/10000);
164  t1.tm_hour=hour;
165  minute= (Int_t)(refTime-hour*10000)/100;
166  t1.tm_min=minute;
167  second= (Int_t)(refTime-hour*10000-minute*100);
168  t1.tm_sec=second;
169  t1.tm_isdst = -1;
170 
171  time_t ttime0=mktime(&t0);
172  time_t ttime1=mktime(&t1);
173  double diff=difftime(ttime0,ttime1);
174 
175  //cout <<"startdate = "<<startDate<<" now = "<<d<<" dt = "<<diff/60<<endl;
176  //cout <<diff<<" "<<diff/60.<<endl;
177  float ddd=fabs(diff/3600.);
178  return ddd;
179 }
180 //_____________________________________________________________________________
181 void StEmcCalibMaker::getMeanAndRms(TH1D* tmp,float amin,float amax,
182  float* m,float* r)
183 {
184  float mean=0,rms=0,sum=0;
185  if(tmp->Integral()>0)
186  {
187  Int_t bin0 = tmp->FindBin(amin);
188  Int_t bin1 = tmp->FindBin(amax);
189  for(Int_t j=bin0;j<bin1;j++)
190  {
191  float w = tmp->GetBinContent(j);
192  float x = tmp->GetBinCenter(j);
193  mean += w*x;
194  sum += w;
195  rms += x*x*w;
196  }
197  if(sum>0)
198  {
199  mean /= sum;
200  rms = ::sqrt(rms/sum-mean*mean);
201  }
202  }
203  *m = mean;
204  *r = rms;
205  return;
206 
207 }
208 //_____________________________________________________________________________
209 void StEmcCalibMaker::getLogMeanAndRms(TH1D* tmp,float amin,float amax,
210  float* m,float* r)
211 {
212  float mean=0,rms=0,sum=0;
213 
214  if(tmp->Integral()>0)
215  {
216  Int_t bin0 = tmp->FindBin(amin);
217  Int_t bin1 = tmp->FindBin(amax);
218  for(Int_t j=bin0;j<bin1;j++)
219  {
220  float w = tmp->GetBinContent(j);
221  if(w==1) w=1.5;
222  if(w>0) w = ::log(w);
223  float x = tmp->GetBinCenter(j);
224  mean += w*x;
225  sum += w;
226  rms += x*x*w;
227  }
228  if(sum>0)
229  {
230  mean /= sum;
231  rms = ::sqrt(rms/sum-mean*mean);
232  }
233  }
234  *m = mean;
235  *r = rms;
236  return;
237 
238 }
239 StEmcGeom* StEmcCalibMaker::getGeom()
240 {
241  return StEmcGeom::instance(getDetector());
242 }
243 //_____________________________________________________________________________
244 void StEmcCalibMaker::calcVoltages(TH1F *gain, char* refFile, char* voltageInput, char* voltageOutput)
245 {
246  char GainShift[]="HVGainShift.dat";
247  char line[300];
248 
249  ofstream out(GainShift);
250  ifstream inp(voltageInput);
251  //TH1F *h = new TH1F("Gain","Current Gain Distribution",100,0,0.02);
252  do
253  {
254  int a,b,c,d,e,f,id;
255  float v;
256  inp>>a>>id>>b>>c>>d>>e>>f>>v;
257  float gainShift = gain->GetBinContent(id);
258  sprintf(line,"%2d %5d %5d %5d %5d %5d %5d %7.5f",a,id,b,c,d,e,f,gainShift);
259  out <<line<<endl;
260  cout <<line<<endl;
261 
262  } while(!inp.eof());
263  out.close();
264 
265  cout <<"Calculating voltages\n";
266 
267  // Instantiate the voltage calibrator class
268  VoltCalibrator vc;
269  // Select the file that contains the Gain vs HV data
270  vc.setRefFile(refFile);
271  // Select the file that contains the relative gains to be
272  // be achieved
273  vc.setGainFile(GainShift);
274  // Select the file that containe the current voltages
275  vc.setVoltInputFile(voltageInput);
276  // Select the file that will contain the new request voltages
277  vc.setVoltOutputFile(voltageOutput);
278  // Perform the calculation
279  vc.process();
280 }
virtual void Clear(Option_t *option="")
User defined functions.
virtual Int_t Make()
Definition: Stypes.h:40
virtual Int_t Finish()
virtual Int_t Finish()
Definition: StMaker.cxx:776