00001 #include "PreElecSimSvc/PreElecSimSvc.h"
00002
00003 #include <map>
00004 #include <iterator>
00005 #include <algorithm>
00006
00007 using namespace DayaBay;
00008
00009 PreElecSimSvc::PreElecSimSvc(const string& name, ISvcLocator* pSvcLocator)
00010 :Service(name, pSvcLocator), m_log(msgSvc(), "PreElecSimSvc")
00011 {
00012
00013 m_mixInputSvc = 0;
00014
00015
00016
00017 double mintimeGap = preTimeTolerance/CLHEP::nanosecond + postTimeTolerance/CLHEP::nanosecond;
00018
00019
00020
00021
00022
00023
00024 TimeStamp initime(mintimeGap/1.0e9);
00025 m_minTimeGap = initime;
00026 }
00027
00028 PreElecSimSvc::~PreElecSimSvc()
00029 {
00030 m_log<< MSG::INFO<< "PreElecSimSvc::~PreElecSimSvc()..."<< endreq;
00031 }
00032
00033 StatusCode PreElecSimSvc::initialize()
00034 {
00035
00036 m_log<< MSG::INFO<< "PreElecSimSvc ininalize @"<< (void*)this<< endreq;
00037 StatusCode sc = Service::initialize();
00038 if(sc.isFailure())
00039 {
00040 m_log<< MSG::ERROR<< "Parent class(Service) failed to initialize!"<< endreq;
00041 return sc;
00042 }
00043
00044
00045 m_log<< MSG::INFO<< "MinTimeGap: "<< m_minTimeGap.GetSec()<< "s--"
00046 << m_minTimeGap.GetNanoSec()<< "ns"<< endreq;
00047
00048
00049 IService* isvc = 0;
00050 sc = serviceLocator()->service("RootIOCnvSvc", isvc, true);
00051 if(sc.isFailure())
00052 {
00053 m_log<< MSG::ERROR<< "Conversion service RootIOCnvSvc could not be retrieved"<< endreq;
00054 return sc;
00055 }
00056 isvc->addRef();
00057 sc = isvc->queryInterface(IMixInputSvc::interfaceID(), (void**)&m_mixInputSvc);
00058 if(sc.isFailure())
00059 {
00060 m_log<< MSG::ERROR<< "Service MixInputSvc does not implement IMixInputSvc"<< endreq;
00061 return sc;
00062 }
00063
00064 return StatusCode::SUCCESS;
00065 }
00066
00067 StatusCode PreElecSimSvc::queryInterface(const InterfaceID& riid, void** ppint)
00068 {
00069 if(IID_IPreElecSimSvc.versionMatch(riid))
00070 {
00071 m_log<< MSG::DEBUG<< "queryInterface("<< riid
00072 << ")-->(IPreElecSimSvc*)"<< (void*)this<< endreq;
00073 *ppint = (IPreElecSimSvc*)this;
00074 }
00075 else
00076 {
00077 return Service::queryInterface(riid, ppint);
00078 }
00079 addRef();
00080 return StatusCode::SUCCESS;
00081 }
00082
00083
00084 SimHitHeader* PreElecSimSvc::getSimHitHeader(vector<const SimHeader*>& shs, TimeStamp& hite, TimeStamp& hitl)
00085 {
00086 m_log<< MSG::INFO<< "Now in the getSimHeaders()...."<< endreq;
00087
00088 TimeStamp initime(0.0);
00089 hite = initime;
00090 hitl = initime;
00091
00092 for( ; ; )
00093 {
00094 m_log<< MSG::DEBUG<< "SimHitHeaders' buffer size = "<< m_propSimHitHeaders.size()<< endreq;
00095
00096
00097 if(0 != m_propSimHitHeaders.size())
00098 {
00099 m_log<< MSG::DEBUG<< "So, get one SimHitHeader and its SimHeaders directly!"<< endreq;
00100
00101 SimHitHeader* shh = m_propSimHitHeaders[0];
00102 if(!shh)
00103 {
00104 m_log<< MSG::ERROR<< "Got one wrong SimHitHeader!"<< endreq;
00105 return shh;
00106 }
00107
00108 hite = m_hitEarliests[0];
00109 hitl = m_hitLatests[0];
00110
00111 shs = m_HitHeaderToHeaders[shh];
00112
00113
00114 m_log<< MSG::DEBUG<< "Before clearing: SimHitHeader buffer size: "<< m_propSimHitHeaders.size()
00115 << " SimHitToSimHeader buffer size: "<< m_HitHeaderToHeaders.size()<< endreq;
00116
00117 m_HitHeaderToHeaders.erase(m_propSimHitHeaders[0]);
00118 m_propSimHitHeaders.pop_front();
00119 m_hitEarliests.pop_front();
00120 m_hitLatests.pop_front();
00121
00122 m_log<< MSG::DEBUG<< "After clearing: SimHitHeader buffer size: "<< m_propSimHitHeaders.size()
00123 << " SimHitToSimHeader buffer size: "<< m_HitHeaderToHeaders.size()<< endreq;
00124
00125 return shh;
00126 }
00127 else
00128 {
00129 m_log<< MSG::DEBUG<< "So, need to create a SimHitHeader(SimHeaders) firstly!"<< endreq;
00130
00131 m_ifDeal = false;
00132
00133
00134 for(; ;)
00135 {
00136
00137 SimHeader* sh = new SimHeader();
00138
00139 GenHeader* gh = new GenHeader();
00140
00141
00142 StatusCode sc = m_mixInputSvc->prepareStream();
00143 if(sc.isFailure())
00144 {
00145 m_log<< MSG::ERROR<< "Error when preparing input-streams from files!"<< endreq;
00146 return 0;
00147 }
00148
00149 sc = m_mixInputSvc->getSimHeader(gh, sh);
00150 if(sc.isFailure())
00151 {
00152 m_log<< MSG::ERROR<< "Error when getting a SimHeader from file!"<< endreq;
00153 return 0;
00154 }
00155
00156
00157 delete gh;
00158
00159
00160 if(0 == m_hitsGap.size())
00161 {
00162 m_log<< MSG::DEBUG<< "The Hits Gap vectors: GapV(TimeStamp) size = "
00163 << m_hitsGap.size()<< endreq;
00164
00165 sc = this->fillHitsBuffer(sh);
00166 if(sc.isFailure())
00167 {
00168 m_log<< MSG::ERROR<< "Error when filling the SimHits!"<< endreq;
00169 return 0;
00170 }
00171 continue;
00172 }
00173 else
00174 {
00175 m_log<< MSG::DEBUG<< "The Hits Gap vectors: GapV(TimeStamp) size = "
00176 << m_hitsGap.size()<< endreq;
00177
00178
00179 deque<TimeStamp>::size_type ind = m_hitsGap.size();
00180 ind--;
00181 for(; ; ind--)
00182 {
00183 m_log<< MSG::DEBUG<< "Now the Gap vector's index is: "<< ind<< endreq;
00184 m_log<< MSG::DEBUG<< "Time interval: "<< (sh->earliest() - m_hitsGap[ind]).GetSec()
00185 << "--"<< (sh->earliest() - m_hitsGap[ind]).GetNanoSec()<< endreq;
00186
00187 if((sh->earliest() - m_hitsGap[ind]) < m_minTimeGap)
00188 {
00189 if(0 == ind)
00190 {
00191 m_log<< MSG::DEBUG<< "Now, there is no Gap with next SimHeader!"<< endreq;
00192 m_ifDeal = false;
00193 break;
00194 }
00195 else continue;
00196 }
00197 else
00198 {
00199 m_ifDeal = true;
00200 m_dealInd = ind;
00201 m_log<< MSG::DEBUG<< "The gap is between m_hitsGap["<< ind<< "]: "
00202 << "SimHeader's earliest()"<< endreq;
00203 break;
00204 }
00205 }
00206
00207 if(false == m_ifDeal)
00208 {
00209 m_log<< MSG::DEBUG<< "So, just fill the SimHits buffer!"<< endreq;
00210
00211
00212 sc = this->fillHitsBuffer(sh);
00213 if(sc.isFailure())
00214 {
00215 m_log<< MSG::ERROR<< "Error when filling the SimHits!"<< endreq;
00216 return 0;
00217 }
00218
00219 continue;
00220 }
00221 else
00222 {
00223 m_log<< MSG::DEBUG<< "So, create a new SimHitHeader firstly!"<< endreq;
00224
00225
00226 sc = this->createHitHeaders();
00227 if(sc.isFailure())
00228 {
00229 m_log<< MSG::ERROR<< "Error when creating the new SimHitHeaders!"<< endreq;
00230 return 0;
00231 }
00232
00233 m_log<< MSG::DEBUG<< "OK, then fill the SimHits buffer!"<< endreq;
00234
00235 sc = this->fillHitsBuffer(sh);
00236 if(sc.isFailure())
00237 {
00238 m_log<< MSG::ERROR<< "Error when filling the SimHits!"<< endreq;
00239 return 0;
00240 }
00241
00242 break;
00243 }
00244 }
00245 }
00246 }
00247 }
00248 }
00249
00250
00251 StatusCode PreElecSimSvc::fillHitsBuffer(SimHeader* sh)
00252 {
00253 m_log<< MSG::INFO<< "Fill SimHits buffer..."<< endreq;
00254
00255
00256 const SimHitHeader* shh = sh->hits();
00257 map<short int, SimHitCollection*> mshc = shh->hitCollection();
00258
00259 m_absTime = sh->timeStamp();
00260 m_log<< MSG::DEBUG<< "The current SimHeader time: "<< m_absTime.GetSec()
00261 << "--"<< m_absTime.GetNanoSec()<< endreq;
00262
00263
00264 if(0 == m_hitsGap.size())
00265 {
00266 m_log<< MSG::DEBUG<< "m_SimHits' size is: "<< m_SimHits.size()<< "!"<< endreq;
00267
00268 map<short int, SimHitCollection*>::iterator mit;
00269 for(mit = mshc.begin(); mit != mshc.end(); mit++)
00270 {
00271
00272 m_log<< MSG::INFO<< "In Det: "<< mit->first<< endreq;
00273
00274 SimHitCollection* shc = mit->second;
00275 vector<SimHit*> hits = shc->collection();
00276 if(0 == hits.size()) return StatusCode::FAILURE;
00277
00278
00279 for(vector<SimHit*>::iterator vit = hits.begin(); vit != hits.end(); vit++)
00280 {
00281 MixSimHit mixhit;
00282
00283 TimeStamp temTime = m_absTime;
00284 temTime.Add(((*vit)->hitTime())/1.0e9);
00285 mixhit.hitTime = temTime;
00286
00287 mixhit.trivialtime = (*vit)->hitTime() - (int)((*vit)->hitTime());
00288 m_log<< MSG::DEBUG<< "SimHit Time: "<< mixhit.hitTime.GetSec()
00289 << "--"<< mixhit.hitTime.GetNanoSec()<< endreq;
00290 mixhit.setHit(*vit);
00291 m_SimHits.push_back(mixhit);
00292 }
00293 }
00294
00295 sort(m_SimHits.begin(), m_SimHits.end());
00296
00297 m_log<< MSG::DEBUG<< "After sorting....."<< endreq;
00298 for(deque<MixSimHit>::iterator vit = m_SimHits.begin(); vit != m_SimHits.end(); vit++)
00299 {
00300
00301 m_log<< MSG::DEBUG<< "SimHit time in m_SimHits: "<< vit->hitTime.GetSec()
00302 << "--"<< vit->hitTime.GetNanoSec()<< endreq;
00303 }
00304 }
00305 else
00306 {
00307 deque<TimeStamp>::size_type curind = m_hitsGap.size();
00308 curind--;
00309
00310 if(sh->earliest() > m_hitsGap[curind])
00311 {
00312
00313 deque<MixSimHit> mixhits;
00314 map<short int, SimHitCollection*>::iterator mit;
00315 for(mit = mshc.begin(); mit != mshc.end(); mit++)
00316 {
00317
00318 m_log<< MSG::INFO<< "In Det: "<< mit->first<< endreq;
00319
00320 SimHitCollection* shc = mit->second;
00321 vector<SimHit*> hits = shc->collection();
00322 if(0 == hits.size()) return StatusCode::FAILURE;
00323
00324
00325 for(vector<SimHit*>::iterator vit = hits.begin(); vit != hits.end(); vit++)
00326 {
00327 MixSimHit mixhit;
00328
00329 TimeStamp temTime = m_absTime;
00330 temTime.Add(((*vit)->hitTime())/1.0e9);
00331 mixhit.hitTime = temTime;
00332 mixhit.trivialtime = (*vit)->hitTime() - (int)((*vit)->hitTime());
00333 m_log<< MSG::DEBUG<< "SimHit Time: "<< mixhit.hitTime.GetSec()
00334 << "--"<< mixhit.hitTime.GetNanoSec()<< endreq;
00335 mixhit.setHit(*vit);
00336 mixhits.push_back(mixhit);
00337 }
00338 }
00339
00340
00341 sort(mixhits.begin(), mixhits.end());
00342
00343 m_log<< MSG::DEBUG<< "After sorting....."<< endreq;
00344 for(deque<MixSimHit>::iterator dit = mixhits.begin(); dit != mixhits.end(); dit++)
00345 {
00346
00347 m_log<< MSG::DEBUG<< "SimHit time in mixhits: "<< dit->hitTime.GetSec()
00348 << "--"<< dit->hitTime.GetNanoSec()<< endreq;
00349 m_SimHits.push_back(*dit);
00350 }
00351
00352 m_log<< MSG::DEBUG<< "After merging: "<< endreq;
00353 for(deque<MixSimHit>::iterator vit = m_SimHits.begin(); vit != m_SimHits.end(); vit++)
00354 {
00355
00356 m_log<< MSG::DEBUG<< "SimHit time in m_SimHits: "<< vit->hitTime.GetSec()
00357 << "--"<< vit->hitTime.GetNanoSec()<< endreq;
00358 }
00359
00360 mixhits.clear();
00361 }
00362 else
00363 {
00364
00365 deque<MixSimHit> mixhits;
00366 map<short int, SimHitCollection*>::iterator mit;
00367 for(mit = mshc.begin(); mit != mshc.end(); mit++)
00368 {
00369
00370 m_log<< MSG::INFO<< "In Det: "<< mit->first<< endreq;
00371
00372 SimHitCollection* shc = mit->second;
00373 vector<SimHit*> hits = shc->collection();
00374 if(0 == hits.size()) return StatusCode::FAILURE;
00375
00376
00377 for(vector<SimHit*>::iterator vit = hits.begin(); vit != hits.end(); vit++)
00378 {
00379 MixSimHit mixhit;
00380
00381 TimeStamp temTime = m_absTime;
00382 temTime.Add(((*vit)->hitTime())/1.0e9);
00383 mixhit.hitTime = temTime;
00384 mixhit.trivialtime = (*vit)->hitTime() - (int)((*vit)->hitTime());
00385 m_log<< MSG::DEBUG<< "SimHit Time: "<< mixhit.hitTime.GetSec()
00386 << "--"<< mixhit.hitTime.GetNanoSec()<< endreq;
00387 mixhit.setHit(*vit);
00388 mixhits.push_back(mixhit);
00389 }
00390 }
00391
00392
00393 sort(mixhits.begin(), mixhits.end());
00394 merge(m_SimHits.begin(), m_SimHits.end(), mixhits.begin(), mixhits.end(), back_inserter(m_tranSimHits));
00395 m_SimHits = m_tranSimHits;
00396
00397 m_log<< MSG::DEBUG<< "After merging: "<< endreq;
00398 for(deque<MixSimHit>::iterator vit = m_SimHits.begin(); vit != m_SimHits.end(); vit++)
00399 {
00400
00401 m_log<< MSG::DEBUG<< "SimHit time in m_SimHits: "<< vit->hitTime.GetSec()
00402 << "--"<< vit->hitTime.GetNanoSec()<< endreq;
00403 }
00404
00405 m_tranSimHits.clear();
00406 mixhits.clear();
00407 }
00408 }
00409
00410
00411 StatusCode sc = this->findGap();
00412 if(sc.isFailure())
00413 {
00414 m_log<< MSG::ERROR<< "Error in findGap()!"<< endreq;
00415 return StatusCode::FAILURE;
00416 }
00417
00418 return StatusCode::SUCCESS;
00419 }
00420
00421 StatusCode PreElecSimSvc::findGap()
00422 {
00423 m_log<< MSG::DEBUG<< "Find gap here(update the Gap Vector)..."<< endreq;
00424
00425
00426
00427 m_hitsGap.clear();
00428
00429 if(m_SimHits.size() < 2)
00430 {
00431 m_log<< MSG::WARNING<< "No enough SimHits in m_SimHits! Be carefull!"<< endreq;
00432
00433
00434
00435
00436 m_hitsGap.push_back(m_SimHits[0].hitTime);
00437
00438 for(vector<TimeStamp>::size_type ind = 0; ind != m_hitsGap.size(); ind++)
00439 {
00440 m_log<< MSG::DEBUG<< "TimeStamp Gap: "<< m_hitsGap[ind].GetSec()<< "--"
00441 << m_hitsGap[ind].GetNanoSec()<< endreq;
00442 }
00443
00444 return StatusCode::SUCCESS;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454 deque<MixSimHit>::iterator dit, ddit, dend;
00455 dend = m_SimHits.end();
00456 dend--;
00457 for(dit = m_SimHits.begin(); dit != dend; dit++)
00458 {
00459 ddit = dit + 1;
00460 TimeStamp timeinter = ddit->hitTime - dit->hitTime;
00461
00462
00463 if(timeinter > m_minTimeGap)
00464 {
00465 m_log<< MSG::DEBUG<< "There is one gap!"<< endreq;
00466
00467 m_hitsGap.push_back(dit->hitTime);
00468 }
00469 }
00470
00471
00472 m_log<< MSG::DEBUG<< "There is the last gap!"<< endreq;
00473 m_hitsGap.push_back(dend->hitTime);
00474
00475 for(vector<TimeStamp>::size_type ind = 0; ind != m_hitsGap.size(); ind++)
00476 {
00477 m_log<< MSG::DEBUG<< "TimeStamp Gap: "<< m_hitsGap[ind].GetSec()<< "--"
00478 << m_hitsGap[ind].GetNanoSec()<< endreq;
00479 }
00480
00481 return StatusCode::SUCCESS;
00482 }
00483
00484 StatusCode PreElecSimSvc::createHitHeaders()
00485 {
00486
00487
00488 m_log<< MSG::INFO<< "Create new SimHitHeaders........"<< endreq;
00489
00490
00491 deque<TimeStamp>::size_type iind;
00492 for(iind = 0; iind <= m_dealInd; iind++)
00493 {
00494
00495 m_log<< MSG::DEBUG<< "Create one SimHitHeader before SimHit("
00496 << m_hitsGap[0].GetSec()<< "--"<< m_hitsGap[0].GetNanoSec()<< ")"<< endreq;
00497
00498
00499 m_hitEarliests.push_back(m_SimHits[0].hitTime);
00500 m_hitLatests.push_back(m_hitsGap[0]);
00501
00502 SimHitHeader* shh = new SimHitHeader();
00503
00504 if(0 == shh)
00505 {
00506 m_log<< MSG::ERROR<< "Can not create a new SimHitHeader!"<< endreq;
00507 return StatusCode::FAILURE;
00508 }
00509 m_log<< MSG::INFO<< "Create a new SimHitHeader: "<< shh<< endreq;
00510
00511
00512 vector<const SimHeader*> inputHeaders;
00513
00514
00515 map<short int, SimHitCollection*>& mshc = shh->hitCollection();
00516
00517
00518 for( ; ; )
00519 {
00520 m_log<< MSG::DEBUG<< "The SimHit buffer size is: "<< m_SimHits.size()<< endreq;
00521
00522 deque<MixSimHit>::iterator dit = m_SimHits.begin();
00523 if(dit == m_SimHits.end())
00524 {
00525 m_log<< MSG::INFO<< "The SimHits Buffer is empty!"<< endreq;
00526 break;
00527 }
00528
00529 if(dit->hitTime <= m_hitsGap[0])
00530 {
00531 Detector Det = dit->sht->hc()->detector();
00532 short int det = Det.siteDetPackedData();
00533 const SimHeader* currentHeader = dit->sht->hc()->header()->header();
00534
00535 m_log<< MSG::DEBUG<< "This hit is from detector: "<< AsString(Det.detectorId())<< endreq;
00536
00537
00538 if(0 == mshc.size() || mshc.find(det) == mshc.end())
00539 {
00540 m_log<< MSG::DEBUG<< "map<short int, SimHitCollection*> size: "
00541 << mshc.size()<< " new detectorId: "<< AsString(Det.detectorId())<< endreq;
00542
00543
00544 SimHitCollection* shc = new SimHitCollection();
00545 shc->setHeader(shh);
00546 shc->setDetector(Det);
00547
00548 vector<SimHit*>& shts = shc->collection();
00549 shts.push_back(dit->sht);
00550 mshc[det] = shc;
00551
00552 m_log<< MSG::DEBUG<< "Now the map size is: "<< mshc.size()<< endreq;
00553
00554
00555 vector<const SimHeader*>::iterator vit = inputHeaders.begin();
00556 if(0 == inputHeaders.size())
00557 {
00558 m_log<< MSG::DEBUG<< "New SimHeader: "<< currentHeader<< endreq;
00559 inputHeaders.push_back(currentHeader);
00560 }
00561 else
00562 {
00563 bool found = false;
00564 for( ; vit != inputHeaders.end(); vit++)
00565 {
00566 if(currentHeader == (*vit))
00567 {
00568 found = true;
00569 m_log<< MSG::DEBUG<< "SimHeader: "<< currentHeader<< " already in InputHeaders!"<< endreq;
00570 break;
00571 }
00572 }
00573 if(false == found)
00574 {
00575 m_log<< MSG::DEBUG<< "SimHeader: "<< currentHeader<< " should be added into InputHeaders!"<< endreq;
00576 inputHeaders.push_back(currentHeader);
00577 }
00578 }
00579
00580 m_SimHits.pop_front();
00581 continue;
00582 }
00583 else
00584 {
00585 m_log<< MSG::DEBUG<< "map<short int, SimHitCollection*> size: "
00586 << mshc.size()<< " detectorId: "<< AsString(Det.detectorId())<< endreq;
00587
00588 vector<SimHit*>& shts = mshc[det]->collection();
00589 shts.push_back(dit->sht);
00590
00591
00592 vector<const SimHeader*>::iterator vit = inputHeaders.begin();
00593 if(0 == inputHeaders.size())
00594 {
00595 m_log<< MSG::DEBUG<< "New SimHeader: "<< currentHeader<< endreq;
00596 inputHeaders.push_back(currentHeader);
00597 }
00598 else
00599 {
00600 bool found = false;
00601 for( ; vit != inputHeaders.end(); vit++)
00602 {
00603 if(currentHeader == (*vit))
00604 {
00605 found = true;
00606 m_log<< MSG::DEBUG<< "SimHeader: "<< currentHeader<< " already in InputHeaders!"<< endreq;
00607 break;
00608 }
00609 }
00610 if(false == found)
00611 {
00612 m_log<< MSG::DEBUG<< "SimHeader: "<< currentHeader<< " should be added into InputHeaders!"<< endreq;
00613 inputHeaders.push_back(currentHeader);
00614 }
00615 }
00616
00617 m_SimHits.pop_front();
00618 continue;
00619 }
00620 }
00621 else
00622 {
00623 m_log<< MSG::DEBUG<< "Now create next SimHitHeader!"<< endreq;
00624 break;
00625 }
00626 }
00627
00628 m_log<< MSG::INFO<< "Get one SimHitHeader: "<< shh<< " with "<< shh->hitCollection().size()<< endreq;
00629 m_propSimHitHeaders.push_back(shh);
00630 m_HitHeaderToHeaders[shh] = inputHeaders;
00631 m_log<< MSG::DEBUG<< "Now prepare the SimHitHeader: "<< shh<< " and "
00632 << m_HitHeaderToHeaders.size()<< " SimHeaders!"<< endreq;
00633 m_hitsGap.pop_front();
00634 }
00635
00636 return StatusCode::SUCCESS;
00637 }