Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:46

0001 //==============================================================================
0002 // Material name/title "fixing"
0003 //==============================================================================
0004 
0005 #include "TGeoManager.h"
0006 #include "TGeoMaterial.h"
0007 #include "TGeoMedium.h"
0008 
0009 #include "TClass.h"
0010 #include "TList.h"
0011 #include "TPRegexp.h"
0012 
0013 TGeoManager* FWGeometryTableViewManager_GetGeoManager();
0014 
0015 TGeoElementTable *g_element_table = 0;
0016 
0017 void fw_simGeo_fix_materials()
0018 {
0019    Int_t base_element_offset = TGeoMaterial::Class()->GetDataMemberOffset("fElement");
0020 
0021    TString vacuum("materials:Vacuum");
0022 
0023    TGeoMaterial *m;
0024    TIter it(FWGeometryTableViewManager_GetGeoManager()->GetListOfMaterials());
0025    while ((m = (TGeoMaterial*) it()) != 0)
0026    {
0027       // Fixes
0028       if (vacuum == m->GetName())
0029       {
0030          m->SetZ(0);
0031       }
0032 
0033       TGeoMixture *mix = dynamic_cast<TGeoMixture*>(m);
0034       if (mix == 0)
0035       {
0036          if ( ! m->GetBaseElement())
0037          {
0038             *(TGeoElement**)(((char*)m) + base_element_offset) = g_element_table->GetElement(m->GetZ());
0039          }
0040       }
0041    }
0042 }
0043 
0044 void fw_simGeo_dump_materials(Bool_t dump_components=false)
0045 {
0046    TGeoMaterial *m;
0047    TIter it(FWGeometryTableViewManager_GetGeoManager()->GetListOfMaterials());
0048    while ((m = (TGeoMaterial*) it()) != 0)
0049    {
0050       TGeoMixture *mix = dynamic_cast<TGeoMixture*>(m);
0051       printf("%-50s | %-40s | %2d | %.3f\n", m->GetName(), m->GetTitle(),
0052              mix ? mix->GetNelements() : 0, m->GetZ());
0053       if (dump_components)
0054       {
0055          if (mix == 0)
0056          {
0057             printf("  %4d %6s %s\n", m->GetBaseElement()->Z(), m->GetBaseElement()->GetName(), m->GetBaseElement()->GetTitle());
0058          }
0059          else
0060          {
0061             Double_t *ww = mix->GetWmixt();
0062             for (Int_t i = 0; i < mix->GetNelements(); ++i)
0063             {
0064                TGeoElement *e = mix->GetElement(i);
0065                printf("  %4d %-4s %f\n",  e->Z(), e->GetName(), ww[i]);
0066             }
0067          }
0068       }
0069    }
0070 }
0071 
0072 void fw_simGeo_set_material_titles(Double_t fraction=0, Bool_t long_names=false)
0073 {
0074    TGeoMaterial *m;
0075    TIter it(FWGeometryTableViewManager_GetGeoManager()->GetListOfMaterials());
0076    while ((m = (TGeoMaterial*) it()) != 0)
0077    {
0078       TString tit(":");
0079       TGeoMixture *mix = dynamic_cast<TGeoMixture*>(m);
0080 
0081       if (mix == 0)
0082       {
0083          TGeoElement *e = m->GetBaseElement();
0084          tit += long_names ? e->GetTitle() : e->GetName();
0085          tit += ":";
0086       }
0087       else
0088       {
0089          Double_t *ww = mix->GetWmixt();
0090          for (Int_t i = 0; i < mix->GetNelements(); ++i)
0091          {
0092             if (ww[i] >= fraction)
0093             {
0094                TGeoElement *e = mix->GetElement(i);
0095                tit += long_names ? e->GetTitle() : e->GetName();
0096                tit += ":";
0097             }
0098          }
0099       }
0100       if (tit == ":") tit += ":";
0101       m->SetTitle(tit);
0102    }
0103 }
0104 
0105 //==============================================================================
0106 
0107 void fw_simGeo_set_volume_color_by_material(const char* material_re, Bool_t use_names, Color_t color, Char_t transparency=-1)
0108 {
0109    // Note: material_re is a perl regexp!
0110    // If you want exact match, enclose in begin / end meta characters (^ / $):
0111    //   set_volume_color_by_material("^materials:Silicon$", kRed);
0112 
0113    TPMERegexp re(material_re, "o");
0114    TGeoMaterial *m;
0115    TIter it(FWGeometryTableViewManager_GetGeoManager()->GetListOfMaterials());
0116    while ((m = (TGeoMaterial*) it()) != 0)
0117    {
0118       if (re.Match(use_names ? m->GetName() : m->GetTitle()))
0119       {
0120          if (transparency != -1)
0121          {
0122             m->SetTransparency(transparency);
0123          }
0124          TGeoVolume *v;
0125          TIter it2(FWGeometryTableViewManager_GetGeoManager()->GetListOfVolumes());
0126          while ((v = (TGeoVolume*) it2()) != 0)
0127          {
0128             if (v->GetMaterial() == m)
0129             {
0130                v->SetLineColor(color);
0131             }
0132          }
0133       }
0134    }
0135 }
0136 
0137 //==============================================================================
0138 
0139 void fw_simGeo_foos()
0140 {
0141    g_element_table = new TGeoElementTable;
0142    g_element_table->BuildDefaultElements();
0143 
0144    fw_simGeo_fix_materials();
0145    fw_simGeo_set_material_titles(0.01);
0146 }