00001 #include "MdcCalibAlg/IniMdcCalib.h"
00002
00003 #include "GaudiKernel/MsgStream.h"
00004 #include "GaudiKernel/IMessageSvc.h"
00005 #include "GaudiKernel/StatusCode.h"
00006 #include "GaudiKernel/ISvcLocator.h"
00007 #include "GaudiKernel/Bootstrap.h"
00008
00009 #include "GaudiKernel/SmartDataPtr.h"
00010 #include "GaudiKernel/IDataProviderSvc.h"
00011 #include "GaudiKernel/PropertyMgr.h"
00012
00013 #include "EventModel/EventHeader.h"
00014 #include "EvTimeEvent/RecEsTime.h"
00015 #include "EventModel/Event.h"
00016 #include "MdcRawEvent/MdcDigi.h"
00017 #include "Identifier/Identifier.h"
00018 #include "Identifier/MdcID.h"
00019 #include "TStyle.h"
00020
00021 #include <iostream>
00022 #include <fstream>
00023 #include <iomanip>
00024 #include <string>
00025 #include <cstring>
00026 #include <cmath>
00027
00028 #include "TF1.h"
00029
00030 using namespace Event;
00031
00032 IniMdcCalib::IniMdcCalib(){
00033 }
00034
00035 IniMdcCalib::~IniMdcCalib(){
00036 }
00037
00038 void IniMdcCalib::clear(){
00039 int iEs;
00040 for(int lay=0; lay<MdcCalNLayer; lay++){
00041 delete m_hlaymapT[lay];
00042 delete m_htdc[lay];
00043 delete m_htraw[lay];
00044 for(iEs=0; iEs<m_param.nEsFlag; iEs++){
00045 delete m_htdcTes[lay][iEs];
00046 delete m_htrawTes[lay][iEs];
00047 }
00048
00049 delete m_hlaymapQ[lay];
00050 delete m_hqraw[lay];
00051 }
00052
00053 for(int wir=0; wir<MdcCalTotCell; wir++){
00054 delete m_htrawCel[wir];
00055 delete m_hqrawCel[wir];
00056 }
00057
00058 delete m_hLayerHitmapT;
00059 delete m_hWireHitMapT;
00060
00061 delete m_hLayerHitmapQ;
00062 delete m_hWireHitMapQ;
00063 for(iEs=0; iEs<m_param.nEsFlag; iEs++) delete m_hTes[iEs];
00064 delete m_hTesAllFlag;
00065 delete m_hTesAll;
00066 delete m_hTesCal;
00067 delete m_hTesFlag;
00068
00069 delete m_fdcom;
00070 delete m_fdTmap;
00071 delete m_fdTraw;
00072 delete m_fdTrawCel;
00073 delete m_fdTrawTes;
00074 delete m_fdQmap;
00075 delete m_fdQraw;
00076 delete m_fdQrawCel;
00077 }
00078
00079 void IniMdcCalib::initialize(TObjArray* hlist, IMdcGeomSvc* mdcGeomSvc,
00080 IMdcCalibFunSvc* mdcFunSvc, IMdcUtilitySvc* mdcUtilitySvc) {
00081 IMessageSvc* msgSvc;
00082 Gaudi::svcLocator() -> service("MessageSvc", msgSvc);
00083 MsgStream log(msgSvc, "IniMdcCalib");
00084 log << MSG::INFO << "IniMdcCalib::initialize()" << endreq;
00085
00086 m_hlist = hlist;
00087 m_mdcGeomSvc = mdcGeomSvc;
00088 m_mdcFunSvc = mdcFunSvc;
00089 m_mdcUtilitySvc = mdcUtilitySvc;
00090
00091 int lay;
00092 int cel;
00093 int wir;
00094 int ncel;
00095 char hname[200];
00096
00097 m_nWire = m_mdcGeomSvc -> getWireSize();
00098 m_nLayer = m_mdcGeomSvc -> getLayerSize();
00099
00100 m_fdcom = new TFolder("Common", "Common");
00101 m_hlist->Add(m_fdcom);
00102
00103 m_fdTmap = new TFolder("Thitmap", "Thitmap");
00104 m_hlist->Add(m_fdTmap);
00105
00106 m_fdTraw = new TFolder("Traw", "Traw");
00107 m_hlist->Add(m_fdTraw);
00108
00109 m_fdTrawCel = new TFolder("TrawCell", "TrawCell");
00110 m_hlist->Add(m_fdTrawCel);
00111
00112 m_fdTrawTes = new TFolder("TrawTes", "TrawTes");
00113 m_hlist->Add(m_fdTrawTes);
00114
00115 m_fdQmap = new TFolder("Qhitmap", "Qhitmap");
00116 m_hlist->Add(m_fdQmap);
00117
00118 m_fdQraw = new TFolder("Qraw", "Qraw");
00119 m_hlist->Add(m_fdQraw);
00120
00121 m_fdQrawCel = new TFolder("QrawCell", "QrawCell");
00122 m_hlist->Add(m_fdQrawCel);
00123
00124 m_hLayerHitmapT = new TH1F("T_Hitmap_Layer", "", 43, -0.5, 42.5);
00125 m_fdcom->Add(m_hLayerHitmapT);
00126
00127 m_hWireHitMapT = new TH1F("T_Hitmap_Wire", "", 6796, -0.5, 6795.5);
00128 m_fdcom->Add(m_hWireHitMapT);
00129
00130 m_hLayerHitmapQ = new TH1F("Q_Hitmap_Layer", "", 43, -0.5, 42.5);
00131 m_fdcom->Add(m_hLayerHitmapQ);
00132
00133 m_hWireHitMapQ = new TH1F("Q_Hitmap_Wire", "", 6796, -0.5, 6795.5);
00134 m_fdcom->Add(m_hWireHitMapQ);
00135
00136 int iEs;
00137 for(iEs=0; iEs<m_param.nEsFlag; iEs++){
00138 sprintf(hname, "Tes_%d", m_param.esFlag[iEs]);
00139 m_hTes[iEs] = new TH1F(hname, "", 750, 0, 1500);
00140 m_fdcom->Add(m_hTes[iEs]);
00141 }
00142
00143 m_hTesAllFlag = new TH1F("TesAllFlag", "", 300, -0.5, 299.5);
00144 m_fdcom -> Add(m_hTesAllFlag);
00145
00146 m_hTesAll = new TH1F("TesAll", "", 750, 0, 1500);
00147 m_fdcom->Add(m_hTesAll);
00148
00149 m_hTesCal = new TH1F("TesCal", "", 750, 0, 1500);
00150 m_fdcom->Add(m_hTesCal);
00151
00152 m_hTesFlag = new TH1F("Tes_Flag", "", 300, -0.5, 299.5);
00153 m_fdcom->Add(m_hTesFlag);
00154
00155 for(lay=0; lay<m_nLayer; lay++){
00156 ncel = m_mdcGeomSvc->Layer(lay)->NCell();
00157
00158 sprintf(hname, "T_hitmap_Lay%02d", lay);
00159 m_hlaymapT[lay] = new TH1F(hname, "", ncel, -0.5, (float)ncel-0.5);
00160 m_fdTmap -> Add(m_hlaymapT[lay]);
00161
00162 sprintf(hname, "TDC_Lay%02d", lay);
00163 m_htdc[lay] = new TH1F(hname, "", 800, 0, 20000);
00164 m_fdTraw -> Add(m_htdc[lay]);
00165
00166 sprintf(hname, "Traw_Lay%02d", lay);
00167 m_htraw[lay] = new TH1F(hname, "", 500, 0, 1000);
00168 m_fdTraw -> Add(m_htraw[lay]);
00169
00170 for(iEs=0; iEs<m_param.nEsFlag; iEs++){
00171 sprintf(hname, "TDC_Lay%02d_Tes%d", lay, m_param.esFlag[iEs]);
00172 m_htdcTes[lay][iEs] = new TH1F(hname, "", 800, 0, 20000);
00173 m_fdTrawTes -> Add(m_htdcTes[lay][iEs]);
00174
00175 sprintf(hname, "Traw_Lay%02d_Tes%d", lay, m_param.esFlag[iEs]);
00176 m_htrawTes[lay][iEs] = new TH1F(hname, "", 300, 0, 600);
00177 m_fdTrawTes -> Add(m_htrawTes[lay][iEs]);
00178 }
00179
00180 sprintf(hname, "Q_hitmap_Lay%02d", lay);
00181 m_hlaymapQ[lay] = new TH1F(hname, "", ncel, -0.5, (float)ncel-0.5);
00182 m_fdQmap -> Add(m_hlaymapQ[lay]);
00183
00184 sprintf(hname, "Qraw_Lay%02d", lay);
00185 m_hqraw[lay] = new TH1F(hname, "", 2000, 0, 4000);
00186 m_fdQraw -> Add(m_hqraw[lay]);
00187 }
00188
00189 for(wir=0; wir<MdcCalTotCell; wir++){
00190 lay = m_mdcGeomSvc -> Wire(wir) -> Layer();
00191 cel = m_mdcGeomSvc -> Wire(wir) -> Cell();
00192
00193 sprintf(hname, "Traw_%02d_%03d_%04d", lay, cel, wir);
00194 m_htrawCel[wir] = new TH1F(hname, "", 300, 0, 600);
00195 m_fdTrawCel -> Add(m_htrawCel[wir]);
00196
00197 sprintf(hname, "Qraw_%02d_%03d_%04d", lay, cel, wir);
00198 m_hqrawCel[wir] = new TH1F(hname, "", 2000, 0, 4000);
00199 m_fdQrawCel -> Add(m_hqrawCel[wir]);
00200 }
00201 }
00202
00203 int IniMdcCalib::fillHist(MdcCalEvent* event){
00204 IMessageSvc* msgSvc;
00205 Gaudi::svcLocator() -> service("MessageSvc", msgSvc);
00206 MsgStream log(msgSvc, "IniMdcCalib");
00207 log << MSG::DEBUG << "IniMdcCalib::fillHist()" << endreq;
00208
00209 int lay;
00210 int cel;
00211 int wir;
00212 int digiId;
00213 unsigned fgOverFlow;
00214 double tdc;
00215 double traw;
00216 double adc;
00217 double qraw;
00218 Identifier id;
00219
00220 IDataProviderSvc* eventSvc = NULL;
00221 Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
00222
00223 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc,"/Event/EventHeader");
00224 if (!eventHeader) {
00225 log << MSG::FATAL << "Could not find Event Header" << endreq;
00226 return( StatusCode::FAILURE);
00227 }
00228 int iRun = eventHeader->runNumber();
00229 int iEvt = eventHeader->eventNumber();
00230
00231
00232 double tes = -9999.0;
00233 int esTimeflag = -1;
00234 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
00235 if( (!aevtimeCol) || (aevtimeCol->size()==0) ){
00236 tes = -9999.0;
00237 esTimeflag = -1;
00238 }else{
00239 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
00240 for(; iter_evt!=aevtimeCol->end(); iter_evt++){
00241 tes = (*iter_evt)->getTest();
00242 esTimeflag = (*iter_evt)->getStat();
00243
00244 }
00245 }
00246 m_hTesAllFlag->Fill(esTimeflag);
00247 m_hTesAll->Fill(tes);
00248 m_hTesFlag->Fill(esTimeflag);
00249 int nTesFlag = -1;
00250 for(int iEs=0; iEs<m_param.nEsFlag; iEs++){
00251 if(esTimeflag == m_param.esFlag[iEs]){
00252 m_hTes[iEs]->Fill(tes);
00253 nTesFlag = iEs;
00254 break;
00255 }
00256 }
00257 bool fgFillTes = false;
00258 if( (nTesFlag > -1) && (tes > m_param.tesMin) && (tes < m_param.tesMax) ){
00259 m_hTesCal->Fill(tes);
00260 fgFillTes = true;
00261 }
00262
00263
00264
00265
00266 SmartDataPtr<MdcDigiCol> mdcDigiCol(eventSvc,"/Event/Digi/MdcDigiCol");
00267 if (!mdcDigiCol) {
00268 log << MSG::FATAL << "Could not find event" << endreq;
00269 }
00270
00271 MdcDigiCol::iterator iter = mdcDigiCol->begin();
00272 digiId = 0;
00273 for(; iter != mdcDigiCol->end(); iter++, digiId++) {
00274 MdcDigi *aDigi = (*iter);
00275 id = (aDigi)->identify();
00276
00277 lay = MdcID::layer(id);
00278 cel = MdcID::wire(id);
00279 wir = m_mdcGeomSvc->Wire(lay, cel)->Id();
00280
00281 tdc = (aDigi) -> getTimeChannel();
00282 adc = (aDigi) -> getChargeChannel();
00283 fgOverFlow = (aDigi) -> getOverflow();
00284
00285 if ( ((fgOverFlow & 1) !=0 ) || ((fgOverFlow & 12) != 0) ||
00286 (aDigi->getTimeChannel() == 0x7FFFFFFF) ||
00287 (aDigi->getChargeChannel() == 0x7FFFFFFF) ) continue;
00288
00289 traw = tdc * MdcCalTdcCnv;
00290 qraw = adc * MdcCalAdcCnv;
00291
00292 m_hLayerHitmapT -> Fill(lay);
00293 m_hWireHitMapT -> Fill(wir);
00294 m_hlaymapT[lay] -> Fill(cel);
00295
00296 m_hLayerHitmapQ -> Fill(lay);
00297 m_hWireHitMapQ -> Fill(wir);
00298 m_hlaymapQ[lay] -> Fill(cel);
00299
00300 if(fgFillTes){
00301 traw -= tes;
00302 traw += m_param.timeShift;
00303
00304 m_htdc[lay] -> Fill(tdc);
00305 m_htraw[lay] -> Fill(traw);
00306 m_hqraw[lay] -> Fill(qraw);
00307
00308 m_htdcTes[lay][nTesFlag] -> Fill(tdc);
00309 m_htrawTes[lay][nTesFlag] -> Fill(traw);
00310
00311 m_htrawCel[wir] -> Fill(traw);
00312 m_hqrawCel[wir] -> Fill(qraw);
00313 }
00314 }
00315 return 1;
00316 }
00317
00318 int IniMdcCalib::updateConst(MdcCalibConst* calconst){
00319 IMessageSvc* msgSvc;
00320 Gaudi::svcLocator() -> service("MessageSvc", msgSvc);
00321 MsgStream log(msgSvc, "IniMdcCalib");
00322 log << MSG::DEBUG << "IniMdcCalib::updateConst()" << endreq;
00323
00324 int lay;
00325 int wir;
00326 double t0Fit[MdcCalNLayer];
00327 double t0Cal[MdcCalNLayer];
00328 double tmax[MdcCalNLayer];
00329 double initT0 = m_param.initT0;
00330
00331 int fitTminFg[MdcCalNLayer];
00332 int fitTmaxFg[MdcCalNLayer];
00333 double chisq;
00334 double ndf;
00335 double chindfTmin[MdcCalNLayer];
00336 double chindfTmax[MdcCalNLayer];
00337 char funname[200];
00338
00339
00340 TF1* ftmin[MdcCalNLayer];
00341
00342
00343 for(lay=0; lay<m_nLayer; lay++){
00344 fitTminFg[lay] = 0;
00345 chindfTmin[lay] = -1;
00346 sprintf(funname, "ftmin%02d", lay);
00347 ftmin[lay] = new TF1(funname, funTmin, 0, 150, 6);
00348
00349 if(1 == m_param.fgCalib[lay]){
00350 Stat_t nEntryTot = 0;
00351 for(int ibin=1; ibin<=25; ibin++){
00352 Stat_t entry = m_htraw[lay]->GetBinContent(ibin);
00353 nEntryTot += entry;
00354 }
00355 double c0Ini = (double)nEntryTot / 25.0;
00356 double c1Ini = (m_htraw[lay]->GetMaximum()) - c0Ini;
00357
00358 ftmin[lay] -> SetParameter(0, c0Ini);
00359 ftmin[lay] -> SetParameter(1, c1Ini);
00360 ftmin[lay] -> SetParameter(2, 0);
00361 ftmin[lay] -> SetParameter(4, initT0);
00362 ftmin[lay] -> SetParameter(5, 2);
00363
00364 m_htraw[lay] -> Fit(funname, "Q", "",
00365 m_param.tminFitRange[lay][0],
00366 m_param.tminFitRange[lay][1]);
00367 chisq = ftmin[lay]->GetChisquare();
00368 gStyle -> SetOptFit(11);
00369 ndf = ftmin[lay]->GetNDF();
00370 chindfTmin[lay] = chisq / ndf;
00371 if(chindfTmin[lay] < m_param.tminFitChindf){
00372 fitTminFg[lay] = 1;
00373 t0Fit[lay] = ftmin[lay]->GetParameter(4);
00374 t0Fit[lay] += m_param.t0Shift;
00375 t0Cal[lay] = t0Fit[lay] - m_param.timeShift;
00376 }
00377 }
00378
00379 if(0 == fitTminFg[lay]){
00380 wir = m_mdcGeomSvc->Wire(lay, 0)->Id();
00381 t0Cal[lay] = calconst->getT0(wir);
00382 t0Fit[lay] = t0Cal[lay] + m_param.timeShift;
00383 }
00384 }
00385
00386
00387 TF1* ftmax[MdcCalNLayer];
00388 for(lay=0; lay<m_nLayer; lay++){
00389 fitTmaxFg[lay] = 0;
00390 chindfTmax[lay] = -1;
00391 sprintf(funname, "ftmax%02d", lay);
00392 ftmax[lay] = new TF1(funname, funTmax, 250, 500, 4);
00393
00394 if(1 == m_param.fgCalib[lay]){
00395 ftmax[lay] -> SetParameter(2, m_param.initTm[lay]);
00396 ftmax[lay] -> SetParameter(3, 10);
00397
00398 m_htraw[lay] -> Fit(funname, "Q+", "",
00399 m_param.tmaxFitRange[lay][0],
00400 m_param.tmaxFitRange[lay][1]);
00401 gStyle -> SetOptFit(11);
00402 chisq = ftmax[lay]->GetChisquare();
00403 ndf = ftmax[lay]->GetNDF();
00404 chindfTmax[lay] = chisq / ndf;
00405 if(chindfTmax[lay] < m_param.tmaxFitChindf){
00406 fitTmaxFg[lay] = 1;
00407 tmax[lay] = ftmax[lay]->GetParameter(2);
00408 }
00409 }
00410
00411 if(0 == fitTmaxFg[lay]){
00412 tmax[lay] = (calconst->getXtpar(lay, 0, 0, 6)) + t0Fit[lay];
00413 }
00414 }
00415
00416
00417 ofstream ft0("iniT0.dat");
00418 for(lay=0; lay<m_nLayer; lay++){
00419 ft0 << setw(5) << lay << setw(3) << fitTminFg[lay]
00420 << setw(12) << t0Cal[lay] << setw(12) << t0Fit[lay]
00421 << setw(12) << chindfTmin[lay] << setw(5) << fitTmaxFg[lay]
00422 << setw(12) << tmax[lay] << setw(12) << tmax[lay] - t0Fit[lay]
00423 << setw(12) << chindfTmax[lay] << endl;
00424 }
00425 ft0.close();
00426 cout << "iniT0.dat was written." << endl;
00427
00428
00429 int i;
00430 int nwire = m_mdcGeomSvc -> getWireSize();
00431 for(i=0; i<nwire; i++){
00432 lay = m_mdcGeomSvc -> Wire(i) -> Layer();
00433 if(1 == m_param.fgCalib[lay]){
00434 calconst -> resetT0(i, t0Cal[lay]);
00435 calconst -> resetDelT0(i, 0.0);
00436 }
00437 }
00438
00439 if(0 == m_param.fgIniCalConst){
00440
00441 int lr;
00442 int iEntr;
00443 int ord;
00444 double xtpar;
00445 double xtini[8] = {0, 0.03, 0, 0, 0, 0, 999.9, 0};
00446 for(lay=0; lay<m_nLayer; lay++){
00447 if(1 != m_param.fgCalib[lay]) continue;
00448
00449 for(iEntr=0; iEntr<MdcCalNENTRXT; iEntr++){
00450 for(lr=0; lr<MdcCalLR; lr++){
00451 for(ord=0; ord<MdcCalXtNPars; ord++){
00452 if(6 == ord){
00453 xtpar = tmax[lay] - t0Fit[lay];
00454 } else{
00455 xtpar = xtini[ord];
00456 }
00457 calconst -> resetXtpar(lay, iEntr, lr, ord, xtpar);
00458 }
00459 }
00460 }
00461 }
00462
00463
00464 for(lay=0; lay<m_nLayer; lay++){
00465 calconst -> resetQtpar0(lay, 0.0);
00466 calconst -> resetQtpar1(lay, 0.0);
00467 }
00468
00469
00470 int bin;
00471 double sdpar = m_param.initSigma;
00472 for(lay=0; lay<m_nLayer; lay++){
00473 for(iEntr=0; iEntr<MdcCalNENTRSD; iEntr++){
00474 for(lr=0; lr<2; lr++){
00475 for(bin=0; bin<MdcCalSdNBIN; bin++){
00476 calconst -> resetSdpar(lay, iEntr, lr, bin, sdpar);
00477 }
00478 }
00479 }
00480 }
00481 } else if(2 == m_param.fgIniCalConst){
00482 int lr;
00483 int iEntr;
00484 double xtpar;
00485 for(lay=0; lay<m_nLayer; lay++){
00486 for(iEntr=0; iEntr<MdcCalNENTRXT; iEntr++){
00487 for(lr=0; lr<MdcCalLR; lr++){
00488 xtpar = tmax[lay] - t0Fit[lay];
00489 calconst -> resetXtpar(lay, iEntr, lr, 6, xtpar);
00490 }
00491 }
00492 }
00493 }
00494
00495
00496 for(lay=0; lay<MdcCalNLayer; lay++){
00497 delete ftmin[lay];
00498 delete ftmax[lay];
00499 }
00500 return 1;
00501 }
00502
00503 Double_t IniMdcCalib::funTmin(Double_t* x, Double_t* par){
00504 Double_t fitval;
00505 fitval = par[0] + par[1]*exp( -par[2]*(x[0]-par[3]) ) /
00506 ( 1 + exp( -(x[0]-par[4])/par[5] ));
00507 return fitval;
00508 }
00509
00510 Double_t IniMdcCalib::funTmax(Double_t* x, Double_t* par){
00511 Double_t fitval;
00512 fitval = par[0] + par[1] / (1 + exp((x[0]-par[2])/par[3]));
00513 return fitval;
00514 }
00515