00001 #include "GaudiKernel/MsgStream.h"
00002 #include "GaudiKernel/AlgFactory.h"
00003 #include "GaudiKernel/IDataProviderSvc.h"
00004 #include "GaudiKernel/SmartDataPtr.h"
00005
00006 #include "GlastEvent/TopLevel/Event.h"
00007
00008 #include "GlastSvc/GlastDetSvc/IGlastDetSvc.h"
00009
00010 #include "reconRootWriterAlg.h"
00011
00012 #include "reconRootData/Recon.h"
00013
00014 #include "reconRootData/TkrSiCluster.h"
00015 #include "reconRootData/TkrTrack.h"
00016 #include "reconRootData/TkrRecon.h"
00017 #include "reconRootData/ReconHeader.h"
00018
00019 #include "reconRootData/TkrLocator.h"
00020 #include "reconRootData/TkrGamma.h"
00021 #include "reconRootData/TkrHit.h"
00022
00023 #include "reconRootData/CalLogEne.h"
00024 #include "reconRootData/CalCluster.h"
00025 #include "reconRootData/CalRecon.h"
00026
00027 #include "TROOT.h"
00028 #include "TFile.h"
00029 #include "TTree.h"
00030 #include "TDirectory.h"
00031
00032 #include "TkrRecon/SiRecObjs.h"
00033 #include "TkrRecon/SiClusters.h"
00034 #include "CalRecon/CalRecLogs.h"
00035 #include "CalRecon/CsIClusters.h"
00036
00037 static const AlgFactory<reconRootWriterAlg> Factory;
00038 const IAlgFactory& reconRootWriterAlgFactory = Factory;
00039
00040 static const int bufsize = 64000;
00041 static const int split = 1;
00042
00043
00044 reconRootWriterAlg::reconRootWriterAlg(const std::string& name, ISvcLocator* pSvcLocator) :
00045 Algorithm(name, pSvcLocator)
00046 {
00047 declareProperty("reconRootFile",m_nameFile);
00048 declareProperty("inputSource",m_inputSource="SIM");
00049 }
00050
00051
00052
00053 StatusCode reconRootWriterAlg::initialize()
00054 {
00055
00056
00057 StatusCode sc = StatusCode::SUCCESS;
00058 MsgStream log(msgSvc(), name());
00059
00060
00062
00063
00064 if(0 == gROOT)
00065 {
00066 static TROOT root("root","Root I/O");
00067 log << MSG::INFO << "Initializing ROOT" << endreq;
00068 } else {
00069 log << MSG::INFO << "Root already initialized(not an error)" <<endreq;
00070 }
00071
00072 TDirectory *saveDir = gDirectory;
00073
00074 recFile = new TFile(m_nameFile.c_str(), "RECREATE");
00075 if (!recFile->IsOpen()) sc = StatusCode::FAILURE;
00076 recFile->cd();
00077 recTree = new TTree("T", "TreeBase");
00078 m_rec = new Recon();
00079 m_rec->Create();
00080 recTree->Branch("Recon","Recon",&m_rec, bufsize, split);
00081
00082 saveDir->cd();
00083 return sc;
00084
00085 }
00086
00087 StatusCode reconRootWriterAlg::execute()
00088 {
00089 StatusCode sc = StatusCode::SUCCESS;
00090
00092 sc = retrieve();
00093
00094 saveHeader();
00095
00097 save(m_SiClusters);
00098 save(m_SiRecObjs);
00099 save(m_CalRecLogs);
00100 save(m_CsIClusterList);
00101
00103 writeEvent();
00104 return sc;
00105 }
00106
00107 StatusCode reconRootWriterAlg::saveHeader() {
00108 MsgStream log(msgSvc(), name());
00109
00110
00111
00112
00113 std::string headerPathStr;
00114 if (strcmp(m_inputSource.c_str(),"ROOT")==0)
00115 headerPathStr = "/Event/Header";
00116 else
00117 headerPathStr = "/Event";
00118
00119 SmartDataPtr<Event> event(eventSvc(),headerPathStr.c_str());
00120 if( 0==event) { log << MSG::ERROR << "could not find the event header" << endreq;
00121 return StatusCode::FAILURE;
00122 }
00123
00124 m_rec->getReconFlags()->setEventId(event->event());
00125 m_rec->getReconFlags()->setRunId(event->run());
00126 return StatusCode::SUCCESS;
00127 }
00128
00129
00130 void reconRootWriterAlg::writeEvent()
00131 {
00132 TDirectory *saveDir = gDirectory;
00133 recFile->cd();
00134 recTree->Fill();
00135
00136 m_rec->Clean();
00137 m_rec->Create();
00138
00139 saveDir->cd();
00140 }
00141
00142 void reconRootWriterAlg::close()
00143 {
00144 TDirectory *saveDir = gDirectory;
00145 recFile->cd();
00146 recFile->Write(0, TObject::kOverwrite);
00147 recFile->Close();
00148 saveDir->cd();
00149 }
00150
00151 StatusCode reconRootWriterAlg::finalize()
00152 {
00153 close();
00154
00155 StatusCode sc = StatusCode::SUCCESS;
00156 return sc;
00157 }
00158
00159 StatusCode reconRootWriterAlg::retrieve()
00160 {
00161 StatusCode sc = StatusCode::SUCCESS;
00162
00163 m_SiClusters = SmartDataPtr<SiClusters>(eventSvc(),"/Event/TkrRecon/SiClusters");
00164 m_SiRecObjs = SmartDataPtr<SiRecObjs>(eventSvc(),"/Event/TkrRecon/SiRecObjs");
00165 m_CalRecLogs = SmartDataPtr<CalRecLogs>(eventSvc(),"/Event/CalRecon/CalRecLogs");
00166 m_CsIClusterList = SmartDataPtr<CsIClusterList>(eventSvc(),"/Event/CalRecon/CsIClusterList");
00167
00168 return sc;
00169 }
00170
00171 void reconRootWriterAlg::save(SiClusters* sicl)
00172 {
00173 if (sicl)
00174 {
00175 int nhits = sicl->nHits();
00176 unsigned int ihit = 0;
00177
00178 TObjArray *clusterArr = m_rec->getTkrRecon()->getSiClusters();
00179
00180 for (ihit = 0; ihit < nhits; ihit ++) {
00181 SiCluster* scl = sicl->getHit(ihit);
00182 TkrSiCluster* rcl = converter(scl);
00183 clusterArr->Add(rcl);
00184 }
00185 }
00186 }
00187
00188 TkrSiCluster* reconRootWriterAlg::converter(const SiCluster* scl)
00189 {
00190 TkrSiCluster* rcl = new TkrSiCluster();
00191 rcl->setCenterStrip(scl->strip());
00192 rcl->setId(scl->id());
00193 rcl->setLayer(scl->plane());
00194 rcl->setNumStrips(scl->size());
00195 Point p = scl->position();
00196 Float_t x = (scl->v() == 0? p.x():p.y());
00197 rcl->setPosition(x);
00198 TkrSiCluster::TKRAxes axis;
00199 axis = (scl->v()==0? TkrSiCluster::X : TkrSiCluster::Y);
00200 rcl->setXY(axis);
00201 rcl->setZPosition(p.z());
00202 return rcl;
00203 }
00204
00205 void reconRootWriterAlg::save(SiRecObjs* sirec)
00206 {
00207 if (sirec)
00208 {
00209 importGamma(sirec);
00210 importTracks(sirec);
00211 }
00212 }
00213
00214 void reconRootWriterAlg::importGamma(SiRecObjs* sirec)
00215 {
00216
00217 int ngamma = sirec->numGammas();
00218 if (ngamma !=1 ) return;
00219
00220
00221 TkrGamma* rgamma = new TkrGamma(1);
00222 m_rec->getTkrRecon()->setGamma(rgamma);
00223
00224 GFgamma* gamma = sirec->Gamma(0);
00225 TkrLocator* rloc = converter(gamma);
00226 ((TObjArray*) rgamma->getLocator())->Add(rloc);
00227 TObjArray* rgammaTracks = rgamma->getTracks();
00228
00229 TObjArray* rtracks = m_rec->getTkrRecon()->getTracks();
00230
00231 m_idtrack = 0;
00232
00233 const GFtrack* xbest = gamma->getBest(SiCluster::X);
00234 TkrTrack* rbxtrack = converter(xbest,0,true);
00235 rbxtrack->setId((UShort_t) m_idtrack++);
00236 rtracks->Add(rbxtrack);
00237 rgammaTracks->Add(rbxtrack);
00238
00239 const GFtrack* ybest = gamma->getBest(SiCluster::Y);
00240 TkrTrack* rbytrack = converter(ybest,0,true);
00241 rbytrack->setId( (UShort_t) m_idtrack++);
00242 rtracks->Add(rbytrack);
00243 rgammaTracks->Add(rbytrack);
00244
00245 const GFtrack* xpair = gamma->getPair(SiCluster::X);
00246 if (!xpair->empty()) {
00247 TkrTrack* rpxtrack = converter(xpair,1,true);
00248 rpxtrack->setId( (UShort_t) m_idtrack++);
00249 rtracks->Add(rpxtrack);
00250 rgammaTracks->Add(rpxtrack);
00251 }
00252
00253 const GFtrack* ypair = gamma->getPair(SiCluster::Y);
00254 if (!ypair->empty()) {
00255 TkrTrack* rpytrack = converter(ypair,1,true);
00256 rpytrack->setId( (UShort_t) m_idtrack++);
00257 rtracks->Add(rpytrack);
00258 rgammaTracks->Add(rpytrack);
00259 }
00260
00261 if (rtracks->GetEntries() <=0) {
00262 int badbad = 1;
00263 }
00264 }
00265
00266
00267 void reconRootWriterAlg::importTracks(SiRecObjs* sirec)
00268 {
00269 int nparticles = sirec->numParticles();
00270 if (nparticles <=0) return;
00271
00272 TObjArray* rTracks = m_rec->getTkrRecon()->getTracks();
00273 for (int iparticle = 0; iparticle < nparticles; iparticle++) {
00274 GFparticle* particle = sirec->Particle(iparticle);
00275 const GFtrack* xtrack = particle->getXGFtrack();
00276 TkrTrack* rxtrack = converter(xtrack,iparticle);
00277 rxtrack->setId( (UShort_t) m_idtrack++);
00278 rTracks->Add(rxtrack);
00279 const GFtrack* ytrack = particle->getYGFtrack();
00280 TkrTrack* rytrack = converter(ytrack,iparticle);
00281 rytrack->setId( (UShort_t) m_idtrack++);
00282 rTracks->Add(rytrack);
00283 }
00284 }
00285
00286
00287 void reconRootWriterAlg::importHits(const GFtrack* track, TkrTrack* rtrack)
00288 {
00289 int nhits = track->nhits();
00290 if (nhits <=2) return;
00291
00292 SiCluster::view a = track->getAxis();
00293 TObjArray* rHits = rtrack->getHits();
00294 for (int ihit = 0; ihit < nhits; ihit++) {
00295 const KalPlane& kplane = track->kplanelist[ihit];
00296 TkrHit* rhit = converter(kplane,a);
00297 rHits->Add(rhit);
00298 }
00299 if (rHits->GetEntries() <=0 ) {
00300 int badbad = 1;
00301 }
00302 }
00303
00304 TkrLocator* reconRootWriterAlg::converter(const GFdata* gf)
00305 {
00306
00307 TkrLocator* rloc = new TkrLocator(1);
00308 Float_t x = gf->vertex().x();
00309 Float_t y = gf->vertex().y();
00310 Float_t z = gf->vertex().z();
00311 rloc->setPosition(new TVector3(x,y,z));
00312
00313 Float_t tx = gf->direction().x();
00314 Float_t ty = gf->direction().y();
00315 Float_t tz = gf->direction().z();
00316 Float_t slopex = (tz != 0. ? slopex = tx/tz : 0);
00317 Float_t slopey = (tz != 0. ? slopey = ty/tz : 0);
00318 rloc->setDirection(new TVector2(slopex,slopey));
00319 return rloc;
00320 }
00321
00322 TkrTrack* reconRootWriterAlg::converter(const GFtrack* track, int id, bool mother)
00323 {
00324 TkrTrack* rtrack = new TkrTrack(1);
00325
00326
00327
00328 TkrLocator *rloc = converter(track);
00329 rtrack->getLocator()->Add(rloc);
00330
00331
00332 Float_t chi2 = track->chiSquare();
00333 rtrack->setChi2(chi2);
00334
00335 Float_t eneDet = track->RCenergy();
00336 rtrack->setEnergyDet(eneDet);
00337
00338 Float_t eneIn = track->inputEnergy();
00339 rtrack->setEnergyInput(eneIn);
00340
00341 UInt_t fstLayer = track->firstLayer();
00342 rtrack->setFirstLayer(fstLayer);
00343
00344 rtrack->setId(id);
00345
00346 if (mother) rtrack->setMother(TkrTrack::GAMMA);
00347 else rtrack->setMother(TkrTrack::NONE);
00348
00349 Float_t quality = track->Q();
00350 rtrack->setQuality(quality);
00351
00352 Float_t residual = 0.;
00353 rtrack->setResidual(residual);
00354
00355 rtrack->setTrackType(TkrTrack::ELECTRON);
00356
00357 importHits(track, rtrack);
00358
00359 return rtrack;
00360 }
00361
00362 TkrHit* reconRootWriterAlg::converter(const KalPlane& kp, SiCluster::view a)
00363 {
00364 TkrHit* rHit = new TkrHit();
00365
00366 Float_t chi2 = kp.getDeltaChiSq(KalHit::SMOOTH);
00367 rHit->setChi2(chi2);
00368
00369 Float_t xmea = kp.getHit(KalHit::MEAS).getPar().getPosition();
00370 Float_t xfit = kp.getHit(KalHit::SMOOTH).getPar().getPosition();
00371 Float_t res = xfit-xmea;
00372 rHit->setResidual(res);
00373
00374 TkrLocator* rloc = converter(kp,a,1);
00375 rHit->setLocator(rloc);
00376
00377 int ihit = kp.getIDHit();
00378 TkrSiCluster* rcl = m_rec->getTkrRecon()->getSiCluster(ihit);
00379 rHit->setSiCluster(rcl);
00380
00381 return rHit;
00382 }
00383
00384
00385 TkrLocator* reconRootWriterAlg::converter(const KalPlane& kp, SiCluster::view a, int idummy)
00386 {
00387 TkrLocator* rloc = new TkrLocator(1);
00388 Float_t x = 0;
00389 Float_t y = 0;
00390 Float_t xp = kp.getHit(KalHit::SMOOTH).getPar().getPosition();
00391 if (a == SiCluster::X) x = xp;
00392 else y = xp;
00393 Float_t z = kp.getZPlane();
00394 rloc->setPosition(new TVector3(x,y,z));
00395
00396 Float_t slopex = 0.;
00397 Float_t slopey = 0.;
00398 Float_t slopep = kp.getHit(KalHit::SMOOTH).getPar().getSlope();
00399 if (a == SiCluster::X) slopex = slopep;
00400 else slopey = slopep;
00401 rloc->setDirection(new TVector2(slopex,slopey));
00402
00403 Float_t ex = sqrt(kp.getHit(KalHit::SMOOTH).getCov().getsiga());
00404 Float_t es = sqrt(kp.getHit(KalHit::SMOOTH).getCov().getsigb());
00405 Float_t ec = kp.getHit(KalHit::SMOOTH).getCov().getcovab();
00406
00407 if (a == SiCluster::X) rloc->setXmatrix(ex,es,ec);
00408 else rloc->setYmatrix(ex,es,ec);
00409
00410 return rloc;
00411 }
00412
00413
00414
00415
00416
00417
00418 void reconRootWriterAlg::save(CsIClusterList *ccl)
00419 {
00420 if (ccl)
00421 {
00422 int nc = ccl->num();
00423 TObjArray* clusArr = m_rec->getCalRecon()->getCalClusters();
00424 for (int ic = 0; ic<nc; ic++){
00425 ICsICluster* csiclus = ccl->Cluster(ic);
00426 CalCluster* calclus = converter(csiclus);
00427 calclus->setId(ic);
00428 clusArr->Add(calclus);
00429 }
00430 }
00431 }
00432
00433 CalCluster* reconRootWriterAlg::converter(const ICsICluster *cc)
00434 {
00435 CalCluster* calclus = new CalCluster();
00436 calclus->setEsum(cc->energySum());
00437 calclus->setEcorr(cc->energyCorrected());
00438 calclus->setEleak(cc->energyLeak());
00439 Point pos = cc->position();
00440 calclus->setPosition(pos.x(),pos.y(),pos.z());
00441 Vector dir = cc->direction();
00442 calclus->setDirection(dir.x(), dir.y(),dir.z());
00443 calclus->setCsiAlpha(cc->getCsiAlpha());
00444 calclus->setCsiLambda(cc->getCsiLambda());
00445 calclus->setCsiStart(cc->getCsiStart());
00446 calclus->setFitEnergy(cc->getFitEnergy());
00447 calclus->setProfChisq(cc->getProfChisq());
00448 for(int i=0;i<8;i++){
00449 calclus->setEneLayer(i,cc->getEneLayer(i));
00450 const Vector& posl = cc->getPosLayer(i);
00451 calclus->setXLayer(i,posl.x());
00452 calclus->setYLayer(i,posl.y());
00453 }
00454
00455
00456
00457
00458 TObjArray* logArr = m_rec->getCalRecon()->getCalLogs();
00459 TObjArray* cluslogs = calclus->getCalLog();
00460 TObjArrayIter it(logArr);
00461 TObject* log;
00462 while(log = it.Next())cluslogs->Add(log);
00463
00464 return calclus;
00465 }
00466
00467 void reconRootWriterAlg::save(CalRecLogs *crls)
00468 {
00469 if (crls)
00470 {
00471 int nlogs = crls->num();
00472 TObjArray* logArr = m_rec->getCalRecon()->getCalLogs();
00473
00474 for (int il=0; il<nlogs; il++){
00475 CalRecLog* crl = crls->Log(il);
00476 if(crl->posEnergy()>0 || crl->negEnergy()>0){
00477 CalLogEne* cle = converter(crl);
00478 logArr->Add(cle);
00479 }
00480 }
00481 }
00482 }
00483
00484
00485 CalLogEne* reconRootWriterAlg::converter(const CalRecLog* crl)
00486 {
00487 CalLogEne* cle = new CalLogEne();
00488 cle->setePlus(crl->posEnergy());
00489 cle->seteMinus(crl->negEnergy());
00490 int tb_layer = crl->layer();
00491 int tb_col = crl->column();
00492 int tb_view = crl->view();
00493 int root_layer = 7-(2*tb_layer+tb_view);
00494 int root_col = tb_col;
00495 LogId* id = new LogId();
00496 id->setColumn(root_col);
00497 id->setLayer(root_layer);
00498 cle->setId(id);
00499 return cle;
00500 }