/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Emc/EmcCalib/EmcBhaCalib/EmcBhaCalib-00-00-34/src/EmcSelBhaEvent.cxx

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //
00004 // Description:
00005 //      Class EmcSelBhaEvent - Select Bhabha events (MCdata) for Emc-digi 
00006 //      Calibration; read rec-data, read MDC & Emc information from tracklist 
00007 //      and select Bhabha event
00008 //
00009 // Environment:
00010 //      Software developed for the BESIII Detector at the BEPCII.
00011 //
00012 // Author List:
00013 //      Chunxiu Liu
00014 //
00015 // Copyright Information:
00016 //      Copyright (C) 2008               IHEP
00017 //
00018 //------------------------------------------------------------------------
00019 
00020 
00021 //-----------------------
00022 
00023 
00024 //-------------
00025 // C Headers --
00026 //-------------
00027 extern "C" {
00028 
00029 }
00030 #include <iostream>
00031 #include <fstream>
00032 #include <cmath>
00033 #include <cassert>
00034 #include <cstdlib>
00035 //-------------------------------
00036 // Collaborating Class Headers --
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 // Constructors --
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   // Declare the properties  
00124 
00125 
00126   declareProperty ("Vr0cut", m_vr0cut);   // suggest cut: |z0|<5cm && r0<1cm
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 // Destructor --
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   //number of events with   TwoProngMatched
00222   m_TwoProngMatched=0;
00223   //number of events with   TwoProngOneMatched
00224   m_TwoProngOneMatched=0;   
00225 
00226   //--------------------------------------->>>>>>>>>>>
00227   //initialize Emc geometry to convert between index <=> theta,phi
00228   initGeom();
00229  
00230   //create the object that holds the calibration data
00231   if ( m_writeMVToFile )
00232     myCalibData = new EmcBhaCalibData(m_nXtals,m_MsgFlag);
00233   else
00234     myCalibData = 0;
00235   
00236   //get EmcRecGeoSvc
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   // use EmcCalibConstSvc
00246   StatusCode scCalib; 
00247   scCalib = Gaudi::svcLocator() -> service("EmcCalibConstSvc", m_emcCalibConstSvc);
00248   if( scCalib != StatusCode::SUCCESS){
00249     log << MSG::ERROR << "can not use EmcCalibConstSvc" << endreq;
00250   } 
00251   else {
00252     std::cout << "Test EmcCalibConstSvc   DigiCalibConst(0)=  " 
00253               << m_emcCalibConstSvc -> getDigiCalibConst(0) << std::endl;
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   // read correction function from the file of 'cor.conf'
00267   //from MC Bhabha data,
00268   // expected depostion energy for bhabha calibration at cms. system
00269   //it is as a function of thetaID(0-55) 
00270   readCorFun();
00271  // read Esigma(Itheta) 
00272   //from MC Bhabha data,
00273   //it is as a function of thetaID(0-55)  from the file of 'sigma.conf'
00274   readEsigma();
00275 
00276   //read peak of bhabha rawdata before bhabha calibration, 
00277   //it is as a function of thetaID(0-55) from the file of "peakI.conf"
00278   readDepEneFun();
00279 
00280   //read sigma of bhabha rawdata before bhabha calibration, 
00281   //it is as a function of thetaID(0-55) from the file of "sigmaI.conf"
00282   readSigmaExp();
00283   readRawPeakIxtal();
00284 
00285   /*
00286   ofstream out;
00287   out.open("expectedE.conf");
00288   for(int i=0; i<6240;i++){
00289     out<<i<<"\t"<<expectedEnergy(i)<<endl;
00290   }
00291   out.close();
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   //create the object that store the needed data of the Bhabha event  
00303  
00304   int event, run;
00305   
00306   SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
00307 
00308   run=eventHeader->runNumber();
00309   event=eventHeader->eventNumber();
00310   //cout<<"---------"<<event<<".........."<<run<<endl;
00311   m_events++;
00312   m_run = run;
00313 
00314 
00316   // get beam energy
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    // if(m_readDb.isRunValid(m_irun))   
00324    //  m_beamEnergy=m_readDb.getbeamE(m_irun);
00325    //if(m_BeamEnergySvc->isRunValid())   
00326      //  m_beamEnergy=m_BeamEnergySvc->getbeamE();
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   // int mmea=0;
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         //cout<<id<<endl;
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          //if ((*iter3)->getMeasure()==3) mmea=9; 
00377         
00378       }
00379     }
00380 
00382   
00383   myBhaEvt = new EmcBhabhaEvent();
00384 
00385   //Select Bhabha
00386   SelectBhabha();
00387   if(m_selectedType == BhabhaType::OneProng) m_OneProng++;
00388   //number of events with   TwoProngMatched
00389   if(m_selectedType == BhabhaType::TwoProngMatched) m_TwoProngMatched++;
00390   //number of events with   TwoProngOneMatched
00391   if(m_selectedType == BhabhaType::TwoProngOneMatched) m_TwoProngOneMatched++;
00392   
00393   if(m_selectedType == BhabhaType::Nothing){
00394     m_Nothing++;
00395   }
00396 
00397   //retreive shower list of event
00398 
00399   if (m_selectedType == BhabhaType::TwoProngMatched) {
00400      FillBhabha();
00401   
00402      //collect bhabha event for digi-calibration of EMC 
00403      //and fill matrix and vector of system of linear equations
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   //output the data of Matrix and vector to files
00421   OutputMV();
00422   /* 
00423   for (int i=1000; i < 1500; i++) {
00424         int xtalIndex=myCalibData->xtalInd(i);
00425 
00426         int nhitxtal=myCalibData->xtalHitsDir(xtalIndex);
00427         cout<<"ixtal, Nhitdir="<< xtalIndex<<"   "<<nhitxtal<<endl;
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   Vint iGood;
00506   iGood.clear();
00507   int nCharge = 0;
00508   int numberOfTrack = 0;
00509 
00510   //the accumulated event information
00511   double totalEnergy = 0.; 
00512   int numberOfShowers = 0; 
00513   int numberOfShowersWithTrack = 0;
00514   bool secondUnMTrackFound=false;
00515   float eNorm = 0.;
00516   float pNorm = 0.;
00517   float acolli_min = 999.;
00518   double minAngFirstSh = 999., minAngSecondSh = 999.;
00519   float theFirstTracksTheta = 0;
00520   float theFirstTracksMomentum = 0;
00521 
00522   //the selection candidates
00523   RecMdcTrack *theFirstTrack = 0;
00524   RecMdcTrack *theSecondTrack = 0;
00525   RecMdcTrack *theKeptTrack = 0;
00526   RecEmcShower *theFirstShower = 0; 
00527 
00528   RecEmcShower *theSecondShower = 0;
00529   RecEmcShower *theKeptShower = 0;
00530   double keptTheta = 0.;
00531   int theFirstShID = 9999;
00532   int theSecondShID = 9999;
00533   int theKeptShID = 9999;
00534   int theTrkID = 9999;
00535   */
00536 
00537    Vint iGam;
00538   iGam.clear(); 
00539 
00540   //double ene5x5,theta,phi,eseed;
00541   //double showerX,showerY,showerZ;
00542   //long int thetaIndex,phiIndex;
00543   //HepPoint3D showerPosition(0,0,0);
00544   // RecEmcID showerId;
00545   //unsigned int npart;
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       //cout<<"Shower5x5="<<Shower5x5<<endl;
00560       /*
00561       ene5x5=theShower->e5x5();   //uncorrected 5x5 energy unit GeV
00562       eseed=theShower->eSeed(); //unit GeV
00563       
00564       showerPosition = theShower->position();
00565       theta = theShower->theta();
00566       phi= theShower->phi();
00567       showerX=theShower->x();
00568       showerY=theShower->y();
00569       showerZ=theShower->z();
00570       
00571       showerId = theShower->getShowerId();
00572  
00573       npart = EmcID::barrel_ec(showerId);
00574       
00575       //The theta number is defined by Endcap_east(0-5),Barrel(0-43),Endcap_west(0-5) 
00576       //module is defined  by Endcap_east(0),Barrel(1),Endcap_west(2)      
00577       
00578       thetaIndex=EmcID::theta_module(showerId);
00579       
00580       phiIndex=EmcID::phi_module(showerId);  
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   //RecMdcTrack *theMdcTrk1 = (*itTrk1)->mdcTrack();
00623 
00624   EvtRecTrackIterator  itTrk2 = evtRecTrkCol->begin() + m_selectedTrkID2;
00625 
00626   RecEmcShower *theShower2 = (*itTrk2)->emcShower();
00627   //RecMdcTrack *theMdcTrk2 = (*itTrk2)->mdcTrack();
00628   EvtRecTrackIterator  itTrk;
00629   RecEmcShower *theShower;
00630 
00631 
00632   // * * * * * * * * * * * * * * * * * * * * * * * * * ** *  ** *
00633   //loop all showers of an event set EmcShower and EmcShDigi
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     //ene=theShower->energy(); //corrected shower energy unit GeV
00659     ene=theShower->e5x5();   //uncorrected 5x5 energy unit GeV
00660     eseed=theShower->eSeed(); //unit GeV
00661  
00662 
00664     /*
00665    RecExtTrack *extTrk = (*itTrk)->extTrack();
00666 
00667       Hep3Vector extpos = extTrk->emcPosition();        
00668  
00669       cout<<"Extpos="<<extpos<<endl;
00670 
00671 
00672       RecEmcShower *theShower = (*itTrk)->emcShower();
00673 
00674       Hep3Vector emcpos(theShower->x(), theShower->y(), theShower->z());
00675 
00676       cout<<"emcpos="<<emcpos<<endl;
00677     
00678       RecEmcID shId= theShower->getShowerId();
00679       unsigned int nprt= EmcID::barrel_ec(shId);
00680       //RecEmcID cellId= theShower->cellId();
00681       HepPoint3D SeedPos(0,0,0);
00682       SeedPos=m_iGeoSvc->GetCFrontCenter(shId);
00683       cout<<"SeedPos="<<SeedPos<<endl;
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     //The theta number is defined by Endcap_east(0-5),Barrel(0-43),Endcap_west(0-5) 
00698     //module is defined  by Endcap_east(0),Barrel(1),Endcap_west(2)      
00699 
00700     thetaIndex=EmcID::theta_module(showerId);
00701     
00702     phiIndex=EmcID::phi_module(showerId);    
00703 
00704     //cout<<thetaIndex<<"   "<<phiIndex<<endl;
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();  //digit energy unit GeV
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       //The theta number is defined by Endcap_east(0-5),Barrel(0-43),Endcap_west(0-5) 
00741       //module is defined  by Endcap_east(0),Barrel(1),Endcap_west(2) 
00742 
00743       digiThetaIndex=EmcID::theta_module(cellId);
00744       
00745       digiPhiIndex = EmcID::phi_module(cellId);
00746 
00747       //set EmcShDigi
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();          //sort energy from small to large
00765          
00766     numberOfDigis = shDigiList.size();
00767     
00768     maxima = *(--shDigiList.end());
00769     //cout<<"maxima="<<maxima.energy()<<endl;
00770     //cout<<maxima.thetaIndex()<<"  max  "<<maxima.phiIndex()<<endl;
00771     //set EmcShower
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   //m_showerList.sort();         //sort energy from small to large
00790   
00791   delete aShower;
00792 
00794 
00795   if (m_showerList.size() == 2) {
00796     //iterator for the bump list
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       //fill the Bhabha event
00809       // if ( (itrk==1&&theMdcTrk1->charge()>0)
00810       //   ||(itrk==2&&theMdcTrk2->charge()>0) ){
00811       if (itrk==1){
00812         //set properties 
00813         myBhaEvt->setPositron()->setShower(shower);
00814         myBhaEvt->setPositron()->setTheta(shower.theta());
00815         myBhaEvt->setPositron()->setPhi(shower.phi());
00816         unsigned int newthetaInd ;
00817         //The thetaIndex is defined by Endcap_east(0-5),Barrel(6-49),Endcap_west(50-55)
00818         //in Emc Bhabha Calibration
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         //if ( (itrk==1&&theMdcTrk1->charge()<0)
00840         //  ||(itrk==2&&theMdcTrk2->charge()<0) ){
00841     if (itrk==2){
00842         //set properties including vertex corrected theta 
00843       myBhaEvt->setElectron()->setShower(shower);
00844         myBhaEvt->setElectron()->setTheta(shower.theta());
00845         myBhaEvt->setElectron()->setPhi(shower.phi());    
00846         unsigned int newthetaInd;       
00847         //The thetaIndex is defined by Endcap_east(0-5),Barrel(6-49),Endcap_west(50-55)
00848       //in Emc Bhabha Calibration
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   //check if the Bhabhas were found
00877   if ( 0 != myBhaEvt->positron()->found() ||
00878        0 != myBhaEvt->electron()->found() ) {
00879     
00880     m_taken++;
00881     //fill the EmcBhabhas 
00882     double calibEnergy=0.; 
00883     double energyError=0.;
00884     
00885     //------------- electron found --------------------------------------
00886     if (myBhaEvt->electron()->found() ) {
00887       /*
00888       int ixtalIndex = index(myBhaEvt->electron()->thetaIndex(),
00889                             myBhaEvt->electron()->phiIndex());
00890       calibEnergy = m_eMcPeak[ixtalIndex];
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       calibEnergy = myBhaEvt->
00914         getDepoMCShowerEnergy_lab(myBhaEvt->electron()->theta(),
00915                                   myBhaEvt->electron()->phi(),
00916                                   myBhaEvt->electron()->thetaIndex(),
00917                                   myBhaEvt->electron()->phiIndex(),
00918                                   m_corFun[myBhaEvt->electron()->thetaIndex()],
00919                                   m_beamEnergy); 
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      calibEnergy = myBhaEvt->
00931         getDepoMCShowerEnergy(myBhaEvt->electron()->thetaIndex(),
00932                               myBhaEvt->electron()->phiIndex(),
00933                               m_corFun[myBhaEvt->electron()->thetaIndex()],
00934                               m_beamEnergy); 
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       //set all that stuff in an EmcBhabha
00956       myBhaEvt->setElectron()->setErrorOnCalibEnergy(energyError);
00957       myBhaEvt->setElectron()->setCalibEnergy(calibEnergy);
00958     
00959       //myBhaEvt->electron()->print();
00960 
00961     } else
00962       log << MSG::INFO<< name()
00963           << ": Electron was not selected ! "
00964           << endreq;
00965     
00966     calibEnergy=0.; 
00967     energyError=0.;
00968     
00969     //---------------- positron found ----------------------------------
00970     if (myBhaEvt->positron()->found() ) {
00971       /*
00972       int ixtalIndex = index(myBhaEvt->positron()->thetaIndex(),
00973                             myBhaEvt->positron()->phiIndex());
00974       calibEnergy = m_eMcPeak[ixtalIndex];
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      calibEnergy = myBhaEvt->
00998         getDepoMCShowerEnergy_lab(myBhaEvt->positron()->theta(),
00999                                   myBhaEvt->positron()->phi(),
01000                                   myBhaEvt->positron()->thetaIndex(),
01001                                   myBhaEvt->positron()->phiIndex(),
01002                                   m_corFun[myBhaEvt->positron()->thetaIndex()],
01003                                   m_beamEnergy); 
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       calibEnergy = myBhaEvt->
01015         getDepoMCShowerEnergy(myBhaEvt->positron()->thetaIndex(),
01016                               myBhaEvt->positron()->phiIndex(),
01017                               m_corFun[myBhaEvt->positron()->thetaIndex()],
01018                               m_beamEnergy); 
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       //set all that stuff in an EmcBhabha
01041       myBhaEvt->setPositron()->setErrorOnCalibEnergy(energyError);
01042       myBhaEvt->setPositron()->setCalibEnergy(calibEnergy);
01043   
01044       //myBhaEvt->positron()->print();
01045       
01046     } else
01047       log << MSG::INFO << name()
01048           << ": Positron was not selected ! "
01049           << endreq;
01050     
01051     //calculate elements of Matrix M and vector R from Bhabha event, 
01052     //M is in SLAP triad format
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   // ---- get the current calibration constants
01073   
01074   
01075   // ---- loop over the two particles
01076   for ( int i = 1; i <= 2; i++ ) { 
01077     
01078     if ( i == 2 ) _theBhabha = *(myBhaEvt->electron()); 
01079     else _theBhabha = *(myBhaEvt->positron());  
01080     //-----------------------------------------------------------
01081     // ---- fill the matrix only if we found the particle and ---
01082     // ---- a energy to calibrate on -----
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     //cout<<_sig<<"  "<< _calibEne<<"  "<<_bhaEne<<"  "<<endl;
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     //peak+/- 1 sigma
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      if (ixtalIndex==2408){
01121       peakCutMin= 1.685 
01122         - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01123 
01124       peakCutMax=1.685 
01125         + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01126     }
01127     */
01128 
01129     //Raw mean+/- 1 RMS
01130     // peakCutMin= m_eRawMean[ixtalIndex] - m_eRawRMS[ixtalIndex]*SigCut;
01131 
01132     //peakCutMax= m_eRawMean[ixtalIndex] + m_eRawRMS[ixtalIndex]*SigCut;
01133 
01134     // cout<< ixtalIndex<<"  "<<peakCutMin<<"   " <<peakCutMax<<endl;
01135 
01136       
01137     //peak(ixtal)+/- 1 sigma
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 //  if (ixtalIndex==689) cout<<peakCutMin<<"\t"<<peakCutMax<<"\t"<<_bhaEne<<endl;
01147   /* 
01148     if (ixtalIndex==2408){
01149       peakCutMin= 1.685 
01150         - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01151       
01152       peakCutMax=1.685 
01153         + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;   
01154     }
01155     */
01156     /*
01157     
01158     if (_bhaThetaIndex==26&&_bhaPhiIndex==26){
01159       peakCutMin= 1.75 
01160         - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01161 
01162       peakCutMax=1.75 
01163         + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01164     }
01165     
01166     if (_bhaThetaIndex==29&&_bhaPhiIndex==26){
01167       peakCutMin= 1.9
01168         - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01169 
01170       peakCutMax=1.9
01171         + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01172     }
01173 
01174     if (_bhaThetaIndex==30&&_bhaPhiIndex==26){
01175       peakCutMin= 1.655
01176         - SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
01177 
01178       peakCutMax=1.655 
01179         + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.;
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     //if ( _theBhabha.found()!=0 && _theBhabha.calibEnergy() > 0. 
01190     //    && _bhaEne >= m_eDepMean[ixtalIndex] 
01191     //  -  SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.
01192     //  &&_bhaEne <= m_eDepMean[ixtalIndex]
01193     //  + SigCut*m_eSigmaExp[_bhaThetaIndex]*m_beamEnergy/100.) {
01194 
01195     // if ( _theBhabha.found()!=0 && _theBhabha.calibEnergy() > 0. 
01196     //    && _bhaEne >= m_eDepPeak[ixtalIndex] 
01197     //  -  SigCut*m_eDepSigma[ixtalIndex]
01198     // &&_bhaEne <= m_eDepPeak[ixtalIndex]
01199     //+ SigCut*m_eDepSigma[ixtalIndex]) {
01200 
01201        m_showersAccepted++;
01202       //the error (measurement + error on the energy to calibrate on)
01203       _sigmaBha = _theBhabha.sigma2();
01204       //cout<<"sigma2="<<_sigmaBha<<endl;
01205       
01206       _theShower = _theBhabha.shower();
01207 
01208       //The thetaIndex is defined by Endcap_east(0-5),Barrel(6-49),Endcap_west(50-55)
01209       //in Emc Bhabha Calibration
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       //     if(_DigiMax.module()==1)
01219       //        {  // only for MC data
01220       //  cout<<"theta..,phi="<<_DigiMax.thetaIndex()<<"   "<<_DigiMax.phiIndex()<<endl;
01221       //}
01222 
01223       int maximaIndex=0;
01224       maximaIndex = index(newThetaIndMax,_DigiMax.phiIndex());
01225       //if (maximaIndex>500&&maximaIndex<5000){
01226       //        cout<<"max="<<maximaIndex<<"   "<<_DigiMax.thetaIndex()
01227       //<<"   "<<_DigiMax.phiIndex()<<endl;
01228       //}
01229 
01230       list<EmcShDigi> _DigiList=_theShower.digiList();
01231       
01232       list<EmcShDigi>::const_iterator _Digi1,_Digi2;
01233       
01234       //------------------------------------------------------
01235       //---- double loop over the digis to fill the matrix ---
01236       
01237       //first loop over Digis 
01238       for (_Digi1 = _DigiList.begin(); 
01239            _Digi1 != _DigiList.end();  _Digi1++) {
01240         
01241         //get Xtal index 
01242         
01243         //The thetaIndex is defined by Endcap_east(0-5),Barrel(6-49),Endcap_west(50-55)
01244         //in Emc Bhabha Calibration
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         //if (Digi1Index>1000&&Digi1Index<5000){
01253         // cout<<"max="<<Digi1Index<<"   "<<_Digi1->thetaIndex()
01254         //     <<"   "<<_Digi1->phiIndex()<<endl;
01255         //}
01256 
01257 
01258         //calculate the vector with Raw data
01259         double dvec = ( (static_cast<double>(_Digi1->energy())) *
01260                         _theBhabha.calibEnergy() / _sigmaBha);
01261 
01262         //fill the vector 
01263         if ( m_writeMVToFile )
01264           (myCalibData->vectorR(Digi1Index)) += dvec;
01265         
01266         //count hits in Xtals and keep theta and phi
01267         if ( m_writeMVToFile )
01268 
01269           (myCalibData->xtalHits(Digi1Index))++;
01270         
01271         //if ( *(_theShower.maxima()) == *(_Digi1) ) {
01272 
01273         if ( Digi1Index == maximaIndex ) {
01274           if ( m_writeMVToFile ){
01275              (myCalibData->xtalHitsDir(Digi1Index))++;
01276 
01277             }
01278         }
01279         
01280         //second loop over Digis 
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           //calculate the matrix element with raw data
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     } //if paricle found and calibration energy
01304     
01305   }//loop over particles
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   //number Of Theta Rings
01321   int nrOfTheRings = theEmcStruc->getNumberOfTheRings();
01322   
01323   m_nXtals = theEmcStruc->getNumberOfXtals();  
01324 
01325   //init geometry
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   //check this first because I sometimes got two endJob transitions
01354   if ( myCalibData != 0 )
01355 
01356     //if set write the matrix and vector to a file
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;  //rad
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     //m_eDepEne[i]=m_eDepEne[i]-0.020;
01495     //m_eDepEne[i]=m_eDepEne[i]/1.843*m_beamEnergy;
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   ifstream EIn1;
01538   std::string EFile1 = m_inputFileDir;
01539   EFile1 += m_fileExt;
01540   EFile1 += "findpeak-mc.conf";
01541   EIn1.open(EFile1.c_str());
01542 
01543   double Peak1[6240];
01544   int ixtal1;
01545   for(int i=0;i<6240;i++) {
01546     EIn1>>ixtal1>>Peak1[i];
01547   m_eMcPeak[ixtal1]=Peak1[i];
01548   }
01549   EIn1.close();
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 

Generated on Tue Nov 29 22:58:16 2016 for BOSS_7.0.2 by  doxygen 1.4.7