2#include "GaudiKernel/Kernel.h"
3#include "GaudiKernel/IInterface.h"
4#include "GaudiKernel/StatusCode.h"
5#include "GaudiKernel/SvcFactory.h"
6#include "GaudiKernel/IJobOptionsSvc.h"
7#include "GaudiKernel/MsgStream.h"
8#include "GaudiKernel/PropertyMgr.h"
9#include "GaudiKernel/IIncidentSvc.h"
10#include "GaudiKernel/Incident.h"
11#include "GaudiKernel/IIncidentListener.h"
12#include "GaudiKernel/INTupleSvc.h"
13#include "GaudiKernel/NTuple.h"
15#include "GaudiKernel/ISvcLocator.h"
16#include "GaudiKernel/Bootstrap.h"
18#include "GaudiKernel/IDataProviderSvc.h"
19#include "GaudiKernel/SmartDataPtr.h"
20#include "GaudiKernel/DataSvc.h"
26#include "GaudiKernel/SmartDataPtr.h"
40 declareProperty(
"IonizationModel", m_ionModel=1);
41 declareProperty(
"DriftAvalancheModel", m_driftAvaModel=1);
42 declareProperty(
"InductionModel", m_inductionModel=1);
44 declareProperty(
"GarfieldDebugging", m_garfDebugging=
false);
45 declareProperty(
"SamplingDebugging", m_samplingDebugging=
false);
46 declareProperty(
"InductionDebugging", m_debugInduction=
false);
47 declareProperty(
"MagConfig", m_magConfig=1);
48 declareProperty(
"LUTFilePath",m_LUTFilePath =
"/bes3fs/cgemCosmic/data/CGEM_cosmic_look_up_table_17.root");
49 declareProperty(
"QBranchVsampleDelay",sample_delay = 162.5);
50 declareProperty(
"StoreUnderThrethold",m_storeFlag =
false);
51 declareProperty(
"Saturation",m_saturation =
true);
52 declareProperty(
"SaveNt",m_saveNt=
false);
53 declareProperty(
"SamplingGarGainMultiplier",m_GainMultiplier);
54 declareProperty(
"SamplingGarTransMultiplier",m_TransMultiplier= 1.0);
55 declareProperty(
"SamplingGarDiffuMultiplier",m_DiffuMultiplier= 1.0);
57 declareProperty(
"Ngaps_microSector",m_Ngaps_microSector= 40);
58 declareProperty(
"gapShift_microSector",m_gapShift_microSector);
59 declareProperty(
"gap_microSector", m_gap_microSector= 1.1);
60 declareProperty(
"microSector_width", m_microSector_width);
61 declareProperty(
"QinGausSigma", m_QinGausSigma);
62 declareProperty(
"ScaleSignalX", m_ScaleSignalX=1.0);
69 if( IID_ICgemDigitizerSvc.versionMatch(riid) ){
72 return Service::queryInterface(riid, ppvInterface);
74 return StatusCode::SUCCESS;
78 MsgStream log(messageService(), name());
79 log << MSG::INFO <<
"CgemDigitizerSvc::initialize()" << endreq;
81 StatusCode sc = Service::initialize();
82 if( sc.isFailure() )
return sc;
84 static IJobOptionsSvc* jobSvc = 0;
86 sc = service(
"JobOptionsSvc", jobSvc);
87 if ( sc.isFailure() ) {
88 std::cout <<
"Can't get the JobOptionsSvc @ DistBoss::GetPropertyValue()" << std::endl;
94 const vector<const Property*>* properties = jobSvc->getProperties(
"BesRndmGenSvc");
96 if ( properties == NULL ) {
97 std::cout <<
"In CgemDigitizerSvc::initialize(), can't get client: " << std::endl;
98 return StatusCode::FAILURE;
101 unsigned int randSeed;
102 for (
unsigned int i = 0; i < properties->size(); ++i ) {
103 if ( properties->at(i)->name() ==
"RndmSeed" ) {
104 cout <<
"name of property: " << properties->at(i)->name() << endl;
105 string strRnd = properties->at(i)->toString();
106 sscanf(strRnd.c_str(),
"%u", &randSeed);
107 cout <<
"random seed from jobOption: " << randSeed << endl;
112 sc = service(
"CgemGeomSvc", m_geomSvc);
113 if(sc != StatusCode::SUCCESS)
115 log << MSG::ERROR <<
"can not use CgemGeomSvc" << endreq;
116 return StatusCode::FAILURE;
119 gRandom->SetSeed(randSeed);
121 cout <<
"ionModel = " << m_ionModel << endl;
124 }
else if(2 == m_ionModel){
129 m_pIon->
init(randSeed, m_geomSvc, m_magConfig);
131 if ((3==m_driftAvaModel && 3!=m_inductionModel )||(3!=m_driftAvaModel && 3==m_inductionModel )) {
132 cout<<
"CgemDigitizerSvc::initialize() Error: DriftAvalancheModel type3 and InductionModel type3 must be used together!"<<endl;
133 return StatusCode::FAILURE;
136 if(1 == m_driftAvaModel){
142 }
else if(2 == m_driftAvaModel){
144 }
else if(3 == m_driftAvaModel){
160 m_pDriftAndAva->
init(m_geomSvc, m_magConfig);
163 if(1 == m_inductionModel){
171 m_pInduction = indGar;
177 }
else if(2 == m_inductionModel){
180 }
else if (3 == m_inductionModel){
191 m_pInduction->
init(m_geomSvc, m_magConfig);
195 cout<<
"CgemDigitizerSvc:"<<endl;
196 cout<<
"gapShift_microSector="<<m_gapShift_microSector[0]
197 <<
", "<<m_gapShift_microSector[1]
198 <<
", "<<m_gapShift_microSector[2]
199 <<
", "<<m_gapShift_microSector[3]
200 <<
", "<<m_gapShift_microSector[4]
201 <<
", "<<m_gapShift_microSector[5]
203 cout<<
"microSector_width="<<m_microSector_width[0]
204 <<
", "<<m_microSector_width[1]
205 <<
", "<<m_microSector_width[2]
206 <<
", "<<m_microSector_width[3]
207 <<
", "<<m_microSector_width[4]
208 <<
", "<<m_microSector_width[5]
210 cout<<
"QinGausSigma="<<m_QinGausSigma[0]
211 <<
", "<<m_QinGausSigma[1]
213 cout<<
"Ngaps_microSector="<<m_Ngaps_microSector<<endl;
214 cout<<
"gap_microSector="<<m_gap_microSector<<endl;
217 TFile *LUTFile = TFile::Open(m_LUTFilePath.c_str(),
"read");
218 TTree *tree = (TTree*)LUTFile->Get(
"tree");
220 Int_t layer,sheet,strip_v,strip_x;
221 tree->SetBranchAddress(
"strip_x_boss", &strip_x);
222 tree->SetBranchAddress(
"strip_v_boss", &strip_v);
223 tree->SetBranchAddress(
"layer", &layer);
224 tree->SetBranchAddress(
"sheet", &sheet);
225 tree->SetBranchAddress(
"calib_QDC_saturation", &QDC_saturation);
226 for(
int i=0;i<tree->GetEntries();i++) {
229 Qsaturation[layer][sheet][0][strip_x] = QDC_saturation;
232 Qsaturation[layer][sheet][1][strip_v] = QDC_saturation;
235 for(
int i=0;i<832;i++) {
236 Qsaturation[2][0][0][i] = 45.;
237 Qsaturation[2][1][0][i] = 45.;
239 for(
int i=0;i<1395;i++) {
240 Qsaturation[2][0][1][i] = 45.;
241 Qsaturation[2][1][1][i] = 45.;
248 Gaudi::svcLocator() -> service(
"NTupleSvc",
ntupleSvc);
249 NTuplePtr nt(
ntupleSvc,
"CgemDigitizerSvcNT/digi");
253 m_tuple =
ntupleSvc->book(
"CgemDigitizerSvcNT/digi", CLID_ColumnWiseTuple,
"noise");
255 m_tuple->addItem(
"nIonE", m_ntNIonE);
256 m_tuple->addItem(
"nMultiE", m_ntNMultiE);
257 m_tuple->addItem(
"nXstrips", m_ntNxstrips,0,100);
258 m_tuple->addItem(
"nVstrips", m_ntNvstrips,0,100);
259 m_tuple->addIndexedItem(
"XstripQ", m_ntNxstrips,m_ntxstripQ);
260 m_tuple->addIndexedItem(
"XstripT", m_ntNxstrips,m_ntxstripT);
261 m_tuple->addIndexedItem(
"VstripQ", m_ntNvstrips,m_ntvstripQ);
262 m_tuple->addIndexedItem(
"VstripT", m_ntNvstrips,m_ntvstripT);
263 m_tuple->addIndexedItem(
"XstripID", m_ntNxstrips,m_XstripID);
264 m_tuple->addIndexedItem(
"VstripID", m_ntNvstrips,m_VstripID);
269 return StatusCode::SUCCESS;
273 MsgStream log(messageService(), name());
274 log << MSG::INFO <<
"CgemDigitizerSvc::finalize()" << endreq;
276 delete m_pDriftAndAva;
279 return StatusCode::SUCCESS;
283 vector<int> Vparticle (1,particle);
284 vector<int> Vcharge (1,charge);
285 vector<double> Vp (1,p);
289 vector<double> posin;
290 posin.push_back(trkPosIn[0]);
291 posin.push_back(trkPosIn[1]);
292 posin.push_back(trkPosIn[2]);
293 vector<vector<double> > VtrkPosIn;
294 VtrkPosIn.push_back(posin);
296 vector<double> posout;
297 posout.push_back(trkPosOut[0]);
298 posout.push_back(trkPosOut[1]);
299 posout.push_back(trkPosOut[2]);
300 vector<vector<double> > VtrkPosOut;
301 VtrkPosOut.push_back(posout);
303 StatusCode sc=
setTrack(layer,Vparticle, Vcharge, Vp, VtrkPosIn, VtrkPosOut);
447StatusCode
CgemDigitizerSvc::setTrack(
int layer, vector<int>particle, vector<int> charge, vector<double> p, vector<vector<double> > trkPosIn, vector<vector<double> > trkPosOut){
454 double time_spent = 0.;
455 clock_t begin = clock();
460 vector<double> ionEx;
461 vector<double> ionEy;
462 vector<double> ionEz;
463 vector<double> ionEt;
464 for(
unsigned TrackSeg=0;TrackSeg<particle.size();TrackSeg++){
466 double SegTrkPosIn[3];
467 double SegTrkPosOut[3];
468 for(
int i=0;i<3;i++){
469 SegTrkPosIn[i]=trkPosIn[TrackSeg][i];
470 SegTrkPosOut[i]=trkPosOut[TrackSeg][i];
473 m_pIon->
setTrack(particle[TrackSeg], charge[TrackSeg], p[TrackSeg], SegTrkPosIn, SegTrkPosOut);
476 nTotIonE=nIonE+nTotIonE;
477 for(
int i=0; i<nIonE; i++){
478 ionEx.push_back(m_pIon->
getEx(i));
479 ionEy.push_back(m_pIon->
getEy(i));
480 ionEz.push_back(m_pIon->
getEz(i));
481 ionEt.push_back(m_pIon->
getEt(i));
485 if(m_saveNt) m_ntNIonE=nTotIonE;
486 clock_t end = clock();
487 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
493 if (m_driftAvaModel !=3){
495 m_pDriftAndAva->
setIonElectrons(layer, nTotIonE, ionEx, ionEy, ionEz, ionEt);
514 m_ntNMultiE=m_nMultiE;
517 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
526 m_pInduction->
setMultiElectrons(layer, m_nMultiE, *m_pmultiEx, *m_pmultiEy, *m_pmultiEz, *m_pmultiEt);
533 pSampling2->
setIonElectrons(layer, nTotIonE, ionEx, ionEy, ionEz, ionEt);
537 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
546 m_ntNMultiE=m_nMultiE;
555 if (!m_samplingDebugging){
556 pMultiElectronsInfo=0;
563 for(
int i=0; i<m_nXstrips; i++){
565 if(m_saveNt && i<100) {
571 m_xstripID.push_back(m_pInduction->
getXstripID(i));
572 m_xstripQ.push_back(m_pInduction->
getXstripQ(i));
574 m_xstripT.push_back(m_pInduction->
getXstripT(i));
575 m_xfirstT.push_back(m_pInduction->
getXfirstT(i));
581 for(
int i=0; i<m_nVstrips; i++){
583 if(m_saveNt && i<100) {
589 m_vstripID.push_back(m_pInduction->
getVstripID(i));
590 m_vstripQ.push_back(m_pInduction->
getVstripQ(i));
592 m_vstripT.push_back(m_pInduction->
getVstripT(i));
593 m_vfirstT.push_back(m_pInduction->
getVfirstT(i));
600 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
610 m_ntNxstrips = m_nXstrips;
611 m_ntNvstrips = m_nVstrips;
616 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
620 return StatusCode::SUCCESS;
623void CgemDigitizerSvc::positionToStrips(){
629 TObjArray* hlist =
new TObjArray(0);
631 for(
int i=0; i<m_nMultiE; i++){
635 double phi = atan2((*m_pmultiEy)[i], (*m_pmultiEx)[i]);
636 double z = (*m_pmultiEz)[i];
637 G4ThreeVector pos((*m_pmultiEx)[i], (*m_pmultiEy)[i], z);
639 for(
int j=0; j<m_nSheet; j++)
653 if(xID>=0&&distX<distV) {
658 map<int, int>::iterator it = m_mapQ[j][iView].find(ID);
659 if(it==m_mapQ[j][iView].end())
661 m_mapQ[j][iView][ID]=1;
662 m_mapT[j][iView][ID]=(*m_pmultiEt)[i];
665 int Q = (it->second) +1;
666 m_mapQ[j][iView][ID]=Q;
667 if((*m_pmultiEt)[i] < m_mapT[j][iView][ID]) m_mapT[j][iView][ID] = (*m_pmultiEt)[i];
670 sprintf(hname,
"time%d_%d_%d", j, iView, ID);
671 TH1F* hist = (TH1F*)hlist->FindObject(hname);
673 hist =
new TH1F(hname,
"", 1000, 0, 1000);
676 hist->Fill((*m_pmultiEt)[i]);
682 m_nXstrips = m_mapQ[0][0].size() + m_mapQ[1][0].size();
683 m_nVstrips = m_mapQ[0][1].size() + m_mapQ[1][1].size();
686 int nhist = hlist->GetEntries();
689 for(
int i=0; i<nhist; i++){
693 hist = (TH1F*)hlist->First();
695 hist = (TH1F*)hlist->After(hlast);
698 const char* histname = hist->GetName();
699 int iSheet, iView, ID;
700 sscanf(histname,
"%*4s%1d%*1s%1d%*1s%d", &iSheet, &iView, &ID);
703 m_xstripSheet.push_back(iSheet);
704 m_xstripID.push_back(ID);
705 m_xstripQ.push_back(m_mapQ[iSheet][iView][ID]);
706 m_xstripT.push_back(m_mapT[iSheet][iView][ID]);
708 m_vstripSheet.push_back(iSheet);
709 m_vstripID.push_back(ID);
710 m_vstripQ.push_back(m_mapQ[iSheet][iView][ID]);
711 m_vstripT.push_back(m_mapT[iSheet][iView][ID]);
715 for(
int i=0; i<nhist; i++){
716 TH1F* hist = (TH1F*)hlist->Last();
724void CgemDigitizerSvc::clear(){
727 m_xstripSheet.clear();
729 m_vstripSheet.clear();
745 for(
int i=0; i<2; i++){
746 for(
int k=0; k<2; k++){
747 m_mapQ[i][k].clear();
748 m_mapT[i][k].clear();
754void CgemDigitizerSvc::checkMemorySize()
757 gSystem->GetProcInfo(&info);
758 double currentMemory = info.fMemResident/1024;
759 cout<<
"CgemDigitizerSvc: current memory size "<<currentMemory<<
" MB"<<endl;
virtual StatusCode finalize()
CgemDigitizerSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode initialize()
StatusCode setTrack(int layer, int particle, int charge, double p, double trkPosIn[], double trkPosOut[])
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
int getNumberOfSheet() const
int getClosestXStripID(double phi, double &dist)
bool OnThePlane(double phi, double z) const
int getClosestVStripID(G4ThreeVector pos, double &dist) const
virtual const std::vector< Float_t > & getXContainer() const =0
virtual const std::vector< Float_t > & getYContainer() const =0
virtual void setIonElectrons(int layer, int nElectrons, std::vector< double > x, std::vector< double > y, std::vector< double > z, std::vector< double > t)=0
virtual void init(ICgemGeomSvc *geomSvc, double magConfig)=0
virtual void setDebugging(bool debugging)=0
virtual const std::vector< Float_t > & getZContainer() const =0
virtual int getNelectrons() const =0
virtual const std::vector< Float_t > & getTContainer() const =0
virtual CgemGeoReadoutPlane * getReadoutPlane(int iLayer, int iSheet) const =0
virtual CgemGeoLayer * getCgemLayer(int i) const =0
void setQinGausSigma(vector< double > sigma)
void setScaleSignalX(double ScaleSignalX)
void setMultiElectrons(int layer, int nElectrons, const std::vector< Float_t > &x, const std::vector< Float_t > &y, const std::vector< Float_t > &z, const std::vector< Float_t > &t)
void setGapSizeSect(double size)
void setMicroSectorWidthRad(vector< double > width)
void setScaleSignalX(double ScaleSignalX)
void setQinGausSigma(vector< double > sigma)
void setGapShiftSect(vector< double > shift)
virtual void setStoreFlag(bool flag)=0
virtual double getXstripT(int n) const =0
virtual double getXfirstT(int n) const =0
virtual int getNVstrips() const =0
virtual int getVstripSheet(int n) const =0
virtual int getXstripID(int n) const =0
virtual int getVstripID(int n) const =0
virtual double getXstripQ_Branch(int n) const =0
virtual void setSaturation(bool flag)=0
virtual double getVstripQ(int n) const =0
virtual void setVsampleDelay(double delay)=0
virtual int getXstripSheet(int n) const =0
virtual double getVfirstT(int n) const =0
virtual void setMultiElectrons(int layer, int nElectrons, const std::vector< Float_t > &x, const std::vector< Float_t > &y, const std::vector< Float_t > &z, const std::vector< Float_t > &t)=0
virtual double getXstripQ(int n) const =0
virtual void setDebugOutput(bool debugOutput)=0
virtual double getVstripQ_Branch(int n) const =0
virtual void setLUTFilePath(std::string path)=0
virtual void init(ICgemGeomSvc *geomSvc, double magConfig)=0
virtual double getVstripT(int n) const =0
virtual int getNXstrips() const =0
virtual double getEx(int nElec)=0
virtual int getNumberIonE()=0
virtual double getEy(int nElec)=0
virtual void setDebugging(bool debugging)=0
virtual double getEt(int nElec)=0
virtual double getEz(int nElec)=0
virtual void setTrack(int particle, int charge, double p, double trkPosIn[], double trkPosOut[])=0
virtual void init(unsigned int random, ICgemGeomSvc *geomSvc, double magConfig)=0
void setTransMultiplier(double TransMultiplier)
void setGapSizeSect(double size)
void setGapShiftSect(vector< double > shift)
void setGainMultiplier(vector< double > GainMultiplier)
void setIonElectrons(int layer, int nElectrons, std::vector< double > x, std::vector< double > y, std::vector< double > z, std::vector< double > t)
void setDiffuMultiplier(double DiffuMultiplier)
void setMultiElectronMapAddr(HitHistMap *address)
void setMicroSectorWidthRad(vector< double > width)
void setGainMultiplier(vector< double > GainMultiplier)
void setDiffuMultiplier(double DiffuMultiplier)
void setTransMultiplier(double TransMultiplier)
const std::vector< Float_t > * z
const std::vector< Float_t > * t
const std::vector< Float_t > * x
const std::vector< Float_t > * y