Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:37

0001 // $Id:$
0002 //====================================================================
0003 //  AIDA Detector description implementation for LCD
0004 //--------------------------------------------------------------------
0005 //
0006 //  Standalone ROOT script to load geometries from DD4hep:
0007 //
0008 //  Full GDML cycle can be tested (deplace <detector> with some meaningful identifier):
0009 //   a) Extract gdml information
0010 //      $ > geoConverter -compact2gdml -input file:<compact-input-xml-file> -output <detector>.gdml
0011 //   b) Extract visualisation hints to csv file
0012 //      $ > geoConverter -compact2vis -input file:<compact-input-xml-file> -output <detector>.vis.csv -ascii
0013 //   c) Load GDML into ROOT (as AClick, but can also be interpreted):
0014 //      $ > root.exe ../doc/gdml_root.C+\(\"<detector>\"\)
0015 //      The macro expects *at least* the <detector>.gdml file. If no visualisation
0016 //      information is found, the resulting graphics however is not very nice!
0017 //
0018 //
0019 //  Author     : M.Frank
0020 //
0021 //====================================================================
0022 // C/C++ include files
0023 #include <fstream>
0024 #include <cerrno>
0025 #include <string>
0026 #include <sstream>
0027 
0028 // ROOT include files
0029 #include <TGeoManager.h>
0030 #include <TColor.h>
0031 #include <TObjArray.h>
0032 #include <TGeoVolume.h>
0033 #include <TGDMLParse.h>
0034 
0035 using namespace std;
0036 
0037 TGeoVolume* gdml_root(const char* sys_name) {
0038   bool         debug_processing = false;
0039   TGeoManager* geo = new TGeoManager();
0040   string       system    = sys_name;
0041   string       gdml_file = system+".gdml";
0042   string       vis_file  = system+".vis.csv";
0043   ifstream     in(vis_file.c_str());
0044   TGDMLParse   parse;
0045   gDebug = 2;
0046   cout << "++ Processing gdml file:" << gdml_file << endl;
0047 
0048   TGeoVolume* top_vol = parse.GDMLReadFile(gdml_file.c_str());
0049   TObjArray*  vols    = gGeoManager->GetListOfVolumes();
0050   int num_volumes = vols->GetEntries();
0051 
0052   if ( !in.good() )
0053     cout << "++ Failed to open visualization file:" << vis_file 
0054          << " :: " << strerror(errno) << endl;
0055   else
0056     cout << "++ Processing visualization file:" << vis_file << endl;
0057 
0058   while(in.good() ) {
0059     stringstream debug;
0060     char         text[1024], *line;
0061     const char*  vol_name="";
0062     float        red = 0, blue = 0, green = 0;
0063     bool         visible = false, show_daughters = false;
0064     string       line_style="solid", drawing_style="solid";
0065 
0066     in.getline(text,sizeof(text),'\n');
0067     line = strtok(text,";");
0068     debug << "+ Process:";
0069     for(int count=0; line != 0; ++count, line=::strtok(0,";"))  {
0070       debug << line << " ";
0071       switch(count) {
0072       case 0:
0073         vol_name = line+::strlen("vol:");
0074         break;
0075       case 1:
0076         break;
0077       case 2:
0078         line += ::strlen("visible:");
0079         visible = *line=='1';
0080         break;
0081       case 3:
0082         line += ::strlen("r:");
0083         red = atof(line);
0084         break;
0085       case 4:
0086         line += ::strlen("g:");
0087         green = atof(line);
0088         break;
0089       case 5:
0090         line += ::strlen("b:");
0091         blue = atof(line);
0092         break;
0093       case 6:
0094         line += ::strlen("alpha:");
0095         //alpha = atof(line);
0096         break;
0097       case 7:
0098         line_style = line+::strlen("line_style:");
0099         break;
0100       case 8:
0101         drawing_style = line+::strlen("drawing_style:");
0102         break;
0103       case 9:
0104         line += ::strlen("show_daughters:");
0105         show_daughters = *line=='1';
0106         break;
0107       default:
0108         break;
0109       }
0110     }
0111     if ( debug_processing ) cout << debug.str() << endl;
0112     debug.str("");
0113 
0114     if ( vol_name && ::strlen(vol_name) )   {
0115       TGeoVolume* volume = 0;
0116       for(int i=0;i<num_volumes;++i) {
0117         TGeoVolume* v=(TGeoVolume*)vols->At(i);
0118         if ( 0 == ::strcmp(vol_name,v->GetName()) ) {
0119           volume = v;
0120           break;
0121         }
0122       }
0123       if ( volume ) {
0124         int     color  = TColor::GetColor(red,green,blue);
0125         Color_t bright = TColor::GetColorBright(color);
0126         Color_t dark   = TColor::GetColorDark(color);
0127         debug << "+ \tr:" << red << " g:" << green << " b:" << blue << " col:" << color 
0128               << " line_style:" << line_style << " drawing_style:" << drawing_style 
0129               << " visible:" << visible << " show_daughters:" << show_daughters;
0130         volume->SetLineColor(dark);
0131         if ( drawing_style == "solid" )  {
0132           volume->SetFillColor(bright);
0133           volume->SetFillStyle(1001); // Root: solid
0134         }
0135         else {
0136           //volume->SetFillColor(bright);
0137           volume->SetFillColor(0);
0138           volume->SetFillStyle(0);    // Root: hollow
0139         }
0140         if ( line_style == "unbroken" )
0141           volume->SetFillStyle(1);
0142         else
0143           volume->SetFillStyle(2);
0144       
0145         volume->SetLineWidth(10);
0146         volume->SetVisibility(visible ? kTRUE : kFALSE);
0147         volume->SetAttBit(TGeoAtt::kVisContainers,kTRUE);
0148         volume->SetVisDaughters(show_daughters ? kTRUE : kFALSE);
0149       }
0150       else {
0151         cout << endl << "++ Failed to find volume with name:" << vol_name;
0152       }
0153     }
0154     if ( debug_processing ) cout << debug.str() << endl;
0155   }
0156   cout << "++ Closing geometry and starting display.... Top volume:" << (void*)top_vol << endl;
0157   geo->SetTopVolume(top_vol);
0158   geo->CloseGeometry();
0159   geo->SetVisLevel(4);
0160   geo->SetVisOption(1);
0161   top_vol->Draw("ogl");
0162   return top_vol;
0163 }