CGEM BOSS 6.6.5.f
BESIII Offline Software System
Loading...
Searching...
No Matches
CgemDigitizerSvc.cxx
Go to the documentation of this file.
1#include "CgemDigitizerSvc/CgemDigitizerSvc.h"
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"
14
15#include "GaudiKernel/ISvcLocator.h"
16#include "GaudiKernel/Bootstrap.h"
17
18#include "GaudiKernel/IDataProviderSvc.h"
19#include "GaudiKernel/SmartDataPtr.h"
20#include "GaudiKernel/DataSvc.h"
21
22#include "CalibData/CalibModel.h"
23// #include "CalibData/Cgem/CgemCalibData.h"
24// #include "CalibData/Cgem/CgemDataConst.h"
25#include "EventModel/EventHeader.h"
26#include "GaudiKernel/SmartDataPtr.h"
27
28#include <iomanip>
29#include <iostream>
30#include <fstream>
31#include <cmath>
32
33#include "TObjArray.h"
34
35using namespace std;
36
37CgemDigitizerSvc::CgemDigitizerSvc( const string& name, ISvcLocator* svcloc) :Service (name, svcloc){
38 // declare properties
39 declareProperty("IonizationModel", m_ionModel);
40 declareProperty("DriftAvalancheModel", m_driftAvaModel);
41 declareProperty("InductionModel", m_inductionModel);
42 declareProperty("GarfieldDebugging", m_garfDebugging);
43 declareProperty("InductionDebugging", m_debugInduction);
44 declareProperty("MagConfig", m_magConfig);
45 //cout << "CgemDigitizerSvc::m_ionModel = " << m_ionModel << endl;
46}
47
49}
50
51StatusCode CgemDigitizerSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
52 if( IID_ICgemDigitizerSvc.versionMatch(riid) ){
53 *ppvInterface = static_cast<ICgemDigitizerSvc*> (this);
54 } else{
55 return Service::queryInterface(riid, ppvInterface);
56 }
57 return StatusCode::SUCCESS;
58}
59
61 MsgStream log(messageService(), name());
62 log << MSG::INFO << "CgemDigitizerSvc::initialize()" << endreq;
63
64 StatusCode sc = Service::initialize();
65 if( sc.isFailure() ) return sc;
66
67 static IJobOptionsSvc* jobSvc = 0;
68 if ( jobSvc == 0 ) {
69 sc = service("JobOptionsSvc", jobSvc);
70 if ( sc.isFailure() ) {
71 std::cout << "Can't get the JobOptionsSvc @ DistBoss::GetPropertyValue()" << std::endl;
72 return sc;
73 }
74 }
75
76 // const vector<const Property*>* properties = jobSvc->getProperties(client);
77 const vector<const Property*>* properties = jobSvc->getProperties("BesRndmGenSvc");
78 // cout << "client of jobSvc: " << jobSvc->getClients() << endl;
79 if ( properties == NULL ) {
80 std::cout << "In CgemDigitizerSvc::initialize(), can't get client: " << std::endl;
81 return StatusCode::FAILURE;
82 }
83
84 unsigned int randSeed;
85 for ( unsigned int i = 0; i < properties->size(); ++i ) {
86 if ( properties->at(i)->name() == "RndmSeed" ) {
87 cout << "name of property: " << properties->at(i)->name() << endl;
88 string strRnd = properties->at(i)->toString();
89 sscanf(strRnd.c_str(), "%u", &randSeed);
90 cout << "random seed from jobOption: " << randSeed << endl;
91 break;
92 }
93 }
94
95 sc = service("CgemGeomSvc", m_geomSvc); // initial pointer of CgemGeomSvc
96 if(sc != StatusCode::SUCCESS)
97 {
98 log << MSG::ERROR << "can not use CgemGeomSvc" << endreq;
99 return StatusCode::FAILURE;
100 }
101
102 gRandom->SetSeed(randSeed); // set random seed
103
104 cout << "ionModel = " << m_ionModel << endl;
105 if(1 == m_ionModel){
106 m_pIon = new IonizationGar();
107 } else if(2 == m_ionModel){
108 m_pIon = new IonizationGTS();
109 }
110 // m_pIon = new IonizationGar();
111 m_pIon->init(randSeed, m_geomSvc, m_magConfig);
112 m_pIon->setDebugging(m_garfDebugging);
113
114 if(1 == m_driftAvaModel){
115 m_pDriftAndAva = new SamplingGar();
116 } else if(2 == m_driftAvaModel){
117 m_pDriftAndAva = new SamplingGTS();
118 }
119 m_pDriftAndAva->init(m_geomSvc, m_magConfig);
120
121 if(1 == m_inductionModel){
122 m_pInduction = new InductionGar();
123 } else if(2 == m_inductionModel){
124 m_pInduction = new InductionGTS();
125 }
126 m_pInduction->init(m_geomSvc, m_magConfig);
127 m_pInduction->setDebugOutput(m_debugInduction);
128
129 // ntuple
130 INTupleSvc* ntupleSvc;
131 Gaudi::svcLocator() -> service("NTupleSvc", ntupleSvc);
132 NTuplePtr nt(ntupleSvc, "CgemDigitizerSvcNT/digi");
133 // NTuplePtr nt(ntupleSvc(), "CgemDigitizerSvcNT/digi");
134 if(nt) m_tuple = nt;
135 else{
136 m_tuple = ntupleSvc->book("CgemDigitizerSvcNT/digi", CLID_ColumnWiseTuple, "noise");
137 if(m_tuple){
138 m_tuple->addItem("nIonE", m_ntNIonE);
139 m_tuple->addItem("nMultiE", m_ntNMultiE);
140 m_tuple->addItem("nXstrips", m_ntNxstrips,0,100);
141 m_tuple->addItem("nVstrips", m_ntNvstrips,0,100);
142 m_tuple->addIndexedItem("XstripQ", m_ntNxstrips,m_ntxstripQ);
143 m_tuple->addIndexedItem("XstripT", m_ntNxstrips,m_ntxstripT);
144 m_tuple->addIndexedItem("VstripQ", m_ntNvstrips,m_ntvstripQ);
145 m_tuple->addIndexedItem("VstripT", m_ntNvstrips,m_ntvstripT);
146 m_tuple->addIndexedItem("XstripID", m_ntNxstrips,m_XstripID);
147 m_tuple->addIndexedItem("VstripID", m_ntNvstrips,m_VstripID);
148 }
149 }
150
151 return StatusCode::SUCCESS;
152}
153
155 MsgStream log(messageService(), name());
156 log << MSG::INFO << "CgemDigitizerSvc::finalize()" << endreq;
157 delete m_pIon;
158 delete m_pDriftAndAva;
159 delete m_pInduction;
160
161 return StatusCode::SUCCESS;
162}
163
164StatusCode CgemDigitizerSvc::setTrack(int layer, int particle, int charge, double p, double trkPosIn[], double trkPosOut[]){
165
166 double time_spent = 0.;
167 clock_t begin = clock();
168
169 clear();
170 m_layer = layer;
171
172 m_pIon->setTrack(particle, charge, p, trkPosIn, trkPosOut);
173
174 vector<double> ionEx;
175 vector<double> ionEy;
176 vector<double> ionEz;
177 vector<double> ionEt;
178 int nIonE = m_pIon->getNumberIonE();
179 for(int i=0; i<nIonE; i++){
180 ionEx.push_back(m_pIon->getEx(i));
181 ionEy.push_back(m_pIon->getEy(i));
182 ionEz.push_back(m_pIon->getEz(i));
183 ionEt.push_back(m_pIon->getEt(i));
184 }
185 clock_t end = clock();
186 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
187 // cout << "CgemDigitizerSvc::ionization " << time_spent << " seconds" << endl;
188 // ---------------------------
189 time_spent = 0.;
190 begin = clock();
191
192 m_pDriftAndAva->setIonElectrons(layer, nIonE, ionEx, ionEy, ionEz, ionEt);
193
194 m_nMultiE = m_pDriftAndAva->getNelectrons();
195 for(int i=0; i<m_nMultiE; i++){
196 double x = m_pDriftAndAva->getX(i);
197 double y = m_pDriftAndAva->getY(i);
198 double z = m_pDriftAndAva->getZ(i);
199 double t = m_pDriftAndAva->getT(i);
200 m_multiEx.push_back(x);
201 m_multiEy.push_back(y);
202 m_multiEz.push_back(z);
203 m_multiEt.push_back(t);
204 }
205 end = clock();
206 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
207 // cout << "CgemDigitizerSvc::diffusion " << time_spent << " seconds" << endl;
208 // --------------------
209 time_spent = 0.;
210 begin = clock();
211
212 // Induction process
213 // positionToStrips();
214 // cout << "nXstrips = " << m_nXstrips << " , m_nVstrips = " << m_nVstrips << endl;
215 m_pInduction->setMultiElectrons(layer, m_nMultiE, m_multiEx, m_multiEy, m_multiEz, m_multiEt);
216 m_nXstrips = m_pInduction->getNXstrips();
217 for(int i=0; i<m_nXstrips; i++){
218 m_xstripSheet.push_back(m_pInduction->getXstripSheet(i));
219 m_xstripID.push_back(m_pInduction->getXstripID(i));
220 m_xstripQ.push_back(m_pInduction->getXstripQ(i));
221 m_xstripT.push_back(m_pInduction->getXstripT(i));
222 }
223 m_nVstrips = m_pInduction->getNVstrips();
224 for(int i=0; i<m_nVstrips; i++){
225 m_vstripSheet.push_back(m_pInduction->getVstripSheet(i));
226 m_vstripID.push_back(m_pInduction->getVstripID(i));
227 m_vstripQ.push_back(m_pInduction->getVstripQ(i));
228 m_vstripT.push_back(m_pInduction->getVstripT(i));
229 }
230 end = clock();
231 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
232 // cout << "CgemDigitizerSvc::induction " << time_spent << " seconds" << endl;
233 // ---------------
234 time_spent = 0.;
235 begin = clock();
236
237 //m_ntNIonE = nIonE;
238 //m_ntNMultiE = m_nMultiE;
239 //m_ntNxstrips = m_nXstrips;
240 //m_ntNvstrips = m_nVstrips;
241 //m_tuple->write();
242
243 end = clock();
244 time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
245 // cout << "CgemDigitizerSvc::write " << time_spent << " seconds" << endl;
246
247
248 return StatusCode::SUCCESS;
249}
250
251void CgemDigitizerSvc::positionToStrips(){
252 m_cgemLayer = m_geomSvc->getCgemLayer(m_layer);
253 m_nSheet = m_cgemLayer->getNumberOfSheet();
254 int i_sheet;
255
256 char hname[200];
257 TObjArray* hlist = new TObjArray(0);
258 // cout << "nMultiE :" << m_nMultiE << endl;
259 for(int i=0; i<m_nMultiE; i++){
260 // ofstream fout("elecPos.txt");
261 // for(int i=0; i<1000; i++){
262 // cout << "nMultiE :" << i << endl;
263 double phi = atan2(m_multiEy[i], m_multiEx[i]);
264 double z = m_multiEz[i];
265 G4ThreeVector pos(m_multiEx[i], m_multiEy[i], z);
266 // fout << setw(5) << i << setw(20) << m_multiEx[i] << setw(20) << m_multiEy[i] << setw(20) << m_multiEz[i] << endl;
267 for(int j=0; j<m_nSheet; j++)
268 {
269 m_cgemReadoutPlane = m_geomSvc->getReadoutPlane(m_layer, j);
270 if(m_cgemReadoutPlane->OnThePlane(phi,z))
271 {
272 int iView = 1;
273 int ID = -1;
274 double distV;
275 int vID = m_cgemReadoutPlane->getClosestVStripID(pos, distV);
276 ID = vID;
277 if(vID>=0) {
278 if(distV>0) {
279 double distX;
280 int xID = m_cgemReadoutPlane->getClosestXStripID(phi, distX);
281 if(xID>=0&&distX<distV) {
282 ID = xID;
283 iView = 0;
284 }
285 }
286 map<int, int>::iterator it = m_mapQ[j][iView].find(ID);
287 if(it==m_mapQ[j][iView].end())
288 {
289 m_mapQ[j][iView][ID]=1;
290 m_mapT[j][iView][ID]=m_multiEt[i];
291 }
292 else {
293 int Q = (it->second) +1;
294 m_mapQ[j][iView][ID]=Q;
295 if(m_multiEt[i] < m_mapT[j][iView][ID]) m_mapT[j][iView][ID] = m_multiEt[i];
296 }
297 // T info
298 sprintf(hname, "time%d_%d_%d", j, iView, ID);
299 TH1F* hist = (TH1F*)hlist->FindObject(hname);
300 if(NULL == hist){
301 hist = new TH1F(hname, "", 1000, 0, 1000);
302 hlist->Add(hist);
303 }
304 hist->Fill(m_multiEt[i]);
305 }
306 break;
307 }
308 }
309 }
310 m_nXstrips = m_mapQ[0][0].size() + m_mapQ[1][0].size();
311 m_nVstrips = m_mapQ[0][1].size() + m_mapQ[1][1].size();
312 // fout.close();
313
314 int nhist = hlist->GetEntries();
315 // cout << setw(10) << m_nXstrips+m_nVstrips << setw(10) << nhist
316 // << "nXstrips = " << m_nXstrips << " , m_nVstrips = " << m_nVstrips << endl;
317 for(int i=0; i<nhist; i++){
318 TH1F* hist;
319 TH1F* hlast;
320 if(0==i){
321 hist = (TH1F*)hlist->First();
322 } else{
323 hist = (TH1F*)hlist->After(hlast);
324 }
325 hlast = hist;
326 const char* histname = hist->GetName();
327 int iSheet, iView, ID;
328 sscanf(histname, "%*4s%1d%*1s%1d%*1s%d", &iSheet, &iView, &ID);
329
330 if(0==iView){
331 m_xstripSheet.push_back(iSheet);
332 m_xstripID.push_back(ID);
333 m_xstripQ.push_back(m_mapQ[iSheet][iView][ID]);
334 m_xstripT.push_back(m_mapT[iSheet][iView][ID]);
335 } else{
336 m_vstripSheet.push_back(iSheet);
337 m_vstripID.push_back(ID);
338 m_vstripQ.push_back(m_mapQ[iSheet][iView][ID]);
339 m_vstripT.push_back(m_mapT[iSheet][iView][ID]);
340 }
341 }
342
343 for(int i=0; i<nhist; i++){
344 TH1F* hist = (TH1F*)hlist->Last();
345 hlist->Remove(hist);
346 delete hist;
347 }
348 hlist->Clear();
349 delete hlist;
350}
351
352void CgemDigitizerSvc::clear(){
353 m_nXstrips = 0;
354 m_nVstrips = 0;
355 m_xstripSheet.clear();
356 m_xstripID.clear();
357 m_vstripSheet.clear();
358 m_vstripID.clear();
359 m_xstripQ.clear();
360 m_vstripQ.clear();
361 m_xstripT.clear();
362 m_vstripT.clear();
363
364 m_nMultiE = 0;
365 m_multiEx.clear();
366 m_multiEy.clear();
367 m_multiEz.clear();
368 m_multiEt.clear();
369
370 for(int i=0; i<2; i++){
371 for(int k=0; k<2; k++){
372 m_mapQ[i][k].clear();
373 m_mapT[i][k].clear();
374 }
375 }
376}
377
Double_t x[10]
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 getClosestXStripID(double phi, double &dist)
int getClosestVStripID(G4ThreeVector pos, double &dist) const
virtual double getX(int n) const =0
virtual double getY(int n) 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 double getZ(int n) const =0
virtual double getT(int n) const =0
virtual int getNelectrons() const =0
virtual CgemGeoReadoutPlane * getReadoutPlane(int iLayer, int iSheet) const =0
virtual CgemGeoLayer * getCgemLayer(int i) const =0
virtual double getXstripT(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 getVstripQ(int n) const =0
virtual int getXstripSheet(int n) const =0
virtual double getXstripQ(int n) const =0
virtual void setDebugOutput(bool debugOutput)=0
virtual void setMultiElectrons(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 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
int t()
Definition: t.c:1