00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 extern "C" {
00028
00029 }
00030 #include <iostream>
00031 #include <fstream>
00032 #include <cmath>
00033 #include <cassert>
00034 #include <cstdlib>
00035
00036
00037
00038
00039 #include "GaudiKernel/MsgStream.h"
00040 #include "GaudiKernel/AlgFactory.h"
00041 #include "GaudiKernel/SmartDataPtr.h"
00042 #include "GaudiKernel/IDataProviderSvc.h"
00043 #include "GaudiKernel/PropertyMgr.h"
00044 #include "GaudiKernel/ISvcLocator.h"
00045 #include "GaudiKernel/Bootstrap.h"
00046
00047 #include "EventModel/EventModel.h"
00048 #include "EventModel/Event.h"
00049
00050 #include "EventModel/EventHeader.h"
00051
00052 #include "EvtRecEvent/EvtRecEvent.h"
00053 #include "EvtRecEvent/EvtRecTrack.h"
00054 #include "EmcRawEvent/EmcDigi.h"
00055 #include "EmcRecEventModel/RecEmcEventModel.h"
00056 #include "EmcRecGeoSvc/EmcRecGeoSvc.h"
00057 #include "EmcBhaCalib/EmcSelBhaEvent.h"
00058
00059 #include "CLHEP/Vector/ThreeVector.h"
00060 #include "CLHEP/Geometry/Point3D.h"
00061 #ifndef ENABLE_BACKWARDS_COMPATIBILITY
00062 typedef HepGeom::Point3D<double> HepPoint3D;
00063 #endif
00064 #include "CLHEP/Random/RandGauss.h"
00065
00066 using namespace std;
00067 using namespace Event;
00068 using CLHEP::Hep3Vector;
00069 using CLHEP::RandGauss;
00070
00071 #include <vector>
00072 #include <list>
00073 typedef std::vector<int> Vint;
00074 typedef std::vector<HepLorentzVector> Vp4;
00075
00076
00077
00078
00079
00080 EmcSelBhaEvent::EmcSelBhaEvent(const std::string& name, ISvcLocator* pSvcLocator)
00081 :Algorithm(name, pSvcLocator),
00082 m_vr0cut(1.0),
00083 m_vz0cut(5.0),
00084 m_lowEnergyShowerCut(0.1),
00085 m_highEnergyShowerCut(0.5),
00086 m_matchThetaCut(0.2),
00087 m_matchPhiCut(0.2),
00088 m_highMomentumCut(0.5),
00089 m_EoPMaxCut(1.3),
00090 m_EoPMinCut(0.6),
00091 m_minAngShEnergyCut(0.2),
00092 m_minAngCut(0.3),
00093 m_acolliCut(0.03),
00094 m_eNormCut(0.5),
00095 m_pNormCut(0.5),
00096 m_oneProngMomentumCut(1.2),
00097
00098 m_digiRangeCut(6),
00099 m_ShEneThreshCut(0.02),
00100 m_ShEneLeptonCut(1.),
00101 m_minNrXtalsShowerCut(10),
00102 m_maxNrXtalsShowerCut(70),
00103 m_phiDiffMinCut(0.05),
00104 m_phiDiffMaxCut(0.2),
00105 m_nrShThreshCut(20),
00106 m_thetaDiffCut(0.04),
00107 m_LATCut(0.8),
00108
00109 m_showersAccepted(0),
00110
00111 m_writeMVToFile(true),
00112 m_fileExt(""),
00113 m_inputFileDir("../InputData/"),
00114 m_fileDir("/ihepbatch/besdata/public/liucx/Calib/"),
00115 m_selMethod("Ixtal"),
00116 m_nXtals(6240),
00117 m_sigmaCut(1.),
00118 m_beamEnergy(1.843),
00119 m_MsgFlag(0)
00120
00121 {
00122
00123
00124
00125
00126 declareProperty ("Vr0cut", m_vr0cut);
00127 declareProperty ("Vz0cut", m_vz0cut);
00128
00129 declareProperty ("lowEnergyShowerCut", m_lowEnergyShowerCut);
00130 declareProperty ("highEnergyShowerCut", m_highEnergyShowerCut);
00131 declareProperty ("matchThetaCut", m_matchThetaCut);
00132 declareProperty ("matchPhiCut", m_matchPhiCut);
00133
00134 declareProperty ("highMomentumCut", m_highMomentumCut);
00135 declareProperty ("EoPMaxCut", m_EoPMaxCut);
00136 declareProperty ("EoPMinCut", m_EoPMinCut);
00137 declareProperty ("minAngShEnergyCut", m_minAngShEnergyCut);
00138 declareProperty ("minAngCut", m_minAngCut);
00139 declareProperty ("acolliCut", m_acolliCut);
00140 declareProperty ("eNormCut", m_eNormCut);
00141 declareProperty ("pNormCut", m_pNormCut);
00142 declareProperty ("oneProngMomentumCut", m_oneProngMomentumCut);
00143
00144
00145
00146 declareProperty("digiRangeCut", m_digiRangeCut);
00147
00148 declareProperty("ShEneThreshCut", m_ShEneThreshCut);
00149 declareProperty("ShEneLeptonCut", m_ShEneLeptonCut);
00150
00151 declareProperty("minNrXtalsShowerCut", m_minNrXtalsShowerCut);
00152 declareProperty("maxNrXtalsShowerCut", m_maxNrXtalsShowerCut);
00153 declareProperty("phiDiffMinCut", m_phiDiffMinCut);
00154 declareProperty("phiDiffMaxCut", m_phiDiffMaxCut);
00155 declareProperty("nrShThreshCut", m_nrShThreshCut);
00156 declareProperty("thetaDiffCut", m_thetaDiffCut);
00157 declareProperty("LATCut", m_LATCut);
00158
00159
00160 declareProperty("writeMVToFile", m_writeMVToFile);
00161 declareProperty("fileExt", m_fileExt);
00162 declareProperty("fileDir", m_fileDir);
00163 declareProperty("inputFileDir", m_inputFileDir);
00164 declareProperty("selMethod",m_selMethod);
00165 declareProperty("sigmaCut", m_sigmaCut);
00166 declareProperty("ReadBeamEFromDB", m_ReadBeamEFromDB = false );
00167
00168 declareProperty("beamEnergy", m_beamEnergy);
00169 declareProperty("elecSaturation", m_elecSaturation = false );
00170
00171 declareProperty("MsgFlag", m_MsgFlag);
00172
00173
00174 int j = 0;
00175 m_index = new int*[56];
00176 for (j=0;j<56;j++ ) {
00177 m_index[j] = new int[120];
00178 for ( int k=0; k<120; k++) {
00179 m_index[j][k]=-1;
00180 }
00181 }
00182
00183 m_iGeoSvc=0;
00184
00185 for (int i=0; i<6240;i++)
00186 {
00187 m_inputConst[i] = 1.0;
00188 }
00189
00190 m_irun=0;
00191 }
00192
00193
00194
00195
00196 EmcSelBhaEvent::~EmcSelBhaEvent() {
00197
00198 if ( m_index != 0 ) {
00199 for (int i =0; i<56; i++ )
00200 delete[] m_index[i];
00201 delete[] m_index;
00202 m_index = 0;
00203 }
00204
00206 for (int j=0;j<6240;j++ ) {
00207 m_measure[j]=-1;
00208 }
00209 }
00210
00211 StatusCode EmcSelBhaEvent::initialize(){
00212
00213 MsgStream log(msgSvc(), name());
00214 log << MSG::INFO << "in initialize()" << endreq;
00215
00216
00217 m_showersAccepted=0;
00218 m_events=0;
00219 m_Nothing=0;
00220 m_OneProng=0;
00221
00222 m_TwoProngMatched=0;
00223
00224 m_TwoProngOneMatched=0;
00225
00226
00227
00228 initGeom();
00229
00230
00231 if ( m_writeMVToFile )
00232 myCalibData = new EmcBhaCalibData(m_nXtals,m_MsgFlag);
00233 else
00234 myCalibData = 0;
00235
00236
00237
00238 ISvcLocator* svcLocator = Gaudi::svcLocator();
00239 StatusCode sc = svcLocator->service("EmcRecGeoSvc",m_iGeoSvc);
00240 if(sc!=StatusCode::SUCCESS) {
00241 cout<<"Error: Can't get EmcRecGeoSvc"<<endl;
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 StatusCode scBeamEnergy;
00258 scBeamEnergy = Gaudi::svcLocator() -> service("BeamEnergySvc", m_BeamEnergySvc);
00259
00260 if( scBeamEnergy != StatusCode::SUCCESS){
00261 log << MSG::ERROR << "can not use BeamEnergySvc" << endreq;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 readCorFun();
00271
00272
00273
00274 readEsigma();
00275
00276
00277
00278 readDepEneFun();
00279
00280
00281
00282 readSigmaExp();
00283 readRawPeakIxtal();
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 return StatusCode::SUCCESS;
00294 }
00295
00296
00297 StatusCode EmcSelBhaEvent::execute() {
00298
00299 MsgStream log(msgSvc(), name());
00300 log << MSG::DEBUG << "in execute()" << endreq;
00301
00302
00303
00304 int event, run;
00305
00306 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
00307
00308 run=eventHeader->runNumber();
00309 event=eventHeader->eventNumber();
00310
00311 m_events++;
00312 m_run = run;
00313
00314
00316
00318 if(m_ReadBeamEFromDB&&m_irun!=run){
00319 m_irun=run;
00320 m_BeamEnergySvc ->getBeamEnergyInfo();
00321 if(m_BeamEnergySvc->isRunValid())
00322 m_beamEnergy=m_BeamEnergySvc->getbeamE();
00323
00324
00325
00326
00327 cout<< "beamEnergy="<< m_beamEnergy<<endl;
00328 double the=0.011;
00329 double phi=0;
00330 HepLorentzVector ptrk;
00331 ptrk.setPx(m_beamEnergy*sin(the)*cos(phi));
00332 ptrk.setPy(m_beamEnergy*sin(the)*sin(phi));
00333 ptrk.setPz(m_beamEnergy*cos(the));
00334 ptrk.setE(m_beamEnergy);
00335
00336 ptrk=ptrk.boost(-0.011,0,0);
00337
00338 cout<< "beamEnergy="<< m_beamEnergy<<" cms "<<ptrk.e()<<" ratio="<< (m_beamEnergy-ptrk.e())/ptrk.e()<<endl;
00339 m_beamEnergy=ptrk.e();
00340 }
00341
00343
00344
00345 for (int j=0;j<6240;j++ ) {
00346 m_measure[j]=-1;
00347 }
00348
00349 if (m_elecSaturation==true)
00350 {
00351
00353 SmartDataPtr<EmcDigiCol> emcDigiCol(eventSvc(),"/Event/Digi/EmcDigiCol");
00354 if (!emcDigiCol) {
00355 log << MSG::FATAL << "Could not find EMC digi" << endreq;
00356 return( StatusCode::FAILURE);
00357 }
00358
00359 EmcDigiCol::iterator iter3;
00360 for (iter3=emcDigiCol->begin();iter3!= emcDigiCol->end();iter3++) {
00361 RecEmcID id((*(iter3))->identify());
00362
00363
00364 unsigned int npart,ntheta,nphi;
00365 npart = EmcID::barrel_ec(id);
00366 ntheta = EmcID::theta_module(id);
00367 nphi = EmcID::phi_module(id);
00368
00369 unsigned int newthetaInd;
00370 if (npart==0) newthetaInd = ntheta;
00371 if (npart==1) newthetaInd = ntheta + 6;
00372 if (npart==2) newthetaInd = 55 - ntheta;
00373
00374 int ixtal= index(newthetaInd,nphi);
00375 m_measure[ixtal]=(*iter3)->getMeasure();
00376
00377
00378 }
00379 }
00380
00382
00383 myBhaEvt = new EmcBhabhaEvent();
00384
00385
00386 SelectBhabha();
00387 if(m_selectedType == BhabhaType::OneProng) m_OneProng++;
00388
00389 if(m_selectedType == BhabhaType::TwoProngMatched) m_TwoProngMatched++;
00390
00391 if(m_selectedType == BhabhaType::TwoProngOneMatched) m_TwoProngOneMatched++;
00392
00393 if(m_selectedType == BhabhaType::Nothing){
00394 m_Nothing++;
00395 }
00396
00397
00398
00399 if (m_selectedType == BhabhaType::TwoProngMatched) {
00400 FillBhabha();
00401
00402
00403
00404 CollectBhabha();
00405 }
00406
00407 delete myBhaEvt;
00408 myBhaEvt=0;
00409
00410 return StatusCode::SUCCESS;
00411 }
00412
00413
00414 StatusCode EmcSelBhaEvent::finalize() {
00415
00416 MsgStream log(msgSvc(), name());
00417
00418 log << MSG::INFO << "in finalize()" << endreq;
00419
00420
00421 OutputMV();
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431 if ( m_writeMVToFile ) {
00432 delete myCalibData;
00433 myCalibData = 0;
00434 }
00435
00436 cout<<"Event="<<m_events<<endl;
00437
00438 cout<<"m_Nothing ="<<m_Nothing <<endl;
00439 cout<<"m_OneProng="<<m_OneProng<<endl;
00440
00441 cout<<"m_TwoProngMatched="<<m_TwoProngMatched<<endl;
00442
00443 cout<<"m_TwoProngOneMatched="<<m_TwoProngOneMatched<<endl;
00444
00445 cout<<"m_showersAccepted="<<m_showersAccepted<<endl;
00446
00447 return StatusCode::SUCCESS;
00448 }
00449
00450
00451
00452
00453
00454 double
00455 EmcSelBhaEvent::expectedEnergy( long int ixtal ) {
00456
00457 EmcStructure* theEmcStruc=new EmcStructure() ;
00458 theEmcStruc->setEmcStruc();
00459
00460 unsigned int module, ntheta, nphi,ThetaIndex;
00461
00462 module=theEmcStruc->getPartId(ixtal);
00463 ntheta=theEmcStruc->getTheta(ixtal);
00464 nphi=theEmcStruc->getPhi(ixtal);
00465
00466 if (module==0) ThetaIndex = ntheta;
00467 if (module==1) ThetaIndex = ntheta + 6;
00468 if (module==2) ThetaIndex = 55 - ntheta;
00469
00470 RecEmcID shId= EmcID::crystal_id(module,ntheta,nphi);
00471 HepPoint3D SeedPos(0,0,0);
00472 SeedPos=m_iGeoSvc->GetCFrontCenter(shId);
00473
00474
00475 double theta=SeedPos.theta();
00476 double phi=SeedPos.phi();
00477 HepLorentzVector ptrk;
00478 ptrk.setPx(m_beamEnergy*sin(theta)*cos(phi));
00479 ptrk.setPy(m_beamEnergy*sin(theta)*sin(phi));
00480 ptrk.setPz(m_beamEnergy*cos(theta));
00481 ptrk.setE(m_beamEnergy);
00482
00483 ptrk=ptrk.boost(0.011,0,0);
00484
00485 double depoEne_lab = m_corFun[ThetaIndex]*ptrk.e();
00486
00487 return depoEne_lab;
00488 }
00489
00490
00491
00492
00493 StatusCode EmcSelBhaEvent::SelectBhabha(){
00494
00495 MsgStream log(msgSvc(), name());
00496
00497 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
00498
00499 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
00500
00501 m_selectedType = BhabhaType::Nothing;
00502 m_events++;
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 Vint iGam;
00538 iGam.clear();
00539
00540
00541
00542
00543
00544
00545
00546
00547 for(int i = 0; i < evtRecEvent->totalTracks(); i++){
00548
00549 if(i>=evtRecTrkCol->size()) break;
00550
00551 EvtRecTrackIterator itTrk=evtRecTrkCol->begin() + i;
00552
00553 if((*itTrk)->isEmcShowerValid()) {
00554
00555
00556 RecEmcShower *theShower = (*itTrk)->emcShower();
00557 int TrkID = (*itTrk)->trackId();
00558 double Shower5x5=theShower->e5x5();
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583 if (Shower5x5>1.1){
00584
00585 iGam.push_back( TrkID );
00586
00587
00588 }
00589
00590 }
00591 }
00592 int nGam = iGam.size();
00593 log << MSG::DEBUG << "num Good Photon " << nGam << " , " <<evtRecEvent->totalNeutral() <<endreq;
00594
00595
00596 if (nGam==2) {
00597
00598 m_selectedType = BhabhaType::TwoProngMatched;
00599 m_selectedTrkID1 =iGam[0];
00600 m_selectedTrkID2 = iGam[1];
00601
00602 }
00603
00604
00605 return StatusCode::SUCCESS;
00606
00607 }
00608
00609
00610
00611 void
00612 EmcSelBhaEvent::FillBhabha(){
00613
00614 MsgStream log(msgSvc(), name());
00615
00616 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
00617 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
00618
00619 EvtRecTrackIterator itTrk1 = evtRecTrkCol->begin() + m_selectedTrkID1;
00620
00621 RecEmcShower *theShower1 = (*itTrk1)->emcShower();
00622
00623
00624 EvtRecTrackIterator itTrk2 = evtRecTrkCol->begin() + m_selectedTrkID2;
00625
00626 RecEmcShower *theShower2 = (*itTrk2)->emcShower();
00627
00628 EvtRecTrackIterator itTrk;
00629 RecEmcShower *theShower;
00630
00631
00632
00633
00634
00635 EmcShower* aShower =new EmcShower();
00636
00637 double ene,theta,phi,eseed;
00638 double showerX,showerY,showerZ;
00639 long int thetaIndex,phiIndex,numberOfDigis;
00640
00641 RecEmcID showerId;
00642 unsigned int showerModule;
00643
00644 HepPoint3D showerPosition(0,0,0);
00645
00646 if ( ! m_showerList.empty()) m_showerList.clear();
00647
00648 for (int ish=0; ish<2; ish++){
00649
00650 if (ish==0) {
00651 itTrk= itTrk1;
00652 theShower=theShower1;
00653 }
00654 if (ish==1) {
00655 itTrk= itTrk2;
00656 theShower=theShower2;
00657 }
00658
00659 ene=theShower->e5x5();
00660 eseed=theShower->eSeed();
00661
00662
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00686
00687 showerPosition = theShower->position();
00688 theta = theShower->theta();
00689 phi= theShower->phi();
00690 showerX=theShower->x();
00691 showerY=theShower->y();
00692 showerZ=theShower->z();
00693
00694 showerId = theShower->getShowerId();
00695 showerModule = EmcID::barrel_ec(showerId);
00696
00697
00698
00699
00700 thetaIndex=EmcID::theta_module(showerId);
00701
00702 phiIndex=EmcID::phi_module(showerId);
00703
00704
00705
00706
00707 EmcShDigi* aShDigi= new EmcShDigi();
00708 EmcShDigi maxima =EmcShDigi();
00709 list<EmcShDigi> shDigiList;
00710 RecEmcID cellId;
00711 unsigned int module;
00712 double digiEne, time, fraction, digiTheta, digiPhi;
00713 double digiX, digiY, digiZ;
00714 long int digiThetaIndex,digiPhiIndex;
00715 HepPoint3D digiPos(0,0,0);
00716
00717 RecEmcFractionMap::const_iterator ciFractionMap;
00718
00719 if ( ! shDigiList.empty()) shDigiList.clear();
00720 RecEmcFractionMap fracMap5x5=theShower->getFractionMap5x5();
00721 for(ciFractionMap=fracMap5x5.begin();
00722 ciFractionMap!=fracMap5x5.end();
00723 ciFractionMap++){
00724
00725 digiEne = ciFractionMap->second.getEnergy();
00726
00727 time = ciFractionMap->second.getTime();
00728 fraction = ciFractionMap->second.getFraction();
00729
00730 cellId=ciFractionMap->second.getCellId();
00731
00732 digiPos=m_iGeoSvc->GetCFrontCenter(cellId);
00733 digiTheta = digiPos.theta();
00734 digiPhi = digiPos.phi();
00735 digiX = digiPos.x();
00736 digiY = digiPos.y();
00737 digiZ = digiPos.z();
00738
00739 module=EmcID::barrel_ec(cellId);
00740
00741
00742
00743 digiThetaIndex=EmcID::theta_module(cellId);
00744
00745 digiPhiIndex = EmcID::phi_module(cellId);
00746
00747
00748 aShDigi->setEnergy(digiEne);
00749 aShDigi->setTheta(digiTheta);
00750 aShDigi->setPhi(digiPhi);
00751 aShDigi->setModule(module);
00752 aShDigi->setThetaIndex(digiThetaIndex);
00753 aShDigi->setPhiIndex(digiPhiIndex);
00754 aShDigi->setTime(time);
00755 aShDigi->setFraction(fraction);
00756 aShDigi->setWhere(digiPos);
00757 aShDigi->setY(digiX);
00758 aShDigi->setY(digiY);
00759 aShDigi->setZ(digiZ);
00760
00761 shDigiList.push_back(*aShDigi);
00762
00763 }
00764 shDigiList.sort();
00765
00766 numberOfDigis = shDigiList.size();
00767
00768 maxima = *(--shDigiList.end());
00769
00770
00771
00772 aShower->setEnergy(ene);
00773 aShower->setTheta(theta);
00774 aShower->setPhi(phi);
00775 aShower->setModule(showerModule);
00776 aShower->setThetaIndex(thetaIndex);
00777 aShower->setPhiIndex(phiIndex);
00778 aShower->setNumberOfDigis(numberOfDigis);
00779 aShower->setDigiList(shDigiList);
00780 aShower->setMaxima(maxima);
00781 aShower->setWhere(showerPosition);
00782 aShower->setX(showerX);
00783 aShower->setY(showerY);
00784 aShower->setZ(showerZ);
00785 m_showerList.push_back(*aShower);
00786 delete aShDigi;
00787
00788 }
00789
00790
00791 delete aShower;
00792
00794
00795 if (m_showerList.size() == 2) {
00796
00797 list<EmcShower>::const_iterator iter_Sh;
00798 int itrk=0;
00799 for (iter_Sh = m_showerList.begin();
00800 iter_Sh != m_showerList.end(); iter_Sh++) {
00801
00802 itrk++;
00803 EmcShower shower;
00804 shower = EmcShower();
00805 double theta = iter_Sh->theta();
00806 shower = *iter_Sh;
00807
00808
00809
00810
00811 if (itrk==1){
00812
00813 myBhaEvt->setPositron()->setShower(shower);
00814 myBhaEvt->setPositron()->setTheta(shower.theta());
00815 myBhaEvt->setPositron()->setPhi(shower.phi());
00816 unsigned int newthetaInd ;
00817
00818
00819 if (shower.module()==0) newthetaInd = shower.thetaIndex();
00820 if (shower.module()==1) newthetaInd = shower.thetaIndex() + 6;
00821 if (shower.module()==2) newthetaInd = 55 - shower.thetaIndex();
00822
00823 myBhaEvt->setPositron()->setThetaIndex(newthetaInd);
00824
00825 unsigned int newphiInd=shower.phiIndex();
00826 myBhaEvt->setPositron()->setPhiIndex(newphiInd);
00827 myBhaEvt->setPositron()->setFound(true);
00828
00829
00830 log << MSG::INFO << name() << ": Positron: theta,phi energy "
00831 << myBhaEvt->positron()->theta() << ","
00832 << myBhaEvt->positron()->shower().phi() << " "
00833 << myBhaEvt->positron()->shower().energy()
00834 << endreq;
00835
00836
00837 }
00838
00839
00840
00841 if (itrk==2){
00842
00843 myBhaEvt->setElectron()->setShower(shower);
00844 myBhaEvt->setElectron()->setTheta(shower.theta());
00845 myBhaEvt->setElectron()->setPhi(shower.phi());
00846 unsigned int newthetaInd;
00847
00848
00849 if (shower.module()==0) newthetaInd = shower.thetaIndex();
00850 if (shower.module()==1) newthetaInd = shower.thetaIndex() + 6;
00851 if (shower.module()==2) newthetaInd = 55 - shower.thetaIndex();
00852
00853 myBhaEvt->setElectron()->setThetaIndex(newthetaInd);
00854 unsigned int newphiInd=shower.phiIndex();
00855 myBhaEvt->setElectron()->setPhiIndex(newphiInd);
00856 myBhaEvt->setElectron()->setFound(true);
00857
00858 log << MSG::INFO << name() << ": Electron: theta,phi energy "
00859 << myBhaEvt->electron()->theta() << ","
00860 << myBhaEvt->electron()->shower().phi() << " "
00861 << myBhaEvt->electron()->shower().energy()
00862 << endreq;
00863
00864 }
00865 }
00866 }
00867
00868
00869 }
00870
00871 void
00872 EmcSelBhaEvent::CollectBhabha(){
00873
00874 MsgStream log(msgSvc(), name());
00875
00876
00877 if ( 0 != myBhaEvt->positron()->found() ||
00878 0 != myBhaEvt->electron()->found() ) {
00879
00880 m_taken++;
00881
00882 double calibEnergy=0.;
00883 double energyError=0.;
00884
00885
00886 if (myBhaEvt->electron()->found() ) {
00887
00888
00889
00890
00891
00892
00893 unsigned int module, ntheta, nphi;
00894 if ( myBhaEvt->electron()->thetaIndex()<=5) {
00895 module=0;
00896 ntheta=myBhaEvt->electron()->thetaIndex();
00897
00898 } else if ( myBhaEvt->electron()->thetaIndex()>=50){
00899 module=2;
00900 ntheta=55-myBhaEvt->electron()->thetaIndex();
00901
00902 } else {
00903 module=1;
00904 ntheta=myBhaEvt->electron()->thetaIndex()-6;
00905 }
00906 nphi=myBhaEvt->electron()->phiIndex();
00907
00908
00909 RecEmcID shId= EmcID::crystal_id(module,ntheta,nphi);
00910 HepPoint3D SeedPos(0,0,0);
00911 SeedPos=m_iGeoSvc->GetCFrontCenter(shId);
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921 calibEnergy = myBhaEvt->
00922 getDepoMCShowerEnergy_lab(SeedPos.theta(),
00923 SeedPos.phi(),
00924 myBhaEvt->electron()->thetaIndex(),
00925 myBhaEvt->electron()->phiIndex(),
00926 m_corFun[myBhaEvt->electron()->thetaIndex()],
00927 m_beamEnergy);
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937 if ( calibEnergy > 0 ) {
00938
00939 energyError = myBhaEvt->
00940 getErrorDepoMCShowerEnergy(myBhaEvt->electron()->thetaIndex(),
00941 myBhaEvt->electron()->phiIndex(),
00942 m_eSigma[myBhaEvt->electron()->thetaIndex()]);
00943
00944 } else
00945 log << MSG::WARNING << " Did not find MC deposited cluster energy "
00946 <<" for this cluster: thetaInd: "
00947 << myBhaEvt->electron()->thetaIndex()
00948 << " phiInd: "
00949 << myBhaEvt->electron()->phiIndex()
00950 << endl
00951 << "Will not use this cluster for the Emc "
00952 << "Bhabha calibration !"
00953 << endreq;
00954
00955
00956 myBhaEvt->setElectron()->setErrorOnCalibEnergy(energyError);
00957 myBhaEvt->setElectron()->setCalibEnergy(calibEnergy);
00958
00959
00960
00961 } else
00962 log << MSG::INFO<< name()
00963 << ": Electron was not selected ! "
00964 << endreq;
00965
00966 calibEnergy=0.;
00967 energyError=0.;
00968
00969
00970 if (myBhaEvt->positron()->found() ) {
00971
00972
00973
00974
00975
00976
00977 unsigned int module, ntheta, nphi;
00978 if ( myBhaEvt->positron()->thetaIndex()<=5) {
00979 module=0;
00980 ntheta=myBhaEvt->positron()->thetaIndex();
00981
00982 } else if ( myBhaEvt->positron()->thetaIndex()>=50){
00983 module=2;
00984 ntheta=55-myBhaEvt->positron()->thetaIndex();
00985
00986 } else {
00987 module=1;
00988 ntheta=myBhaEvt->positron()->thetaIndex()-6;
00989 }
00990 nphi=myBhaEvt->positron()->phiIndex();
00991
00992
00993 RecEmcID shId= EmcID::crystal_id(module,ntheta,nphi);
00994 HepPoint3D SeedPos(0,0,0);
00995 SeedPos=m_iGeoSvc->GetCFrontCenter(shId);
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005 calibEnergy = myBhaEvt->
01006 getDepoMCShowerEnergy_lab(SeedPos.theta(),
01007 SeedPos.phi(),
01008 myBhaEvt->positron()->thetaIndex(),
01009 myBhaEvt->positron()->phiIndex(),
01010 m_corFun[myBhaEvt->positron()->thetaIndex()],
01011 m_beamEnergy);
01012
01013
01014
01015
01016
01017
01018
01019
01020
01021 if ( calibEnergy > 0. ) {
01022
01023 energyError = myBhaEvt->
01024 getErrorDepoMCShowerEnergy(myBhaEvt->positron()->thetaIndex(),
01025 myBhaEvt->positron()->phiIndex(),
01026 m_eSigma[myBhaEvt->positron()->thetaIndex()]);
01027
01028 } else
01029 log << MSG::WARNING << " Did not find MC deposited cluster energy "
01030 << "for this cluster: thetaInd: "
01031 << myBhaEvt->positron()->thetaIndex()
01032 << " phiInd: "
01033 << myBhaEvt->positron()->phiIndex()
01034 << endl
01035 << "Will not use this cluster for the Emc "
01036 << "Bhabha calibration !"
01037 << endreq;
01038
01039
01040
01041 myBhaEvt->setPositron()->setErrorOnCalibEnergy(energyError);
01042 myBhaEvt->setPositron()->setCalibEnergy(calibEnergy);
01043
01044
01045
01046 } else
01047 log << MSG::INFO << name()
01048 << ": Positron was not selected ! "
01049 << endreq;
01050
01051
01052
01053 fillMatrix();
01054
01055 } else {
01056 log << MSG::WARNING << " No Bhabha data for calibration found in event !"
01057 << endreq;
01058
01059 }
01060
01061 }
01062
01063
01064 void
01065 EmcSelBhaEvent::fillMatrix( ) {
01066
01067 EmcBhabha _theBhabha=EmcBhabha();
01068 EmcShower _theShower=EmcShower();
01069 EmcShDigi _DigiMax=EmcShDigi();
01070 double _sigmaBha;
01071
01072
01073
01074
01075
01076 for ( int i = 1; i <= 2; i++ ) {
01077
01078 if ( i == 2 ) _theBhabha = *(myBhaEvt->electron());
01079 else _theBhabha = *(myBhaEvt->positron());
01080
01081
01082
01083 double _sig=_theBhabha.errorOnCalibEnergy();
01084 double _calibEne=_theBhabha.calibEnergy();
01085 double _bhaEne=_theBhabha.shower().energy();
01086 int _bhaThetaIndex=_theBhabha.thetaIndex();
01087 int _bhaPhiIndex=_theBhabha.phiIndex();
01088
01090 double eraw =_bhaEne ;
01091 double phi = _theBhabha.shower().phi();
01092 double the = _theBhabha.shower().theta();
01093
01094 HepLorentzVector ptrk;
01095 ptrk.setPx(eraw*sin(the)*cos(phi));
01096 ptrk.setPy(eraw*sin(the)*sin(phi));
01097 ptrk.setPz(eraw*cos(the));
01098 ptrk.setE(eraw);
01099
01100 ptrk=ptrk.boost(-0.011,0,0);
01101 _bhaEne=ptrk.e();
01103
01104 double SigCut=0.0;
01105
01106 SigCut=m_sigmaCut;
01107
01108 int ixtalIndex = index(_bhaThetaIndex,_bhaPhiIndex);
01109 double peakCutMin,peakCutMax;
01110
01111
01112 if (m_selMethod=="Ithe"){
01113 peakCutMin= m_eDepEne[_bhaThetaIndex]*m_beamEnergy
01114 - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01115
01116 peakCutMax=m_eDepEne[_bhaThetaIndex] *m_beamEnergy
01117 + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01118 }
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138 if (m_selMethod=="Ixtal"){
01139 peakCutMin= m_eRawPeak[ixtalIndex] *m_beamEnergy
01140 - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01141
01142 peakCutMax= m_eRawPeak[ixtalIndex] *m_beamEnergy
01143 + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01144 }
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183 if ( _theBhabha.found()!=0 && _theBhabha.calibEnergy() > 0.
01184 && _bhaEne >= peakCutMin
01185 && _bhaEne <= peakCutMax
01186 && m_measure[ixtalIndex] !=3 ) {
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201 m_showersAccepted++;
01202
01203 _sigmaBha = _theBhabha.sigma2();
01204
01205
01206 _theShower = _theBhabha.shower();
01207
01208
01209
01210
01211 _DigiMax = _theShower.maxima();
01212
01213 unsigned int newThetaIndMax=999;
01214 if (_DigiMax.module()==0) newThetaIndMax = _DigiMax.thetaIndex();
01215 if (_DigiMax.module()==1) newThetaIndMax = _DigiMax.thetaIndex() + 6;
01216 if (_DigiMax.module()==2) newThetaIndMax = 55 - _DigiMax.thetaIndex();
01217
01218
01219
01220
01221
01222
01223 int maximaIndex=0;
01224 maximaIndex = index(newThetaIndMax,_DigiMax.phiIndex());
01225
01226
01227
01228
01229
01230 list<EmcShDigi> _DigiList=_theShower.digiList();
01231
01232 list<EmcShDigi>::const_iterator _Digi1,_Digi2;
01233
01234
01235
01236
01237
01238 for (_Digi1 = _DigiList.begin();
01239 _Digi1 != _DigiList.end(); _Digi1++) {
01240
01241
01242
01243
01244
01245 unsigned int newThetaInd1=999;
01246 if (_Digi1->module()==0) newThetaInd1 = _Digi1->thetaIndex();
01247 if (_Digi1->module()==1) newThetaInd1 = _Digi1->thetaIndex() + 6;
01248 if (_Digi1->module()==2) newThetaInd1 = 55 - _Digi1->thetaIndex();
01249
01250 int Digi1Index = index(newThetaInd1,_Digi1->phiIndex());
01251
01252
01253
01254
01255
01256
01257
01258
01259 double dvec = ( (static_cast<double>(_Digi1->energy())) *
01260 _theBhabha.calibEnergy() / _sigmaBha);
01261
01262
01263 if ( m_writeMVToFile )
01264 (myCalibData->vectorR(Digi1Index)) += dvec;
01265
01266
01267 if ( m_writeMVToFile )
01268
01269 (myCalibData->xtalHits(Digi1Index))++;
01270
01271
01272
01273 if ( Digi1Index == maximaIndex ) {
01274 if ( m_writeMVToFile ){
01275 (myCalibData->xtalHitsDir(Digi1Index))++;
01276
01277 }
01278 }
01279
01280
01281 for (_Digi2 = _Digi1;
01282 _Digi2 != _DigiList.end(); _Digi2++) {
01283
01284 unsigned int newThetaInd2=999;
01285 if (_Digi2->module()==0) newThetaInd2 = _Digi2->thetaIndex();
01286 if (_Digi2->module()==1) newThetaInd2 = _Digi2->thetaIndex() + 6;
01287 if (_Digi2->module()==2) newThetaInd2 = 55 - _Digi2->thetaIndex();
01288
01289 int Digi2Index = index(newThetaInd2, _Digi2->phiIndex());
01290
01291
01292 double val =
01293 static_cast<double>((( (_Digi1->energy() *
01294 _Digi2->energy() )
01295 ))/_sigmaBha);
01296
01297 if ( m_writeMVToFile )
01298 myCalibData->matrixMEle( Digi1Index, Digi2Index) += val;
01299
01300 }
01301 }
01302
01303 }
01304
01305 }
01306
01307 }
01308
01309
01310 void
01311 EmcSelBhaEvent::initGeom() {
01312
01313 MsgStream log(msgSvc(), name());
01314
01315 int numberOfXtalsRing;
01316
01317 EmcStructure* theEmcStruc=new EmcStructure() ;
01318 theEmcStruc->setEmcStruc();
01319
01320
01321 int nrOfTheRings = theEmcStruc->getNumberOfTheRings();
01322
01323 m_nXtals = theEmcStruc->getNumberOfXtals();
01324
01325
01326 for ( int the = theEmcStruc->startingTheta(); the< nrOfTheRings; the++) {
01327
01328 numberOfXtalsRing = theEmcStruc->crystalsInRing((unsigned int) the );
01329
01330 for ( int phi=0; phi < numberOfXtalsRing; phi++) {
01331
01332 m_index[the][phi] = theEmcStruc->getIndex( (unsigned int)the , (unsigned int)phi);
01333
01334 }
01335
01336 }
01337
01338 log << MSG::INFO << " Emc geometry for Bhabha calibration initialized !! "
01339 << endl
01340 << "Number of Xtals: " << m_nXtals << endreq;
01341 delete theEmcStruc;
01342
01343
01344
01345 }
01346
01347
01348 void
01349 EmcSelBhaEvent::OutputMV(){
01350
01351 MsgStream log(msgSvc(), name());
01352
01353
01354 if ( myCalibData != 0 )
01355
01356
01357 if ( m_writeMVToFile ) {
01358
01359 int count;
01360 char cnum[10];
01361 if (m_run<0){
01362 count = sprintf(cnum,"MC%d",-m_run);
01363 }
01364 if (m_run>=0){
01365 count = sprintf(cnum,"%d",m_run);
01366 }
01367
01368 std::string runnum="";
01369 runnum.append(cnum);
01370
01371 ofstream runnumberOut;
01372 std::string runnumberFile = m_fileDir;
01373 runnumberFile += m_fileExt;
01374 runnumberFile +="runnumbers.dat";
01375 runnumberOut.open(runnumberFile.c_str(),ios::out|ios::app);
01376
01377 ifstream runnumberIn;
01378 runnumberIn.open(runnumberFile.c_str());
01379 bool status=false;
01380 while( !runnumberIn.eof() ) {
01381
01382 std::string num;
01383 runnumberIn >> num;
01384 if (runnum==num) {
01385 status=true;
01386 log << MSG::INFO<< " the runnumber: "<<runnum
01387 <<" exists in the file runnumbers.dat" <<endreq;
01388 break;
01389 }else{
01390 status=false;
01391 log << MSG::INFO<< " the runnumber: "<<runnum
01392 <<" does not exist in the file runnumbers.dat" <<endreq;
01393 }
01394 }
01395 runnumberIn.close();
01396
01397 ofstream vectorOut;
01398 std::string vectorFile = m_fileDir;
01399 vectorFile += m_fileExt;
01400 vectorFile += runnum;
01401 vectorFile += "CalibVector.dat";
01402 vectorOut.open(vectorFile.c_str());
01403
01404 ofstream matrixOut;
01405 std::string matrixFile = m_fileDir;
01406 matrixFile += m_fileExt;
01407 matrixFile += runnum;
01408 matrixFile += "CalibMatrix.dat";
01409 matrixOut.open(matrixFile.c_str());
01410
01411 if ( vectorOut.good() && matrixOut.good() &&runnumberOut.good()) {
01412
01413 myCalibData->writeOut(matrixOut, vectorOut);
01414
01415 log << MSG::INFO<< " Wrote files "
01416 << (vectorFile) << " and "
01417 << (matrixFile) <<endreq;
01418
01419
01420 if ( !status ) {
01421 runnumberOut<<runnum<<"\n";
01422 log << MSG::INFO<< "Wrote files "<<runnumberFile<< endreq;
01423 }
01424
01425 } else
01426 log << MSG::WARNING << " Could not open vector and/or matrix file !"
01427 << endl
01428 << "matrix file : " << matrixFile << endl
01429 << "vector file : " << vectorFile
01430 << endreq;
01431
01432 vectorOut.close();
01433 matrixOut.close();
01434 runnumberOut.close();
01435 }
01436
01437 }
01438
01439
01440 double
01441 EmcSelBhaEvent::findPhiDiff( double phi1, double phi2)
01442 {
01443 double diff;
01444 diff = phi1 - phi2;
01445
01446 while( diff> pi ) diff -= twopi;
01447 while( diff< -pi ) diff += twopi;
01448
01449 return diff;
01450 }
01451
01452
01453 void
01454 EmcSelBhaEvent::readCorFun(){
01455
01456 ifstream corFunIn;
01457 std::string corFunFile = m_inputFileDir;
01458 corFunFile += m_fileExt;
01459 corFunFile += "cor.conf";
01460 corFunIn.open(corFunFile.c_str());
01461 for(int i=0;i<56;i++) {
01462 corFunIn>>m_corFun[i];
01463
01464 cout<<"energy corFun="<<m_corFun[i]<<endl;
01465 }
01466 corFunIn.close();
01467 }
01468
01469 void
01470 EmcSelBhaEvent::readEsigma(){
01471
01472 ifstream EsigmaIn;
01473 std::string EsigmaFile = m_inputFileDir;
01474 EsigmaFile += m_fileExt;
01475 EsigmaFile += "sigma.conf";
01476 EsigmaIn.open(EsigmaFile.c_str());
01477 for(int i=0;i<56;i++) {
01478 EsigmaIn>>m_eSigma[i];
01479 cout<<"Sigma="<<m_eSigma[i]<<endl;
01480 }
01481 EsigmaIn.close();
01482 }
01483
01484 void
01485 EmcSelBhaEvent::readDepEneFun(){
01486
01487 ifstream EDepEneIn;
01488 std::string EDepEneFile = m_inputFileDir;
01489 EDepEneFile += m_fileExt;
01490 EDepEneFile += "peakI.conf";
01491 EDepEneIn.open(EDepEneFile.c_str());
01492 for(int i=0;i<56;i++) {
01493 EDepEneIn>>m_eDepEne[i];
01494
01495
01496 cout<<"DepEne="<<m_eDepEne[i]<<endl;
01497 }
01498 EDepEneIn.close();
01499
01500 }
01501
01502 void
01503 EmcSelBhaEvent::readSigmaExp(){
01504 ifstream ESigmaExpIn;
01505 std::string ESigmaExpFile = m_inputFileDir;
01506 ESigmaExpFile += m_fileExt;
01507 ESigmaExpFile += "sigmaI.conf";
01508 ESigmaExpIn.open(ESigmaExpFile.c_str());
01509 for(int i=0;i<56;i++) {
01510 ESigmaExpIn>>m_eSigmaExp[i];
01511 cout<<"SigmaExp="<<m_eSigmaExp[i]<<endl;
01512 }
01513 ESigmaExpIn.close();
01514
01515
01516 }
01517
01518 void
01519 EmcSelBhaEvent::readRawPeakIxtal(){
01520
01521
01522 ifstream EIn;
01523 std::string EFile = m_inputFileDir;
01524 EFile += m_fileExt;
01525 EFile += "findpeak.conf";
01526 EIn.open(EFile.c_str());
01527
01528 double Peak[6240];
01529 int ixtal;
01530 for(int i=0;i<6240;i++) {
01531 EIn>>ixtal>>Peak[i];
01532 m_eRawPeak[ixtal]=Peak[i];
01533 }
01534 EIn.close();
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551 }
01552
01553
01554
01555 double
01556 EmcSelBhaEvent::Angle2ClosestShower( int ShowerID ) {
01557
01558 double minDist=999;
01559
01560 SmartDataPtr<EvtRecEvent> evtRecEvent(eventSvc(), EventModel::EvtRec::EvtRecEvent);
01561 SmartDataPtr<EvtRecTrackCol> evtRecTrkCol(eventSvc(), EventModel::EvtRec::EvtRecTrackCol);
01562
01563 EvtRecTrackIterator itTrk = evtRecTrkCol->begin() + ShowerID;
01564
01565 RecEmcShower *theShower = (*itTrk)->emcShower();
01566 HepLorentzVector theShowerVec(1,1,1,1);
01567 theShowerVec.setTheta(theShower->theta());
01568 theShowerVec.setPhi(theShower->phi());
01569 theShowerVec.setRho(theShower->energy());
01570 theShowerVec.setE(theShower->energy());
01571
01572 for(int j = 0; j < evtRecEvent->totalTracks(); j++){
01573 EvtRecTrackIterator jtTrk=evtRecTrkCol->begin() + j;
01574
01575 if(!(*jtTrk)->isEmcShowerValid()) continue;
01576 if (ShowerID == (*jtTrk)->trackId()) continue;
01577
01578 RecEmcShower *aShower = (*jtTrk)->emcShower();
01579
01580 if (aShower->energy() > m_minAngShEnergyCut ){
01581
01582 HepLorentzVector aShowerVec(1,1,1,1);
01583 aShowerVec.setTheta(aShower->theta());
01584 aShowerVec.setPhi(aShower->phi());
01585 aShowerVec.setRho(aShower->energy());
01586 aShowerVec.setE(aShower->energy());
01587
01588 double dist = theShowerVec.angle(aShowerVec);
01589
01590 if ( dist < minDist )
01591 minDist = dist;
01592
01593 }
01594
01595 }
01596
01597 return minDist;
01598 }
01599