29 #include "G4Material.hh"
32 #include "G4Polyhedra.hh"
33 #include "G4Sphere.hh"
35 #include "G4LogicalVolume.hh"
36 #include "G4AssemblyVolume.hh"
38 #include "G4PVPlacement.hh"
39 #include "G4PVParameterised.hh"
40 #include "G4UserLimits.hh"
41 #include "G4VisAttributes.hh"
42 #include "G4Colour.hh"
44 #include "G4SubtractionSolid.hh"
45 #include "G4PhysicalVolumeStore.hh"
46 #include "G4LogicalVolumeStore.hh"
47 #include "G4SDManager.hh"
48 #include "G4UnitsTable.hh"
55 #include "G4GDMLParser.hh"
58 using namespace SToGS;
63 G4int SToGS::DetectorFactory::gCopyNb = 0;
66 std::vector < SToGS::DetectorFactory * > SToGS::DetectorFactory::fSubFactory;
71 if ( pMainFactory == 0x0 ) {
75 SToGS::MaterialConsultant::theConsultant();
84 for (std::vector<SToGS::DetectorFactory *>::iterator it = fSubFactory.begin() ; it != fSubFactory.end(); ++it){
86 if ( path.contains(sub_name) ) {
97 G4VPhysicalVolume *l_top = top;
99 && G4TransportationManager::GetTransportationManager()
100 && G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()) {
101 l_top = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
107 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> phycical_stored; std::vector<G4VPhysicalVolume *> phycical_active;
109 CollectVolumes(l_top, logical_stored, phycical_stored, phycical_active);
111 for (
size_t i = 0; i < logical_stored.size(); i++) {
113 if ( logical_stored[i]->GetSensitiveDetector() )
114 SetActive(logical_stored[i], opt);
137 for (std::vector<SToGS::DetectorFactory *>::iterator it = fSubFactory.begin() ; it != fSubFactory.end(); ++it) {
138 G4cout <<
"[+..] BUILDING Store " << (*it)->GetFactoryName() << G4endl;
140 G4cout <<
"[..+] BUILDING Store " << (*it)->GetFactoryName() << G4endl;
147 std::ifstream gdml_in(GetFileName(GetDetName(name,version_string),
"gdml").data());
148 if ( gdml_in.is_open() ) {
150 G4cout <<
"[===] Detector " << GetDetName(name,version_string) <<
" already in store " << G4endl;
153 G4cout <<
"[+..] Building Detector " << GetDetName(name,version_string) << G4endl;
154 G4VPhysicalVolume *theDetector = Make(name, version_string);
158 G4cout <<
"[..+] Building Detector " << GetDetName(name,version_string) << G4endl;
165 result = GetFactoryName(); result += detname; result +=
"."; result += extention;
173 size_t where = dname.find(
":");
174 if ( where != std::string::npos ) {
175 std::string tmp = dname.substr( where );
182 std::vector<G4LogicalVolume *> &logical_stored,
183 std::vector<G4VPhysicalVolume *> &physical_stored, std::vector<G4VPhysicalVolume *> &physical_active,
184 G4VPhysicalVolume *theDetector)
186 G4LogicalVolume *alogical = theDetector->GetLogicalVolume(); G4VPhysicalVolume *aphysical = 0x0;
187 if ( alogical == 0x0 )
191 G4bool in_the_list =
false;
192 for (
size_t i = 0; i < logical_stored.size() ; i++) {
193 if (alogical == logical_stored[i] )
196 if ( !in_the_list ) {
200 if ( alogical->GetSensitiveDetector() ) {
201 sd += (alogical->GetSensitiveDetector())->GetFullPathName();
206 const G4VisAttributes *att = alogical->GetVisAttributes(); G4Color color(0.8,0.8,0.8,0);
208 color = att->GetColor();
210 amap << alogical->GetName()
226 logical_stored.push_back(alogical);
231 physical_stored.push_back(theDetector);
234 G4int is_to_be_removed_from_active =
false;
235 if ( theDetector->GetMotherLogical() && physical_active.size() == 0 ) {
236 physical_active.push_back(theDetector);
237 is_to_be_removed_from_active =
true;
240 if ( theDetector->GetLogicalVolume()->GetSensitiveDetector() ) {
242 if ( is_to_be_removed_from_active ==
false ) {
243 physical_active.push_back(theDetector);
244 is_to_be_removed_from_active =
true;
248 G4int offset = 0, global_unique_copy_number = 0;
249 G4String first_physical = physical_active[0]->GetName();
251 for (
size_t i = 0; i < physical_active.size() ; i++) {
254 offset = physical_active[i]->GetCopyNo();
258 if ( physical_active.size() > 1 ) {
259 global_unique_copy_number = offset + theDetector->GetCopyNo();
262 global_unique_copy_number = theDetector->GetCopyNo();
265 for (
size_t i = 0; i < physical_stored.size() ; i++) {
266 ToTr += physical_stored[i]->GetTranslation();
268 dmap << setw(5) << setfill(
'0') << global_unique_copy_number <<
"\t"
269 << physical_active[0]->GetName() <<
"\t" << physical_active[0]->GetCopyNo() <<
"\t"
270 << theDetector->GetName() <<
"\t" << theDetector->GetCopyNo() <<
"\t" ;
277 if ( ToTr.getX() < 0.0 )
278 dmap << ToTr.getX()/CLHEP::cm <<
" ";
280 dmap <<
"+" << ToTr.getX()/CLHEP::cm <<
" ";
282 if ( ToTr.getY() < 0.0 )
283 dmap << ToTr.getY()/CLHEP::cm <<
" ";
285 dmap <<
"+" << ToTr.getY()/CLHEP::cm <<
" ";
287 if ( ToTr.getZ() < 0.0 )
288 dmap << ToTr.getZ()/CLHEP::cm <<
" ";
290 dmap <<
"+" << ToTr.getZ()/CLHEP::cm <<
" ";
293 dmap <<
" "<< std::endl;
323 for (G4int i = 0; i < alogical->GetNoDaughters(); i++) {
324 aphysical = alogical->GetDaughter(i);
325 StoreMap(amap, dmap, logical_stored, physical_stored, physical_active, aphysical );
328 physical_stored.pop_back();
329 if ( is_to_be_removed_from_active ) {
330 physical_active.pop_back();
335 std::vector<G4LogicalVolume *> &logical_stored,
336 std::vector<G4VPhysicalVolume *> &phycical_stored, std::vector<G4VPhysicalVolume *> &physical_active)
338 G4LogicalVolume *alogical = theDetector->GetLogicalVolume(); G4VPhysicalVolume *aphysical = 0x0; G4bool in_the_list;
341 for (
size_t i = 0; i < phycical_stored.size() ; i++) {
342 if ( aphysical == phycical_stored[i] ) {
347 if ( !in_the_list ) {
348 phycical_stored.push_back(theDetector);
349 if ( theDetector->GetLogicalVolume()->GetSensitiveDetector() ) {
350 physical_active.push_back(theDetector);
354 for (
size_t i = 0; i < logical_stored.size() ; i++) {
355 if ( alogical == logical_stored[i] ) {
360 if ( !in_the_list ) {
361 logical_stored.push_back(alogical);
365 for (G4int i = 0; i < alogical->GetNoDaughters(); i++) {
366 aphysical = alogical->GetDaughter(i);
367 CollectVolumes(aphysical,logical_stored, phycical_stored, physical_active);
375 G4String filename = GetFileName(theDetector->GetName(),
"gdml");
377 std::ifstream gdml_in(filename.data());
378 if ( gdml_in.is_open() ) {
380 G4cout <<
" file already exiting " << filename.data() << G4endl;
386 parser.Write(filename.data(),theDetector,
false);
390 filename = GetFileName(theDetector->GetName(),
"amap"); std::ofstream amap(filename.data());
391 filename = GetFileName(theDetector->GetName(),
"dmap"); std::ofstream dmap(filename.data());
393 if ( amap.is_open() && dmap.is_open() ) {
395 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> physical_stored, physical_active;
397 for (G4int i = 0; i < theDetector->GetLogicalVolume()->GetNoDaughters(); i++) {
398 StoreMap(amap, dmap, logical_stored, physical_stored, physical_active, theDetector->GetLogicalVolume()->GetDaughter(i) );
408 G4int start = path.last(
'/');
409 tmp.remove(0,start+1);
410 start = tmp.last(
'.');
418 G4String detname = name;
420 if ( version_string !=
"" ) {
422 detname += version_string;
432 std::vector < G4VPhysicalVolume* >::iterator vol = assembly->GetVolumesIterator();
433 for (
size_t i = 0; i < assembly->TotalImprintedVolumes(); i++) {
434 G4VPhysicalVolume *an_element = *vol++;
436 tmp = (an_element)->GetName();
437 if ( tmp.find(volume_to_find) != std::string::npos ) {
439 G4int av, impr, pv, nbread;
char *tmpname =
new char[tmp.size()];
442 tmp.replace(tmp.find(
"_pv"), 3, std::string(
" _pv"));
443 nbread = sscanf(tmp.data(),
"av_%d_impr_%d_%s _pv_%d",&av,&impr,tmpname,&pv);
445 G4cout <<
" Volume " << (an_element)->GetName() <<
", with copy number " << an_element->GetCopyNo() <<
" " << (
void*)(an_element->GetLogicalVolume()) << G4endl;;
446 sprintf(tmpname,
"%s_%d",tmpname,--impr);
447 (an_element)->SetName(tmpname);
448 G4cout <<
" |--> becomes " << (an_element)->GetName() << G4endl;
456 G4VPhysicalVolume *volume_used_to_built_assembly, G4int copy_number_offset)
const
459 G4cout <<
"[+] Results of snapshots of " << volume_used_to_built_assembly->GetName() << endl;
464 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> physical_stored; std::vector<G4VPhysicalVolume *> physical_active;
466 CollectVolumes(volume_used_to_built_assembly, logical_stored, physical_stored, physical_active);
468 std::vector<G4int > volume_counter; volume_counter.resize(physical_stored.size(),0);
472 std::vector < G4VPhysicalVolume* >::iterator vol = assembly->GetVolumesIterator();
473 for (
size_t i = 0; i < assembly->TotalImprintedVolumes(); i++) {
475 G4VPhysicalVolume *an_element = *vol++;
476 tmp = (an_element)->GetName();
479 G4int av, impr, pv, nbread;
char *tmpname =
new char[tmp.size()];
480 tmp.replace(tmp.find(
"_pv"), 3, std::string(
" _pv"));
481 nbread = sscanf(tmp.data(),
"av_%d_impr_%d_%s _pv_%d",&av,&impr,tmpname,&pv);
487 for (
size_t j = 1; j < physical_stored.size(); j++) {
488 if ( an_element->GetLogicalVolume() == physical_stored[j]->GetLogicalVolume() ) {
489 if ( impr == volume_counter[j] ) {
498 hname << volume_used_to_built_assembly->GetName() <<
":" << setw(3) << setfill(
'0') << impr <<
":" << physical_stored[keep_j]->GetName() ;
499 if ( physical_stored[keep_j]->GetLogicalVolume()->GetNoDaughters() == 0 ) {
500 if ( physical_stored[keep_j]->GetLogicalVolume()->GetSensitiveDetector() ) {
501 (an_element)->SetName(hname.str());
502 (an_element)->SetCopyNo( copy_number_offset + impr*physical_active.size() + physical_stored[keep_j]->GetCopyNo() );
503 G4cout <<
" Set Copy Number of Imprinted Physical Volume " << (an_element)->GetName() <<
" to " << (an_element)->GetCopyNo() << G4endl;
506 (an_element)->SetName(hname.str());
507 (an_element)->SetCopyNo( physical_stored[keep_j]->GetCopyNo() );
508 G4cout <<
" Set Copy Number of Imprinted Physical Volume " << (an_element)->GetName() <<
" to " << (an_element)->GetCopyNo() << G4endl;
512 (an_element)->SetName(hname.str());
513 (an_element)->SetCopyNo( copy_number_offset + impr*physical_active.size() );
514 G4cout <<
" Set Copy Number of Imprinted Physical Volume " << (an_element)->GetName() <<
" to " << (an_element)->GetCopyNo() << G4endl;
520 G4cout <<
"[-] Results of snapshots of " << volume_used_to_built_assembly->GetName() << endl;
522 return physical_active.size();
528 std::string lopt = opt, tmp; G4bool ok =
false;
534 std::vector < std::string > all_opt; std::vector < size_t > pos;
size_t start = 0;
536 for (
size_t i = 0; i < lopt.size(); i++) {
538 if ( lopt[i] ==
'^' ) {
540 tmp = lopt.substr(start,i-start); start = i+1;
543 for (
size_t j = 0; j < tmp.size(); j++) {
544 if ( tmp[j] ==
'|' ) {
548 if ( pos.size() == 2 ) {
550 pos.push_back( tmp.size() - 1 );
552 if ( pos.size() == 3 ) {
554 G4String volumename = tmp.substr(0,pos[0]);
555 G4String matname = tmp.substr(pos[0]+1,pos[1]-pos[0]-1);
556 G4String sdname = tmp.substr(pos[1]+1,pos[2]-pos[1]-1);
557 G4String color = tmp.substr(pos[2]+1);
564 if ( volumename.find(
"*") == std::string::npos ) {
565 if ( pv->GetName().find(volumename) != std::string::npos )
570 if ( matname.find(
"*") == std::string::npos ) {
571 if ( pv->GetMaterial()->GetName().find(matname) != std::string::npos )
576 G4VisAttributes *visatt = 0x0; G4int valid_col = 0; G4double r = 0.5, g = 0.5, b = 0.5, a = 0.5;
577 for (
size_t k = 0; k < color.size(); k++) {
578 if ( color[k] ==
';' ) {
583 if ( valid_col == 3 ) {
584 istringstream col(color);
585 col >> r >> g >> b >> a;
587 visatt =
new G4VisAttributes( G4Colour(r, g, b, a) );
588 pv->SetVisAttributes( visatt );
590 if ( pv->GetSensitiveDetector() == 0x0 ) {
591 visatt =
new G4VisAttributes( G4Colour(r, g, b, a) );
592 pv->SetVisAttributes( visatt );
596 G4VSensitiveDetector *sensdet = GetSD(sdname);
605 pv->SetSensitiveDetector(sensdet);
606 G4cout << pv->GetName() <<
" is now sensitive of type " << sensdet->GetFullPathName() << G4endl;
617 G4VSensitiveDetector *sensdet = 0x0;
620 sensdet = G4SDManager::GetSDMpointer()->FindSensitiveDetector(sdname,
false);
621 if ( sensdet == 0x0 ) {
622 if ( sd_type ==
'S' ) {
623 if ( sdname ==
"/SToGS/SD/CopCluster")
625 if ( sdname ==
"/SToGS/SD/Tracker")
628 if ( sd_type ==
's' ) {
632 G4cout <<
"[i] " << sensdet->GetFullPathName() <<
" has been added to the list of SD detector" << G4endl;
640 G4VPhysicalVolume *theDetector = 0x0;
643 std::ifstream isgdml_in(gdmlfile.data());
644 if ( isgdml_in.is_open() ) {
651 cout <<
"[+..] Importing " << gdmlfile <<
" in " << GetFactoryName() << detname << endl;
654 parser.Read(gdmlfile,
false);
656 theDetector = parser.GetWorldVolume();
660 theDetector->SetName(detname);
661 theDetector->GetLogicalVolume()->SetName(detname);
664 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> phycical_stored; std::vector<G4VPhysicalVolume *> phycical_active;
666 CollectVolumes(theDetector, logical_stored, phycical_stored, phycical_active);
668 for (
size_t i = 0; i < logical_stored.size(); i++) {
669 SetActive(logical_stored[i], opt_amap);
671 for (
size_t i = 0; i < phycical_stored.size(); i++) {
673 if ( phycical_stored[i]->GetLogicalVolume()->GetSensitiveDetector() ) {
677 phycical_stored[i]->SetCopyNo(-1);
679 if ( opt_dmap ==
"T" && phycical_stored[i] != theDetector && phycical_stored[i]->GetCopyNo() >=0 ) {
681 tmp << phycical_stored[i]->GetName() <<
":" << phycical_stored[i]->GetCopyNo();
683 phycical_stored[i]->SetName(tmp.str());
688 cout <<
"[..+] Importing " << gdmlfile <<
" in store " << GetFactoryName() <<
" with name " << detname << endl;
695 G4VPhysicalVolume *theDetector = 0x0; G4String detname, fullname;
701 for (
size_t i = 0; i < fLoadedPhysical.size(); i++) {
702 if ( fLoadedPhysical[i].first == fullname ) {
703 theDetector = fLoadedPhysical[i].second;
712 std::ifstream isgdml_in(fullname.data());
713 if ( isgdml_in.is_open() ) {
720 cout <<
"[+..] Loading from store " << basename << endl;
723 parser.Read(fullname,
false);
725 detname = GetDetName(fullname); theDetector = parser.GetWorldVolume(detname);
726 if ( theDetector == 0x0 ) {
731 std::pair < G4String, G4VPhysicalVolume *> p(fullname,theDetector);
732 fLoadedPhysical.push_back(p);
735 G4String phy_name = theDetector->GetName();
736 size_t pos = phy_name.find(
"_PV");
737 phy_name.erase(pos,3);
739 theDetector->SetName(phy_name);
741 GetAttributes(basename,
true,
true);
743 G4cout <<
"[..+] Loading from store " << basename << G4endl;
750 G4VPhysicalVolume *theDetector; G4String detname, fullname;
752 theDetector = Get(basename);
753 if ( theDetector == 0x0 )
756 if ( do_amap ==
false && do_amap ==
false ) {
760 G4cout <<
"[+..] Loading Attributes from store " << basename << endl;
763 fullname = basename; fullname +=
".dmap"; std::ifstream dmap(fullname.data());
764 fullname = basename; fullname +=
".amap"; std::ifstream amap(fullname.data());
766 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> phycical_stored; std::vector<G4VPhysicalVolume *> physical_active;
767 G4String aline; G4bool has_done_amap =
false;
769 CollectVolumes(theDetector, logical_stored, phycical_stored, physical_active);
773 if ( amap.is_open() ) {
776 while ( amap.good() && !amap.eof() ) {
778 istringstream decode(aline);
779 G4String vname, key_sd, sd, key_color, touchable; G4double r,g,b,a;
791 for (
size_t i = 0; i < logical_stored.size(); i++) {
792 if ( vname == logical_stored[i]->GetName() ) {
795 G4VisAttributes visatt(G4Color(r,g,b,a));
796 if ( key_color ==
"C" )
797 visatt.SetVisibility(
true);
799 visatt.SetVisibility(
false);
801 logical_stored[i]->SetVisAttributes(visatt);
805 G4VSensitiveDetector * sensdet = GetSD(sd);
808 logical_stored[i]->SetSensitiveDetector(sensdet);
821 fullname = basename; fullname +=
".amap"; std::ofstream amap_(fullname.data());
823 if ( amap_.is_open() ) {
824 has_done_amap =
true;
825 for (
size_t i = 0; i < logical_stored.size(); i++) {
832 amap_ << logical_stored[i]->GetName()
854 if ( dmap.is_open() ) {
856 for (
size_t i = 0; i < phycical_stored.size(); i++) {
857 phycical_stored[i]->SetCopyNo(-1);
862 while ( dmap.good() && !dmap.eof()) {
864 G4String pname, unit, firstpname; G4double x,y,z; G4int uid, top_id, id;
865 istringstream decode(aline);
879 G4cout <<
" Load Copy number of " << firstpname <<
" [" << top_id <<
"] and " << pname <<
" [" <<
id <<
"] " << G4endl;
882 for (
size_t i = 0; i < phycical_stored.size(); i++) {
883 if ( firstpname == phycical_stored[i]->GetName() ) {
886 phycical_stored[i]->SetCopyNo(top_id);
888 if ( pname == phycical_stored[i]->GetName() ) {
891 phycical_stored[i]->SetCopyNo(
id);
902 for (
size_t i = 0; i < phycical_stored.size(); i++) {
903 phycical_stored[i]->SetCopyNo(-1);
906 if ( !has_done_amap ) {
908 fullname = basename; fullname +=
".dmap"; std::ofstream dmap_(fullname.data());
910 for (
size_t i = 0; i < phycical_stored.size(); i++) {
911 if ( phycical_stored[i]->GetLogicalVolume()->GetSensitiveDetector() ) {
912 cout <<
" Change Name and Copy number of "
913 << phycical_stored[i]->GetName() <<
" -> "
919 phycical_stored[i]->SetName(tmp.str());
922 if ( dmap_.is_open() ) {
923 dmap_ << setw(5) << setfill(
'0')
924 << phycical_stored[i]->GetCopyNo() <<
"\t"
925 << phycical_stored[i]->GetName() <<
"\t";
926 StreamTouchable(dmap_, phycical_stored[i]->GetName()); dmap_ <<
"\t";
928 if ( phycical_stored[i]->GetTranslation().getX() < 0.0 )
929 dmap_ << phycical_stored[i]->GetTranslation().getX()/CLHEP::cm <<
" ";
931 dmap_ <<
"+" << phycical_stored[i]->GetTranslation().getX()/CLHEP::cm <<
" ";
933 if ( phycical_stored[i]->GetTranslation().getY() < 0.0 )
934 dmap_ << phycical_stored[i]->GetTranslation().getY()/CLHEP::cm <<
" ";
936 dmap_ <<
"+" << phycical_stored[i]->GetTranslation().getY()/CLHEP::cm <<
" ";
938 if ( phycical_stored[i]->GetTranslation().getZ() < 0.0 )
939 dmap_ << phycical_stored[i]->GetTranslation().getZ()/CLHEP::cm <<
" ";
941 dmap_ <<
"+" << phycical_stored[i]->GetTranslation().getZ()/CLHEP::cm <<
" ";
944 dmap_ <<
" "<< std::endl;
951 cout <<
"[..+] Loading Attributes from store " << basename << endl;
957 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> physical_stored, physical_active;
958 CollectVolumes(adetector, logical_stored, physical_stored, physical_active);
960 for (
size_t i = 0; i < physical_active.size(); i++) {
961 G4cout <<
"---> Changing Copy Number of Detector " << physical_active[i]->GetName() <<
" from " << physical_active[i]->GetCopyNo()
962 <<
" to " << offset + i << G4endl;;
963 physical_active[i]->SetCopyNo(offset + i);
965 return G4int(physical_active.size());
969 G4int top_copy_number_offset,
const G4ThreeVector *T,
const G4RotationMatrix *R, G4int imprint_number)
971 G4VPhysicalVolume *thefullDetector = 0x0, *subdetector ; G4LogicalVolume *volume_to_copy;
974 thefullDetector = Get(basename);
975 if ( thefullDetector == 0x0 ) {
978 else { volume_to_copy = thefullDetector->GetLogicalVolume(); }
981 std::vector<G4LogicalVolume *> logical_stored; std::vector<G4VPhysicalVolume *> phycical_stored; std::vector<G4VPhysicalVolume *> phycical_active;
983 CollectVolumes(thefullDetector, logical_stored, phycical_stored, phycical_active);
986 for (G4int i = 0; i < volume_to_copy->GetNoDaughters(); i++) {
989 subdetector = volume_to_copy->GetDaughter(i);
1007 if ( dynamic_cast<G4PVPlacement *>(subdetector) == 0x0 ) {
1008 G4cout <<
" Current limitation in setting this kind of physical volume " << subdetector->GetName() << G4endl;
1013 ostringstream hname;
1014 hname << thefullDetector->GetName() <<
":" << setw(3) << setfill(
'0') << imprint_number <<
":" << subdetector->GetName() ;
1043 G4Transform3D Ta(subdetector->GetObjectRotationValue(),subdetector->GetObjectTranslation());
1045 const G4RotationMatrix *pRot = R;
1048 const_cast<G4RotationMatrix*
>( &G4RotationMatrix::IDENTITY );
1050 G4Transform3D Tm(*pRot,*T); G4Transform3D Tf = Tm * Ta;
1052 if ( subdetector->GetLogicalVolume()->GetNoDaughters() == 0 ) {
1053 if ( subdetector->GetLogicalVolume()->GetSensitiveDetector() ) {
1054 new G4PVPlacement(Tf,
1055 subdetector->GetLogicalVolume(),
1057 mother->GetLogicalVolume(),
1059 top_copy_number_offset + subdetector->GetCopyNo() );
1060 G4cout <<
"---> Add to " << mother->GetName() <<
" " << hname.str() <<
" with top copy number "
1061 << top_copy_number_offset + subdetector->GetCopyNo()<< G4endl;
1064 new G4PVPlacement(Tf,
1065 subdetector->GetLogicalVolume(),
1067 mother->GetLogicalVolume(),
1070 G4cout <<
"---> Add to " << mother->GetName() <<
" " << hname.str() <<
" with top copy number -1 " << G4endl;
1075 new G4PVPlacement(Tf,
1076 subdetector->GetLogicalVolume(),
1078 mother->GetLogicalVolume(),
1080 top_copy_number_offset + subdetector->GetCopyNo() );
1081 G4cout <<
"---> Add to " << mother->GetName() <<
" " << hname.str() <<
" with top copy number "
1082 << top_copy_number_offset + subdetector->GetCopyNo()<< G4endl;
1085 return phycical_active.size();
1089 G4int
SToGS::DetectorFactory::Set(G4String basename, G4VPhysicalVolume *world, G4int copy_number_offset,
const G4Transform3D *Tr,G4int main_copy_number)
1092 G4ThreeVector T = Tr->getTranslation(); G4RotationMatrix R = Tr->getRotation().inverse();
1093 return Set(basename,world,copy_number_offset,&T,&R,main_copy_number);
1095 else return Set(basename,world,copy_number_offset,0x0,0x0,main_copy_number);
1101 G4AssemblyVolume *theAssembly = 0x0; G4VPhysicalVolume *theDetector = 0x0, *subdetector; G4String detname, fullname;
1103 fullname = basename;
1104 fullname +=
".gdml";
1107 for (
size_t i = 0; i < fLoadedAssembly.size(); i++) {
1108 if ( fLoadedAssembly[i].first == fullname ) {
1109 theAssembly = fLoadedAssembly[i].second;
1114 if ( theAssembly == 0x0 ) {
1115 theDetector = Get(basename);
1116 if (theDetector == 0x0 ) {
1119 theAssembly =
new G4AssemblyVolume();
1121 std::pair < G4String, G4AssemblyVolume *> p(fullname,theAssembly);
1122 fLoadedAssembly.push_back(p);
1125 for (G4int i = 0; i < theDetector->GetLogicalVolume()->GetNoDaughters(); i++) {
1126 subdetector = theDetector->GetLogicalVolume()->GetDaughter(i);
1129 G4Transform3D Tr = G4Transform3D(subdetector->GetObjectRotationValue(),subdetector->GetObjectTranslation());
1130 theAssembly->AddPlacedVolume( subdetector->GetLogicalVolume(), Tr );
1138 if (
this == DetectorFactory::theMainFactory() ) {
1139 for (
size_t i = 0; i < fSubFactory.size(); i++) {
1140 fSubFactory[i]->Clean();
1142 G4LogicalVolumeStore::Clean(); G4PhysicalVolumeStore::Clean();
1146 for (
size_t i = 0; i < fLoadedPhysical.size(); i++) {
1147 fLoadedPhysical[i].second = 0x0;
1149 fLoadedPhysical.resize(0);
1150 for (
size_t i = 0; i < fLoadedAssembly.size(); i++) {
1151 delete fLoadedAssembly[i].second; fLoadedAssembly[i].second = 0x0;
1153 fLoadedAssembly.resize(0);
1160 G4VPhysicalVolume *theRoom = 0x0; G4LogicalVolume *logicRoom; G4Box *room;
1166 room=
new G4Box(name,HalfX,HalfY,HalfZ);
1167 logicRoom=
new G4LogicalVolume(room, MaterialConsultant::theConsultant()->FindOrBuildMaterial(
"AIR"), name, 0, 0, 0);
1169 logicRoom->SetVisAttributes(G4VisAttributes::Invisible);
1172 theRoom =
new G4PVPlacement(0,
1186 G4VPhysicalVolume *theDetector = 0x0; std::vector< std::pair< G4AssemblyVolume *,G4VPhysicalVolume * > > all_assembly; G4bool is_new_assembly =
false;
1189 std::ifstream g4map(input_file.data());
1191 if ( ! g4map.is_open() ) {
1196 G4String key, detector_name, subdetector_name, aline, unit1, what, option_copy_number;
1197 G4double X, Y, Z, r_value;
1200 getline(g4map,aline);
1201 while ( g4map.good() ) {
1203 istringstream decode(aline); decode.clear(); G4bool is_Tr =
false;
1207 if ( key.size() == 0 || key[0] ==
'#' ) {
1208 getline(g4map,aline);
1213 G4int offset; decode >> offset >> option_copy_number;
1217 getline(g4map,aline);
1222 G4String gdmlfile, fullfactoryname, option_amap(
"*|*|*|0.5;0.5;0.5;0.5"), option_dmap(
"");
1223 decode >> gdmlfile >> fullfactoryname >> option_amap >> option_dmap;
1228 theDetector = factory->
Import(gdmlfile, detector_name, option_amap, option_dmap);
1230 getline(g4map,aline);
1234 if ( key ==
"[" && theDetector ) {
1235 is_new_assembly =
true;
1236 getline(g4map,aline);
1239 if ( key ==
"]" && theDetector ) {
1242 for (
size_t i = 0; i < all_assembly.size(); i++ ) {
1246 all_assembly.clear(); is_new_assembly =
false;
1247 getline(g4map,aline);
1251 decode >> subdetector_name ;
1253 if ( key ==
"w" && decode.good() ) {
1257 if ( theDetector == 0x0 ) {
1259 decode >> X >> Y >> Z >> unit1 ;
1261 G4UnitDefinition::GetValueOf(unit1);
1266 if ( key ==
"+" && decode.good() && theDetector ) {
1268 G4RotationMatrix *R =
new G4RotationMatrix();
1270 decode >> X >> Y >> Z >> unit1 ;
1272 G4UnitDefinition::GetValueOf(unit1);
1273 G4ThreeVector T(do_unit*X,do_unit*Y,do_unit*Z);
1275 while ( !decode.eof() ) {
1277 if ( what ==
"Rx" ) {
1279 R->rotateX(r_value*CLHEP::deg);
1281 if ( what ==
"Ry" ) {
1283 R->rotateY(r_value*CLHEP::deg);
1285 if ( what ==
"Rz" ) {
1287 R->rotateZ(r_value*CLHEP::deg);
1289 if ( what ==
"Rt" ) {
1292 if ( what ==
"Tr" ) {
1298 if ( where_to_load ) {
1301 if ( nb_added < 0 ) {
1302 cout <<
"[+..] Reading " << input_file << endl;
1303 cout <<
" *** !! *** " << subdetector_name <<
" DOES NOT EXIST IN THE FACTORY " << where_to_load->
GetFactoryName() << endl;
1304 cout <<
"[..+] Reading " << input_file << endl;
1310 cout <<
"[+..] Reading " << input_file << endl;
1311 cout <<
" *** !! *** THE FACTORY DOES NOT EXIST " << endl;
1312 cout <<
"[..+] Reading " << input_file << endl;
1316 if ( key ==
"*" && decode.good() && theDetector && is_new_assembly ) {
1323 decode >> X >> Y >> Z >> unit1 ;
1325 G4UnitDefinition::GetValueOf(unit1);
1326 G4ThreeVector T(do_unit*X,do_unit*Y,do_unit*Z);
1330 for (
size_t i = 0; i < all_assembly.size(); i++ ) {
1331 if ( assembly == all_assembly[i].first ) {
1337 all_assembly.push_back( std::pair< G4AssemblyVolume *, G4VPhysicalVolume *>(assembly,detector) );
1340 G4RotationMatrix *R =
new G4RotationMatrix();
1342 while ( !decode.eof() ) {
1344 if ( what ==
"Rx" ) {
1346 R->rotateX(r_value*CLHEP::deg);
1348 if ( what ==
"Ry" ) {
1350 R->rotateY(r_value*CLHEP::deg);
1352 if ( what ==
"Rz" ) {
1354 R->rotateZ(r_value*CLHEP::deg);
1356 if ( what ==
"Rt" ) {
1359 if ( what ==
"Tr" ) {
1365 assembly->MakeImprint( theDetector->GetLogicalVolume(), T, R );
1369 getline(g4map,aline);
1377 #include "G4UIdirectory.hh"
1378 #include "G4UIcmdWithAString.hh"
1380 DFMessenger::DFMessenger():
1383 cmdToSaveInFactory(0x0)
1385 theDirectory =
new G4UIdirectory(
"/DetectoryFactory/");
1386 theDirectory->SetGuidance(
"To manage the DetectoryFactory");
1388 cmdToChangeSD =
new G4UIcmdWithAString(
"/DetectoryFactory/changeSD",
this);
1389 cmdToChangeSD->SetGuidance(
"Change Sensitivity of Sensitive detectors ");
1390 cmdToChangeSD->SetGuidance(
"Requires one string having the form XXX:YYY:ZZZ ");
1391 cmdToChangeSD->SetGuidance(
" \t XXX is the volume name, could be * for all");
1392 cmdToChangeSD->SetGuidance(
" \t YYY is the mat name, could be * ");
1393 cmdToChangeSD->SetGuidance(
" \t ZZZ is the name of the new SD ");
1395 cmdToChangeSD->AvailableForStates(G4State_PreInit,G4State_Idle);
1397 cmdToSaveInFactory =
new G4UIcmdWithAString(
"/DetectoryFactory/Save",
this);
1398 cmdToSaveInFactory->SetGuidance(
"To save the current setup in MyStore ");
1399 cmdToSaveInFactory->SetGuidance(
"Requires one string which is the name of the setup to be stored");
1400 cmdToSaveInFactory->AvailableForStates(G4State_PreInit,G4State_Idle);
1405 delete theDirectory;
1411 if( command == cmdToChangeSD ) {
1412 std::string modifier(newValue);
1413 if ( modifier[modifier.length()-1] !=
'^' )
1418 if( command == cmdToSaveInFactory ) {
1420 if ( where_to_store ) {
1421 G4VPhysicalVolume *world = 0x0;
1422 if ( G4TransportationManager::GetTransportationManager()
1423 && G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()) {
1424 world = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
1427 world->SetName(newValue); world->GetLogicalVolume()->SetName(newValue);
1428 where_to_store->
Store(world);
1431 G4cout <<
" Cannot save world in MyStore ! " << G4endl;
G4int Set(G4String basename, G4VPhysicalVolume *world, G4int copy_number_offset, const G4ThreeVector *T=0x0, const G4RotationMatrix *R=0x0, G4int main_copy_number=0)
to be used once a detector is fully contructed to simply place it in the world
static G4VPhysicalVolume * MakeVCR(G4String name, G4double HalfX=5.0 *CLHEP::m, G4double HalfY=5.0 *CLHEP::m, G4double HalfZ=5.0 *CLHEP::m, G4int copy=-1)
make a cubic world
the Messenger for the Detector Factory
void StoreMap(std::ofstream &amap, std::ofstream &dmap, std::vector< G4LogicalVolume * > &logical_stored, std::vector< G4VPhysicalVolume * > &physical_stored, std::vector< G4VPhysicalVolume * > &physical_active, G4VPhysicalVolume *theDetector)
recursively called in the tree struture
static DetectorFactory * GetFactory(G4String fullname)
get the specific factory for a full name to a file
void CollectVolumes(TGeoNode *theDetector, TObjArray &logical_stored, TObjArray &phycical_stored)
From the Full geometry, extract the logical and physical [nodes] volumes.
void Clean()
clear factory i.e. in memory collections of physicals and assemblies
G4String GetDetName(const G4String &path) const
Get detector name from full path to the gdml file.
void MakeInStore(G4String name, G4String version_string)
build in store a particular detector from its names and version. i.e. call th Make method of the sub ...
virtual void Store(G4VPhysicalVolume *)
Store in the sub-factory the given physical volume.
static G4int GetGCopyNb()
virtual G4VPhysicalVolume * Get(G4String basename)
search for a detector in DetectorFactory
static G4VSensitiveDetector * GetSD(G4String opt, const char sd_type= 'S')
Get a particular SD. S means a SD while s is for Scorers.
static G4int AddGCopyNb()
global copy number, used to set copy number once building detector. Not protected for MT since in pri...
static DetectorFactory * theMainFactory()
to get the main factory
void SetNewValue(G4UIcommand *, G4String)
virtual G4VPhysicalVolume * MakeAnArrayFromFactory(G4String input_file)
built an array from the factory using the given input file
G4String GetFileName(G4String detname, G4String extention)
compute name of the file to store/read information
virtual G4int ReMap(G4VPhysicalVolume *adetector, G4int offset)
for the given volume, it changes all copy number of active volumes by starting with offset ...
static G4int SetGCopyNb(G4int val)
static G4VSensitiveDetector * GetCopClusterSD(G4String name="/SToGS/SD/CopCluster")
to get a general SToGS Calorimeter. In Multi-threading mode, return a new instance otherwise a global...
static void CollectVolumes(G4VPhysicalVolume *theDetector, std::vector< G4LogicalVolume * > &logical_stored, std::vector< G4VPhysicalVolume * > &physical_stored, std::vector< G4VPhysicalVolume * > &physical_active)
From the given physical volume, it collects into collection all logical (sensitive) and physical volu...
virtual void GetAttributes(G4String basename, G4bool do_amap=true, G4bool do_dmap=false)
Read the amap file and apply atributes to the detector. if not found, it creates a deefault one from ...
Base Factory. This is a container of sub-factories.
void AssemblyRename(G4AssemblyVolume *assembly, const G4String &volume_to_find) const
rename physical volume produced by the assembly
G4String GetFactoryName() const
virtual void MakeStore()
build the default store i.e. all the detectors. Here It calls MakeStore for all registered sub factor...
static G4VSensitiveDetector * GetTrackerSD(G4String name="/SToGS/SD/Tracker")
to get a general SToGS tracker. In Multi-threading mode, return a new instance otherwise a global one...
G4int DoMap(G4AssemblyVolume *assembly, G4VPhysicalVolume *volume_used_to_built_assembly, G4int copy_number_offset=0) const
remap all the physical volumes produced by the Imprint method.
G4VPhysicalVolume * Import(G4String gdmlfile, G4String detname, const G4String &opt_amap, const G4String &opt_dmap)
import a gdml file (extention to ascii ?) into factory. See Active for opt_amap option ...
void StreamTouchable(std::ofstream &dmap, G4String)
extrac from the name of the detector the touchable part
static void SetActive(G4LogicalVolume *pv, const G4String &option="*|*|*|0.5;0.5;0.5;0.5")
Set to the given logical volume some attributs (sensitive, color) depending of the given option...
static void ChangeSD(G4String opt, G4VPhysicalVolume *top=0x0)
Change some SD detector into an other type of SD. Opt is used eventually to filter on the name of the...
G4AssemblyVolume * GetAssembly(G4String basename)