00001 #include "SimHitSplitSvc/SimHitSplitSvc.h"
00002
00003 #include <map>
00004 #include <iterator>
00005 #include <algorithm>
00006
00007 using namespace DayaBay;
00008
00009 SimHitSplitSvc::SimHitSplitSvc(const string& name, ISvcLocator* pSvcLocator)
00010 :Service(name, pSvcLocator), m_log(msgSvc(), "SimHitSplitSvc")
00011 {
00012
00013
00014 m_minTimeGap = preTimeTolerance/CLHEP::nanosecond + postTimeTolerance/CLHEP::nanosecond;
00015 TimeStamp timeGap(m_minTimeGap/1.0e9);
00016 m_MinTimeGap = timeGap;
00017
00018
00019
00020
00021
00022 }
00023
00024 SimHitSplitSvc::~SimHitSplitSvc()
00025 {
00026 m_log<< MSG::INFO<< "SimHitSplitSvc::~SimHitSplitSvc()..."<< endreq;
00027 }
00028
00029 StatusCode SimHitSplitSvc::initialize()
00030 {
00031
00032 m_log<< MSG::INFO<< "SimHitSplitSvc ininalize @"<< (void*)this<< endreq;
00033 StatusCode sc = Service::initialize();
00034 if(sc.isFailure())
00035 {
00036 m_log<< MSG::ERROR<< "Parent class(Service) failed to initialize!"<< endreq;
00037 return sc;
00038 }
00039
00040
00041 m_log<< MSG::INFO<< "MinTimeGap: "<< m_minTimeGap<< " ns"<< endreq;
00042 m_log<< MSG::INFO<< "MinTimeGap: "<< m_MinTimeGap.GetSec()<< "s --"
00043 << m_MinTimeGap.GetNanoSec()<< " ns"<< endreq;
00044
00045 return StatusCode::SUCCESS;
00046 }
00047
00048 StatusCode SimHitSplitSvc::queryInterface(const InterfaceID& riid, void** ppint)
00049 {
00050 if(IID_ISimHitSplitSvc.versionMatch(riid))
00051 {
00052 m_log<< MSG::DEBUG<< "queryInterface("<< riid
00053 << ")-->(ISimHitSplitSvc*)"<< (void*)this<< endreq;
00054 *ppint = (ISimHitSplitSvc*)this;
00055 }
00056 else
00057 {
00058 return Service::queryInterface(riid, ppint);
00059 }
00060 addRef();
00061 return StatusCode::SUCCESS;
00062 }
00063
00064
00065 StatusCode SimHitSplitSvc::getSimHitHeader(SimHeader* sh, vector<SimHitHeader*>& shhs, vector<TimeStamp>& hites, vector<TimeStamp>& hitls)
00066 {
00067 m_log<< MSG::DEBUG<< "Now in the getSimHitHeaders()."<< endreq;
00068
00069
00070 TimeStamp TimeRange = sh->latest() - sh->earliest();
00071 m_log<< MSG::DEBUG<< "The SimHeader's range: "
00072 << TimeRange.GetSec()<< "--"<< TimeRange.GetNanoSec()<< endreq;
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 StatusCode sc = this->fillHitsBuffer(sh);
00084 if(sc.isFailure())
00085 {
00086 m_log<< MSG::ERROR<< "Error when filling the SimHits!"<< endreq;
00087 return sc;
00088 }
00089
00090
00091 sc = this->createHitHeaders(sh, shhs, hites, hitls);
00092 if(sc.isFailure())
00093 {
00094 m_log<< MSG::ERROR<< "Error when creating the new SimHitHeaders!"<< endreq;
00095 return sc;
00096 }
00097
00098 return StatusCode::SUCCESS;
00099 }
00100
00101
00102 StatusCode SimHitSplitSvc::fillHitsBuffer(SimHeader* sh)
00103 {
00104 m_log<< MSG::DEBUG<< "Fill SimHits buffer"<< endreq;
00105
00106
00107 const SimHitHeader* shh = sh->hits();
00108 map<short int, SimHitCollection*> mshc = shh->hitCollection();
00109 if(0 == mshc.size())
00110 {
00111 m_log<< MSG::DEBUG<< "Empty SimHitHeader!"<< endreq;
00112 return StatusCode::SUCCESS;
00113 }
00114
00115 m_absTime = sh->timeStamp();
00116 m_log<< MSG::DEBUG<< "The SimHeader TimeStamp: "<< m_absTime.GetSec()
00117 << "--"<< m_absTime.GetNanoSec()<< endreq;
00118 m_log<< MSG::DEBUG<< "The SimHeader Earliest: "<< sh->earliest().GetSec()
00119 << "--"<< sh->earliest().GetNanoSec()<< endreq;
00120 m_log<< MSG::DEBUG<< "The SimHeader Latest: "<< sh->latest().GetSec()
00121 << "--"<< sh->latest().GetNanoSec()<< endreq;
00122
00123
00124
00125
00126 map<short int, SimHitCollection*>::iterator mit;
00127 for(mit = mshc.begin(); mit != mshc.end(); mit++)
00128 {
00129
00130 m_log<< MSG::DEBUG<< "In Det: "<< mit->first<< ", there are "<< mit->second->collection().size()<< " SimHits."<< endreq;
00131
00132 SimHitCollection* shc = mit->second;
00133 vector<SimHit*> hits = shc->collection();
00134
00135
00136
00137
00138
00139
00140
00141 for(vector<SimHit*>::iterator vit = hits.begin(); vit != hits.end(); vit++)
00142 {
00143 MixSimHit mixhit;
00144 mixhit.setHit(*vit);
00145 m_log<< MSG::DEBUG<< "SimHit Time: "<< mixhit.sht->hitTime()<< endreq;
00146 m_SimHits.push_back(mixhit);
00147 }
00148 }
00149
00150 sort(m_SimHits.begin(), m_SimHits.end());
00151
00152 deque<MixSimHit>::size_type mixsize = m_SimHits.size();
00153 mixsize--;
00154 TimeStamp ET(m_SimHits[0].sht->hitTime()/1.0e9);
00155 TimeStamp LT(m_SimHits[mixsize].sht->hitTime()/1.0e9);
00156 ET.Add(m_absTime);
00157 LT.Add(m_absTime);
00158
00159 m_log<< MSG::DEBUG<< "The earliest SimHit time: "<< ET.GetSec()
00160 << "--"<< ET.GetNanoSec()<< " from "<< m_SimHits[0].sht->hc()->detector().detName()<< endreq;
00161 m_log<< MSG::DEBUG<< "The latest SimHit time: "<< LT.GetSec()
00162 << "--"<< LT.GetNanoSec()<< " from "<< m_SimHits[mixsize].sht->hc()->detector().detName()<< endreq;
00163
00164 m_log<< MSG::DEBUG<< "After sorting....."<< endreq;
00165 for(deque<MixSimHit>::iterator vit = m_SimHits.begin(); vit != m_SimHits.end(); vit++)
00166 {
00167
00168 m_log<< MSG::DEBUG<< "SimHit time in m_SimHits: "<< vit->sht->hitTime()<< endreq;
00169 }
00170
00171
00172 StatusCode sc = this->findGap();
00173 if(sc.isFailure())
00174 {
00175 m_log<< MSG::ERROR<< "Error in findGap()!"<< endreq;
00176 return StatusCode::FAILURE;
00177 }
00178
00179 return StatusCode::SUCCESS;
00180 }
00181
00182 StatusCode SimHitSplitSvc::findGap()
00183 {
00184 m_log<< MSG::DEBUG<< "Find gap here(update the Gap Vector)..."<< endreq;
00185
00186
00187 m_hitsGap.clear();
00188
00189
00190 if(m_SimHits.size() < 2)
00191 {
00192 m_log<< MSG::DEBUG<< "No enough SimHits in m_SimHits! Be carefull!"<< endreq;
00193
00194 m_hitsGap.push_back(m_SimHits[0].sht->hitTime());
00195
00196 for(vector<double>::size_type ind = 0; ind != m_hitsGap.size(); ind++)
00197 {
00198 m_log<< MSG::DEBUG<< "Time Gap: "<< m_hitsGap[ind]<< endreq;
00199 }
00200
00201 return StatusCode::SUCCESS;
00202 }
00203
00204
00205
00206
00207
00208 deque<MixSimHit>::iterator dit, ddit, dend;
00209 dend = m_SimHits.end();
00210 dend--;
00211 for(dit = m_SimHits.begin(); dit != dend; dit++)
00212 {
00213 ddit = dit + 1;
00214 double timeinter = ddit->sht->hitTime() - dit->sht->hitTime();
00215
00216
00217 if(timeinter > m_minTimeGap)
00218 {
00219 m_log<< MSG::DEBUG<< "There is one gap!"<< endreq;
00220
00221 m_hitsGap.push_back(dit->sht->hitTime());
00222 }
00223 }
00224
00225
00226 m_log<< MSG::DEBUG<< "There is the last gap!"<< endreq;
00227 m_hitsGap.push_back(dend->sht->hitTime());
00228
00229 for(vector<double>::size_type ind = 0; ind != m_hitsGap.size(); ind++)
00230 {
00231 m_log<< MSG::DEBUG<< "Time Gap: "<< m_hitsGap[ind]<< endreq;
00232 }
00233
00234 return StatusCode::SUCCESS;
00235 }
00236
00237 StatusCode SimHitSplitSvc::createHitHeaders(SimHeader* sh, vector<SimHitHeader*>& shhs, vector<TimeStamp>& hites, vector<TimeStamp>& hitls)
00238 {
00239
00240 m_log<< MSG::DEBUG<< "Create new SimHitHeaders."<< endreq;
00241
00242
00243 if(0 == m_hitsGap.size())
00244 {
00245 shhs.push_back(const_cast<SimHitHeader*>(sh->hits()));
00246 hites.push_back(sh->earliest());
00247 hitls.push_back(sh->latest());
00248 return StatusCode::SUCCESS;
00249 }
00250
00251 deque<double>::size_type iind;
00252 for(iind = 0; iind <= m_hitsGap.size(); iind++)
00253 {
00254
00255 m_log<< MSG::DEBUG<< "Create one SimHitHeader before SimHit("<< m_hitsGap[0]<< ")."<< endreq;
00256
00257
00258 TimeStamp HitE(m_SimHits[0].sht->hitTime()/1.0e9);
00259 HitE.Add(m_absTime);
00260 TimeStamp HitL(m_hitsGap[0]/1.0e9);
00261 HitL.Add(m_absTime);
00262 m_hitEarliests.push_back(HitE);
00263 m_hitLatests.push_back(HitL);
00264
00265 SimHitHeader* shh = new SimHitHeader();
00266
00267 if(0 == shh)
00268 {
00269 m_log<< MSG::ERROR<< "Can not create a new SimHitHeader!"<< endreq;
00270 return StatusCode::FAILURE;
00271 }
00272 m_log<< MSG::DEBUG<< "Create a new SimHitHeader: "<< shh<< endreq;
00273
00274
00275 map<short int, SimHitCollection*>& mshc = shh->hitCollection();
00276
00277
00278 for( ; ; )
00279 {
00280 m_log<< MSG::DEBUG<< "The SimHit buffer size is: "<< m_SimHits.size()<< endreq;
00281
00282 deque<MixSimHit>::iterator dit = m_SimHits.begin();
00283 if(dit == m_SimHits.end())
00284 {
00285 m_log<< MSG::DEBUG<< "The SimHits Buffer is empty!"<< endreq;
00286 break;
00287 }
00288
00289 if(dit->sht->hitTime() <= m_hitsGap[0])
00290 {
00291 Detector Det = dit->sht->hc()->detector();
00292 short int det = Det.siteDetPackedData();
00293
00294 m_log<< MSG::DEBUG<< "This hit is from detector: "<< AsString(Det.detectorId())<< endreq;
00295
00296
00297 if(0 == mshc.size() || mshc.find(det) == mshc.end())
00298 {
00299 m_log<< MSG::DEBUG<< "map<short int, SimHitCollection*> size: "
00300 << mshc.size()<< " new detectorId: "<< AsString(Det.detectorId())<< endreq;
00301
00302
00303 SimHitCollection* shc = new SimHitCollection();
00304 shc->setHeader(shh);
00305 shc->setDetector(Det);
00306
00307 vector<SimHit*>& shts = shc->collection();
00308 shts.push_back(dit->sht);
00309 mshc[det] = shc;
00310
00311 m_log<< MSG::DEBUG<< "Now the map size is: "<< mshc.size()<< endreq;
00312
00313 m_SimHits.pop_front();
00314 continue;
00315 }
00316 else
00317 {
00318 m_log<< MSG::DEBUG<< "map<short int, SimHitCollection*> size: "
00319 << mshc.size()<< " detectorId: "<< AsString(Det.detectorId())<< endreq;
00320
00321 vector<SimHit*>& shts = mshc[det]->collection();
00322 shts.push_back(dit->sht);
00323
00324 m_SimHits.pop_front();
00325 continue;
00326 }
00327 }
00328 else
00329 {
00330 m_log<< MSG::DEBUG<< "Now create the next SimHitHeader!"<< endreq;
00331 break;
00332 }
00333 }
00334
00335 m_log<< MSG::DEBUG<< "Get one SimHitHeader: "<< shh<< " with "<< shh->hitCollection().size()<< endreq;
00336
00337 m_log<< MSG::DEBUG<< "Now prepare the SimHitHeader: "<< endreq;
00338 shhs.push_back(shh);
00339 m_hitsGap.pop_front();
00340 }
00341
00342 vector<SimHitHeader*>::iterator vsit = shhs.begin();
00343 vector<TimeStamp>::iterator veit = m_hitEarliests.begin();
00344 vector<TimeStamp>::iterator vlit = m_hitLatests.begin();
00345 for( ; vsit != shhs.end(); vsit++)
00346 {
00347 m_log<< MSG::DEBUG<< "Created SimHitHeader with : "<< shhs.size()<< " SimHitHeaders!"<< endreq;
00348 m_log<< MSG::DEBUG<< "Earliest: "<< veit->GetSec()
00349 << "--"<< veit->GetNanoSec()<< endreq;
00350 m_log<< MSG::DEBUG<< "Latest: "<< vlit->GetSec()
00351 << "--"<< vlit->GetNanoSec()<< endreq;
00352 }
00353
00354 hites = m_hitEarliests;
00355 hitls = m_hitLatests;
00356
00357 m_hitEarliests.clear();
00358 m_hitLatests.clear();
00359 m_SimHits.clear();
00360
00361 return StatusCode::SUCCESS;
00362 }