OpenWalnut  1.5.0dev
WFilterPropertyHandler.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <regex>
26 #include <string>
27 #include <vector>
28 
29 #include "core/kernel/WKernel.h"
30 #include "WFilterPropertyHandler.h"
31 
32 
34  WPropertyGroup::SPtr properties,
36  m_protonData( protonData ),
37  m_properties( properties ),
38  m_dataUpdate( dataUpdate )
39 {
40 }
41 
43 {
44  m_filteringGroup = m_properties->addPropertyGroup( "Filtering", "Filter primaries, secondaries and particle types" );
45 
49 
51 }
52 
54 {
55  if( m_protonData->isColumnAvailable( WSingleSelectorName::getParentId() ) )
56  {
57  m_showPrimaries->setHidden( false );
58  m_showSecondaries->setHidden( false );
59  }
60  else
61  {
62  m_showPrimaries->setHidden( true );
63  m_showSecondaries->setHidden( true );
64  }
65 
66  if( m_protonData->isColumnAvailable( WSingleSelectorName::getPDG() ) )
67  {
69  m_multiSelection->setHidden( false );
70  m_filteringsubGroup->setHidden( false );
71  }
72  else
73  {
74  m_multiSelection->setHidden( true );
75  m_filteringsubGroup->setHidden( true );
76  }
77 }
78 
80 {
81  m_currentColumnIndex = m_protonData->getColumnIndexBySelection( WSingleSelectorName::getPDG() );
82 
83  m_filteringGroup->removeProperty( m_multiSelection );
84  m_filteringGroup->removeProperty( m_filteringsubGroup );
85 
88 }
89 
91 {
92  WPropertyBase::PropertyChangeNotifierType notifierCheckBox = boost::bind(
93  &WFilterPropertyHandler::updateCheckboxProperty, this, boost::placeholders::_1 );
94 
95  m_showPrimaries = m_filteringGroup->addProperty( "Show primaries",
96  "Show or hide primaries. One can either hide primaries or secondaries, but not both at the same time.",
97  true, notifierCheckBox );
98  m_showSecondaries = m_filteringGroup->addProperty( "Show secondaries",
99  "Show or hide secondaries. One can either hide primaries or secondaries, but not both at the same time.",
100  true, notifierCheckBox );
101 }
102 
104 {
105  int pdgColumnIndex = m_protonData->getColumnIndexBySelection( "Particle Data Group" );
106 
107  if( pdgColumnIndex < 0 )
108  {
109  return;
110  }
111 
112  for( size_t idx = 0; idx < m_protonData->getCSVData()->size(); idx++ )
113  {
114  std::vector< std::string > row = m_protonData->getCSVData()->at( idx );
115  std::string rowContent = row.at( pdgColumnIndex );
116 
117  int currentParticleID = -1;
118 
119  try
120  {
121  currentParticleID = ( int ) boost::lexical_cast< double >( rowContent );
122  }
123  catch( const boost::bad_lexical_cast &e )
124  {
125  std::string errorMessage = "The selected column has an incorrect format. Received: " +
126  rowContent + ". Required: Numbers are expected. " +
127  std::string( e.what() );
128 
129  throw WException( errorMessage );
130  }
131 
132  if( std::find( m_pdgTypes.begin(), m_pdgTypes.end(), currentParticleID ) == m_pdgTypes.end() )
133  {
134  m_pdgTypes.push_back( currentParticleID );
135  }
136  }
137 }
138 
140 {
141  WPropertyBase::PropertyChangeNotifierType pdgEncodingnotifier = boost::bind(
143 
145 
146  m_pdgTypes.clear();
147  searchPDGTypes();
148 
149  if( m_pdgTypes.size() <= 0 )
150  {
151  m_multiSelection = m_filteringGroup->addProperty( "Show particles", "Choose particle type(s) to be shown.",
152  m_particleItemSelectionList->getSelectorNone(), pdgEncodingnotifier );
153  return;
154  }
155 
156  for( auto pdgType : m_pdgTypes )
157  {
159  getParticleNameFromPdg( pdgType ) + " (" + std::to_string( pdgType ) + ")"
160  );
161  }
162 
163  m_multiSelection = m_filteringGroup->addProperty( "Show particles", "Choose particle type(s) to be shown.",
164  m_particleItemSelectionList->getSelectorAll(), pdgEncodingnotifier );
165 
167 }
168 
170 {
171  m_dataUpdate( );
172 }
173 
175 {
176  m_notifierSetParticleName = boost::bind(
177  &WFilterPropertyHandler::selectPdgAndSaveEvent, this, boost::placeholders::_1 );
178 
179 
180  m_filteringsubGroup = m_filteringGroup->addPropertyGroup( "Rename Particle Types",
181  "Filtering/Rename or Delete Particle-Name" );
182 
183  m_PdgForRenameSelection = m_filteringsubGroup->addProperty( "Select particle", "Select the particle type to be renamed.",
185 
186  m_inputNewParticleName = m_filteringsubGroup->addProperty( "New name (press enter)", "Type in a new name for the selected particle type. "
187  "To submit your entry press enter while you are in the textbox.",
189 
193 }
194 
196 {
197  WItemSelector selectedPdg = m_PdgForRenameSelection->get( true );
198 
199  if( selectedPdg.empty() )
200  {
201  return "";
202  }
203 
204  std::string particleName = selectedPdg.at( 0 )->getName();
205  return particleName.substr( 0, particleName.find( " (" ) );
206 }
207 
209 {
210  if( property == m_PdgForRenameSelection )
211  {
212  m_filteringsubGroup->removeProperty( m_inputNewParticleName );
213  m_inputNewParticleName = m_filteringsubGroup->addProperty( "New name (press enter)", "Type in a new name for the selected particle type. "
214  "To submit your entry press enter while you are in the textbox.",
216  }
217  if( property == m_inputNewParticleName )
218  {
219  WItemSelector selectedPdg = m_PdgForRenameSelection->get( true );
220  changePdgBiMap( getPdgFromName( selectedPdg.at( 0 )->getName() ), m_inputNewParticleName->get( true ) );
222  }
223 }
224 
226 {
227  WItemSelector selectedItems = m_multiSelection->get( true );
228 
229  for( size_t i = 0; i < selectedItems.size(); ++i )
230  {
231  if( getPdgFromName( selectedItems.at( i )->getName() ) == pdgType )
232  {
233  return true;
234  }
235  }
236 
237  return false;
238 }
239 
241 {
242  if( m_showPrimaries->get() || m_showSecondaries->get() )
243  {
244  m_dataUpdate( );
245  }
246  else
247  {
248  if( property == m_showPrimaries )
249  {
250  m_showPrimaries->set( true );
251  }
252 
253  if( property == m_showSecondaries )
254  {
255  m_showSecondaries->set( true );
256  }
257  }
258 }
259 
261 {
262  return m_showPrimaries;
263 }
264 
266 {
267  return m_showSecondaries;
268 }
269 
270 void WFilterPropertyHandler::createPDGMap( std::string path )
271 {
272  std::fstream pdgSignFile;
273  std::string dataRow;
274  std::string particleName;
275  int pdg;
276 
277  pdgSignFile.open( getParticleNameFilePath() );
278 
279  if( !pdgSignFile.is_open() )
280  {
282  {
283  throw WException( "Failed to copy particle name file into homedir" );
284  }
285  pdgSignFile.open( getParticleNameFilePath() );
286  }
287 
288  std::getline( pdgSignFile, dataRow );
289  while( std::getline( pdgSignFile, dataRow ) )
290  {
291  std::istringstream iss( dataRow );
292  iss >> particleName >> pdg;
293 
294  m_PdgNamesByID.insert( PdgElement( particleName, pdg ) );
295  }
296  pdgSignFile.close();
297 }
298 
300 {
301  BM_PDG::right_const_iterator pdg_iter = m_PdgNamesByID.right.find( pdg );
302 
303  return pdg_iter != m_PdgNamesByID.right.end() ? pdg_iter->second : "Unknown";
304 }
305 
306 int WFilterPropertyHandler::getPdgFromName( std::string particleName )
307 {
308  BM_PDG::left_const_iterator pdg_iter = m_PdgNamesByID.left.find( particleName.substr( 0, particleName.find( " (" ) ) );
309 
310  return pdg_iter != m_PdgNamesByID.left.end() ? pdg_iter->second : getPdgFromUnkownParticle( particleName );
311 }
312 
314 {
315  std::regex regexp( "[-+0-9]+" );
316  std::smatch m;
317  std::regex_search( particleName, m, regexp );
318 
319  return std::stoi( m[0] );
320 }
321 
322 void WFilterPropertyHandler::changePdgBiMap( int pdg, std::string newParticleName )
323 {
324  BM_PDG::right_iterator pdg_iter = m_PdgNamesByID.right.find( pdg );
325 
326  if( pdg_iter != m_PdgNamesByID.right.end() )
327  {
328  bool success_replace = m_PdgNamesByID.right.replace_data( pdg_iter, newParticleName );
329  if( !success_replace )
330  {
331  throw WException( "You cannot choose the same particle name twice" );
332  }
333  }
334  else //unknown particle
335  {
336  m_PdgNamesByID.insert( PdgElement( newParticleName, pdg ) );
337  }
338 
340 }
341 
343 {
344  std::ofstream pdgSignFile;
345  pdgSignFile.open( getParticleNameFilePath() , std::ios_base::out | std::ios::trunc );
346 
347  if( !pdgSignFile.is_open() )
348  {
349  throw WException( "File could not be opened!" );
350  }
351  for( auto pdgNameIterator = m_PdgNamesByID.begin(); pdgNameIterator != m_PdgNamesByID.end(); ++pdgNameIterator )
352  {
353  pdgSignFile << pdgNameIterator->left << " " << pdgNameIterator->right << "\n";
354  }
355  pdgSignFile.close();
356 }
357 
359 {
360  return WPathHelper::getHomePath().string() + "\\PDGEncodingNameMap.txt";
361 }
362 
363 bool WFilterPropertyHandler::copyFileToHomePath( std::string shareDirFile, std::string homeDirFile )
364 {
365  std::ifstream src( shareDirFile, std::ios::binary );
366  std::ofstream dest( homeDirFile, std::ios::binary );
367 
368  dest << src.rdbuf();
369  return src && dest;
370 }
Basic exception handler.
Definition: WException.h:39
WPropBool getShowSecondaries()
Getter.
void createCheckBoxForPrimaryAndSecondary()
Create the Checkbox for primaries and secondaries.
BM_PDG m_PdgNamesByID
A Map of the PDG and their names.
WPropBool getShowPrimaries()
Getter.
void createPDGMap(std::string path)
creates a bimap out of the names and ids of PDG txt
WPropBool m_showPrimaries
Decides whether to hide or show primaries.
void createMultiSelectionForPDG()
Create the multiselection for the option of PDG.
void writePdgMapInParticleNameFile()
write the BiMap in the Pdg particle name file
WPropSelection m_multiSelection
Stores users selected items.
WPropGroup m_filteringsubGroup
Stores subgroup for change the pdg-Name.
void updateProperty()
update current group property and subproperty
WPropSelection m_PdgForRenameSelection
A property to select one pdg to change.
boost::function< void() > CallbackPtr
shared_ptr that points to itself
WFilterPropertyHandler::CallbackPtr m_dataUpdate
A function variable that reinitializes the WDataSets.
WFilterPropertyHandler(WProtonData::SPtr protonData, WPropertyGroup::SPtr properties, WFilterPropertyHandler::CallbackPtr dataUpdate)
constructor
void createProperties()
creates the group property and the subproperty
std::string setDefaultForRenameField()
Set the default string for the string property textfield.
int getPdgFromName(std::string particleName)
Search the pdg in the map with a given particlename.
void changePdgBiMap(int pdg, std::string newParticleName)
set or replace a particlename and pdg in the bimap
WPropBool m_showSecondaries
Decides whether to hide or show secondaries.
int m_currentColumnIndex
The current column that is selected as particle data group.
void updatePDGProperties()
Update PDG Properties (Multiselector and change-Name-Properties)
WPropGroup m_filteringGroup
Property group for filtering options.
void searchPDGTypes()
Collect all particle types from your input data.
bool copyFileToHomePath(std::string shareDirFile, std::string homeDirFile)
Helperfunction to copy the particlename file.
WPropString m_inputNewParticleName
A string for the new particle name.
WProtonData::SPtr m_protonData
Pointer to the content and header of the CSV.
void createPropToSetParticleNames()
Create the Subgroup for change of PDG names.
WPropertyBase::PropertyChangeNotifierType m_notifierSetParticleName
A notifier for the Changeevents of the Rename PDG property.
std::shared_ptr< WItemSelection > m_particleItemSelectionList
Stores selectable items.
void selectPdgAndSaveEvent(WPropertyBase::SPtr property)
Event of the save button in the rename pdg subgroup.
std::string getParticleNameFromPdg(int pdg)
Seatch the ParticleName in the map with a given pdg.
void updateCheckboxProperty(WPropertyBase::SPtr property)
Reload data when properties for selection of primaries and secondaries changed.
int getPdgFromUnkownParticle(std::string particleName)
get the PDG number of an unknown particle element with the help of regex
void updateSelectedPDGTypes()
Update the m_selectedPDGTypes.
WPropertyGroup::SPtr m_properties
A property variable that is generated by the WModul.
BM_PDG::value_type PdgElement
represent an Element of a pdg and a particlename for the map
std::vector< int > m_pdgTypes
Stores every unique particle id.
bool isPDGTypeSelected(int pdgType)
Check whether the PDG type is contained in the m_selectedPDGTypes.
A class containing a list of named items.
std::shared_ptr< WItemSelection > SPtr
Convenience typedef for a std::shared_ptr< WItemSelection >
This class represents a subset of a WItemSelection.
Definition: WItemSelector.h:53
virtual bool empty() const
True if the selection is empty.
virtual const std::shared_ptr< WItemSelectionItem > at(size_t index) const
Gets the selected item with the given index.
virtual size_t size() const
The number of selected items.
static boost::filesystem::path getHomePath()
The path to the OW dir in the user's home.
boost::function< void(std::shared_ptr< WPropertyBase >)> PropertyChangeNotifierType
Signal signature emitted during set operations.
std::shared_ptr< WPropertyBase > SPtr
Convenience typedef for a std::shared_ptr< WPropertyBase >
Definition: WPropertyBase.h:53
std::shared_ptr< WPropertyGroup > SPtr
shared pointer to object of this type
std::shared_ptr< WProtonData > SPtr
shared_ptr that points to itself
Definition: WProtonData.h:52
static std::string getPDG()
getter
static std::string getParentId()
getter
void addTo(WPropSelection prop)
Add the PC_NOTEMPTY constraint to the property.
void addTo(WPropSelection prop)
Add the PC_SELECTONLYONE constraint to the property.