00001
00002
00003 #include "Types.h"
00004
00005
00006 #include <Inventor/nodes/SoSeparator.h>
00007 #include <Inventor/nodes/SoLightModel.h>
00008 #include <Inventor/nodes/SoDrawStyle.h>
00009 #include <Inventor/nodes/SoCoordinate3.h>
00010 #include <Inventor/nodes/SoLineSet.h>
00011 #include <Inventor/nodes/SoTransform.h>
00012 #include <Inventor/nodes/SoTranslation.h>
00013
00014 #include <Inventor/nodes/SoFont.h>
00015 #include <Inventor/nodes/SoText2.h>
00016
00017
00018 #include <HEPVis/SbPolyhedron.h>
00019 #include <HEPVis/nodes/SoPolyhedron.h>
00020 #include <HEPVis/nodes/SoHighlightMaterial.h>
00021
00022
00023 #include <Lib/Interfaces/IIterator.h>
00024 #include <Lib/Interfaces/ISession.h>
00025 #include <Lib/Variable.h>
00026 #include <Lib/smanip.h>
00027
00028
00029 #include <GaudiKernel/IParticlePropertySvc.h>
00030 #include <GaudiKernel/MsgStream.h>
00031 #include <GaudiKernel/ParticleProperty.h>
00032 #include <GaudiKernel/IDataProviderSvc.h>
00033 #include <GaudiKernel/SmartDataPtr.h>
00034
00035 #include <OnXSvc/IUserInterfaceSvc.h>
00036 #include <OnXSvc/Helpers.h>
00037
00038
00039 #include <Event/HepMCEvent.h>
00040 #include <HepMC/GenEvent.h>
00041
00043 GenParticleType::GenParticleType(
00044 IParticlePropertySvc* aParticlePropertySvc
00045 ,IDataProviderSvc* aDataProviderSvc
00046 ,IUserInterfaceSvc* aUISvc
00047 )
00048 :OnX::BaseType(aUISvc->printer())
00049 ,fType("GenParticle")
00050 ,fParticlePropertySvc(aParticlePropertySvc)
00051 ,fDataProviderSvc(aDataProviderSvc)
00052 ,fUISvc(aUISvc)
00055 {
00056 addProperty("particle",Lib::Property::STRING);
00057 addProperty("barcode",Lib::Property::INTEGER);
00058 addProperty("pdg_id",Lib::Property::INTEGER);
00059 addProperty("energy",Lib::Property::DOUBLE);
00060 addProperty("pt",Lib::Property::DOUBLE);
00061 addProperty("charge",Lib::Property::DOUBLE,6);
00062 addProperty("beg_vtx",Lib::Property::BOOLEAN);
00063 addProperty("end_vtx",Lib::Property::BOOLEAN);
00064 addProperty("beg_vtx_d",Lib::Property::DOUBLE);
00065 addProperty("end_vtx_d",Lib::Property::DOUBLE);
00066 addProperty("beg_vtx_barcode",Lib::Property::INTEGER);
00067 addProperty("end_vtx_barcode",Lib::Property::INTEGER);
00068 }
00070 std::string GenParticleType::name() const
00073 {
00074 return fType;
00075 }
00077 Lib::IIterator* GenParticleType::iterator(
00078 )
00079
00080
00081 {
00082 if(!fDataProviderSvc) return 0;
00083 SmartDataPtr<DataObject>
00084 smartDataObject(fDataProviderSvc,"/Event/McEventCollection");
00085 if(smartDataObject) {}
00086 DataObject* dataObject = 0;
00087 StatusCode sc =
00088 fDataProviderSvc->retrieveObject("/Event/Gen/HepMCEvents", dataObject);
00089 if(!sc.isSuccess()) return 0;
00090 LHCb::HepMCEvents* mcEvents = dynamic_cast<LHCb::HepMCEvents*>(dataObject);
00091 if(!mcEvents) return 0;
00092
00093 class Iterator : public Lib::IIterator {
00094 public:
00095 virtual Lib::Identifier object() {
00096 if(fIterator==fEvent.particles_end()) return 0;
00097 return *fIterator;
00098 }
00099 virtual void next() { ++fIterator;}
00100 virtual void* tag() { return 0;}
00101 public:
00102 Iterator(HepMC::GenEvent& aEvent):fEvent(aEvent) {
00103 fIterator = fEvent.particles_begin();
00104 }
00105 private:
00106 HepMC::GenEvent& fEvent;
00107 HepMC::GenEvent::particle_const_iterator fIterator;
00108 };
00109
00110 LHCb::HepMCEvents::iterator it;
00111 for(it=mcEvents->begin();it!=mcEvents->end();it++) {
00112
00113 HepMC::GenEvent* genEvent = (*it)->pGenEvt();
00114 return new Iterator(*genEvent);
00115 }
00116 return 0;
00117 }
00119 Lib::Variable GenParticleType::value(
00120 Lib::Identifier aIdentifier
00121 ,const std::string& aName
00122 ,void*
00123 )
00124
00125
00126 {
00127 HepMC::GenParticle* obj = (HepMC::GenParticle*)aIdentifier;
00128 if(aName=="id") {
00129 return Lib::Variable(printer(),(void*)obj);
00130 } else if(aName=="particle") {
00131 if(fParticlePropertySvc) {
00132 ParticleProperty* pp =
00133 fParticlePropertySvc->findByStdHepID((int)obj->pdg_id());
00134 if(pp) return Lib::Variable(printer(),pp->particle());
00135 }
00136 return Lib::Variable(printer(),std::string("nil"));
00137 } else if(aName=="barcode") {
00138 return Lib::Variable(printer(),obj->barcode());
00139 } else if(aName=="pdg_id") {
00140 return Lib::Variable(printer(),(int)(obj->pdg_id()));
00141 } else if(aName=="energy") {
00142 return Lib::Variable(printer(),obj->momentum().e());
00143 } else if(aName=="pt") {
00144 double px = obj->momentum().px();
00145 double py = obj->momentum().py();
00146 return Lib::Variable(printer(),sqrt(px*px+py*py));
00147 } else if(aName=="charge") {
00148 if(fParticlePropertySvc) {
00149 ParticleProperty* pp =
00150 fParticlePropertySvc->findByStdHepID((int)obj->pdg_id());
00151 if(pp) return Lib::Variable(printer(),pp->charge());
00152 }
00153 return Lib::Variable(printer(),0.);
00154
00155 } else if(aName=="beg_vtx_barcode") {
00156 HepMC::GenVertex* v = obj->production_vertex();
00157 return Lib::Variable(printer(),v? v->barcode() : 0);
00158 } else if(aName=="end_vtx_barcode") {
00159 HepMC::GenVertex* v = obj->end_vertex();
00160 return Lib::Variable(printer(),v? v->barcode() : 0);
00161 } else if(aName=="beg_vtx") {
00162 HepMC::GenVertex* v = obj->production_vertex();
00163 return Lib::Variable(printer(),v?true:false);
00164 } else if(aName=="beg_vtx_d") {
00165 HepMC::GenVertex* v = obj->production_vertex();
00166 if(v) return Lib::Variable(printer(),v->point3d().r());
00167 return Lib::Variable(printer(),0.);
00168 } else if(aName=="end_vtx") {
00169 HepMC::GenVertex* v = obj->end_vertex();
00170 return Lib::Variable(printer(),v?true:false);
00171 } else if(aName=="end_vtx_d") {
00172 HepMC::GenVertex* v = obj->end_vertex();
00173 if(v) return Lib::Variable(printer(),v->point3d().r());
00174 return Lib::Variable(printer(),0.);
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 } else {
00209 return Lib::Variable(printer());
00210 }
00211 }
00213 void GenParticleType::visualize(
00214 Lib::Identifier aIdentifier
00215 ,void*
00216 )
00217
00218
00219 {
00220 if(!aIdentifier) return;
00221 if(!fUISvc) return;
00222 SoRegion* soRegion = fUISvc->currentSoRegion();
00223 if(!soRegion) return;
00224
00225 ISession* session = fUISvc->session();
00226 std::string value;
00227 bool showDecay = false;
00228 if(session->parameterValue("modeling.showDecay",value))
00229 Lib::smanip::tobool(value,showDecay);
00230
00231 if(showDecay)
00232 representDecay((HepMC::GenParticle*)aIdentifier,
00233 session,soRegion,fParticlePropertySvc);
00234 else
00235 represent((HepMC::GenParticle*)aIdentifier,
00236 session,soRegion,fParticlePropertySvc);
00237
00238 }
00240 void GenParticleType::clear(
00241 )
00242
00243
00244 {
00245
00246 }
00248 void GenParticleType::represent(
00249 HepMC::GenParticle* aGenParticle
00250 ,ISession* aSession
00251 ,SoRegion* aRegion
00252 ,IParticlePropertySvc* aParticlePropertySvc
00253 )
00254
00255
00256 {
00257 HepMC::GenVertex* pv = aGenParticle->production_vertex();
00258 HepMC::GenVertex* ev = aGenParticle->end_vertex();
00259
00260 if(!pv && !ev) return;
00261
00262
00263
00264 double r = 0.5, g = 0.5, b = 0.5;
00265 double hr = 1.0, hg = 1.0, hb = 0.0;
00266 std::string value;
00267 if(aSession->parameterValue("modeling.color",value))
00268 Lib::smanip::torgb(value,r,g,b);
00269 if(aSession->parameterValue("modeling.highlightColor",value))
00270 Lib::smanip::torgb(value,hr,hg,hb);
00271 double lineWidth = 0;
00272 if(aSession->parameterValue("modeling.lineWidth",value))
00273 if(!Lib::smanip::todouble(value,lineWidth)) lineWidth = 0;
00274
00275 bool showText = false;
00276 aSession->parameterValue("modeling.showText",value);
00277 Lib::smanip::tobool(value,showText);
00278
00279
00280
00281 aSession->parameterValue("modeling.posText",value);
00282 std::string posTextAtb = value=="" ? "medium" : value;
00283
00284 double scale = 1;
00285 aSession->parameterValue("modeling.scale",value);
00286 if(!Lib::smanip::todouble(value,scale)) scale = 1;
00287
00288 HepMC::ThreeVector* pb;
00289 HepMC::ThreeVector* pe;
00290 HepMC::ThreeVector* vect;
00291 if(!pv && ev) {
00292 pe->set(scale * ev->point3d().x(),
00293 scale * ev->point3d().y(),
00294 scale * ev->point3d().z());
00295 vect->set(aGenParticle->momentum().px(),
00296 aGenParticle->momentum().py(),
00297 aGenParticle->momentum().pz());
00298 double factor = 10000/vect->r();
00299 pb->set( pe->x() - factor * vect->x(),
00300 pe->y() - factor * vect->y(),
00301 pe->z() - factor * vect->z()) ;
00302 } else if(pv && !ev) {
00303 pb->set(scale * pv->point3d().x(),
00304 scale * pv->point3d().y(),
00305 scale * pv->point3d().z());
00306 vect->set(aGenParticle->momentum().px(),
00307 aGenParticle->momentum().py(),
00308 aGenParticle->momentum().pz());
00309 double factor = 10000/vect->r();
00310 pe->set( pb->x() + factor * vect->x(),
00311 pb->y() + factor * vect->y(),
00312 pb->z() + factor * vect->z()) ;
00313 } else {
00314 pb->set(scale * pv->point3d().x(),
00315 scale * pv->point3d().y(),
00316 scale * pv->point3d().z());
00317 pe->set(scale * ev->point3d().x(),
00318 scale * ev->point3d().y(),
00319 scale * ev->point3d().z());
00320 }
00321
00322 SoSeparator* separator = new SoSeparator;
00323 separator->setName("sceneGraph");
00324
00325
00326 SoHighlightMaterial* highlightMaterial = new SoHighlightMaterial;
00327 highlightMaterial->setName("highlight");
00328 highlightMaterial->diffuseColor.setValue
00329 (SbColor((float)r,(float)g,(float)b));
00330 highlightMaterial->highlightColor.setValue
00331 (SbColor((float)hr,(float)hg,(float)hb));
00332
00333 separator->addChild(highlightMaterial);
00334
00335
00336 std::string s;
00337 Lib::smanip::printf(s,128,"GenParticle/0x%lx",(unsigned long)aGenParticle);
00338 SbName name(s.c_str());
00339
00340 SoDrawStyle* drawStyle = new SoDrawStyle;
00341 drawStyle->style.setValue(SoDrawStyle::LINES);
00342 drawStyle->linePattern.setValue(0xFFFF);
00343 drawStyle->lineWidth.setValue((float)lineWidth);
00344
00345 SoLightModel* lightModel = new SoLightModel;
00346 lightModel->model = SoLightModel::BASE_COLOR;
00347
00348 SbVec3f points[2];
00349 points[0].setValue((float)pb->x(),(float)pb->y(),(float)pb->z());
00350 points[1].setValue((float)pe->x(),(float)pe->y(),(float)pe->z());
00351
00352
00353 if(showText) {
00354 SbVec3f textPoint;
00355 if(posTextAtb=="medium") {
00356 textPoint = (points[0] + points[1])/2;
00357 } else {
00358 double posText;
00359 if(Lib::smanip::todouble(posTextAtb,posText)) {
00360 SbVec3f direction = points[1]-points[0];
00361 direction.normalize();
00362 direction *= (float)posText;
00363 textPoint = points[0] + direction;
00364 } else {
00365 textPoint = (points[0] + points[1])/2;
00366 }
00367 }
00368
00369 SoSeparator* sepText = new SoSeparator;
00370 SoTransform* tsf = new SoTransform;
00371 tsf->translation.setValue(textPoint);
00372
00373 std::string partname = "nil";
00374 ParticleProperty* pp =
00375 aParticlePropertySvc->findByStdHepID((int)aGenParticle->pdg_id());
00376 if(pp) partname = pp->particle();
00377
00378 SoFont* font = new SoFont();
00379 font->name.setValue("times");
00380 font->size.setValue(20.0F);
00381
00382 SoText2* text = new SoText2();
00383 text->string.set1Value(0,partname.c_str());
00384 text->setName(name);
00385
00386 sepText->addChild(tsf);
00387 sepText->addChild(font);
00388 sepText->addChild(text);
00389
00390 separator->addChild(sepText);
00391 }
00392
00393 separator->addChild(lightModel);
00394 separator->addChild(drawStyle);
00395
00396 float d = (points[1]-points[0]).length();
00397 if(d<=0) {
00398 SoTranslation* translation = new SoTranslation;
00399 translation->translation = points[0];
00400 separator->addChild(translation);
00401
00402 double radius = aGenParticle->momentum().e()/1000;
00403
00404 SbPolyhedron* sbPol = new SbPolyhedronBox(radius,radius,radius);
00405 SoPolyhedron* polyhedron = new SoPolyhedron(*sbPol);
00406 delete sbPol;
00407 polyhedron->setName(name);
00408 polyhedron->solid.setValue(FALSE);
00409 separator->addChild(polyhedron);
00410 } else {
00411 SoCoordinate3* coordinate3 = new SoCoordinate3;
00412 coordinate3->point.setValues(0,2,points);
00413 separator->addChild(coordinate3);
00414
00415 SoLineSet* lineSet = new SoLineSet;
00416 int32_t vertices = 2;
00417 lineSet->numVertices.setValues(0,1,&vertices);
00418 lineSet->setName(name);
00419 separator->addChild(lineSet);
00420 }
00421
00422 region_addToDynamicScene(*aRegion,separator);
00423
00424 }
00426 void GenParticleType::representDecay(
00427 HepMC::GenParticle* aGenParticle
00428 ,ISession* aSession
00429 ,SoRegion* aRegion
00430 ,IParticlePropertySvc* aParticlePropertySvc
00431 )
00432
00433
00434 {
00435
00436
00437 HepMC::GenVertex* ev = aGenParticle->end_vertex();
00438 if(!ev) return;
00439 HepMC::GenVertex::particle_iterator it;
00440 for(it=ev->particles_begin(HepMC::children);
00441 it!=ev->particles_end(HepMC::children);++it) {
00442 HepMC::GenParticle* particle = *it;
00443 if(particle) {
00444 represent(aGenParticle,aSession,aRegion,aParticlePropertySvc);
00445
00446 representDecay(particle,aSession,aRegion,aParticlePropertySvc);
00447 }
00448 }
00449
00450 }