CreateNTuple.cxx
Go to the documentation of this file.
1 
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15 
16 #include "CreateNTuple.h"
17 
18 #include "CanvasWindow.h"
19 #include "WindowController.h"
20 #include "Inspector.h"
21 #include "QtFileDialog.h"
22 #include "QtSortedCheckListItem.h"
23 
24 #include "qcombobox.h"
25 #include "qlineedit.h"
26 #include "qmessagebox.h"
27 
30 #include "datasrcs/NTuple.h"
31 #include "datasrcs/TupleCut.h"
32 
34 #include "plotters/Cut2DPlotter.h"
35 
36 #include "fits/FitsController.h"
37 
38 #include <cassert>
39 
40 using std::string;
41 using std::vector;
42 
43 using namespace hippodraw;
44 
46 CreateNTuple ( QWidget * parent, const char * name )
47  : CreateNTupleBase ( parent, name )
48 {
50  initListViews ();
52 }
53 
54 
55 void
58 {
59  const vector < string > & nt_vector
60  = DataSourceController::instance() -> getNTupleNames ();
61  if ( nt_vector.empty () ) {
62  m_ntuple_in -> clear ();
63  return;
64  }
65 
66  unsigned int count = m_ntuple_in -> count ();
67  if ( count == nt_vector.size () ) return;
68 
69  m_ntuple_in -> clear();
70  vector < string > ::const_iterator first = nt_vector.begin();
71  while ( first != nt_vector.end() ) {
72  const string & name = *first++;
73  m_ntuple_in->insertItem ( name.c_str() );
74  }
75 
76 }
77 
78 void
81 {
82 
84 
85  DataSource * current_ds = DataSourceController::instance() -> getCurrent();
86  columnNumber = current_ds -> columns();
87 
88  for ( unsigned int i = 0; i < columnNumber; i++ )
89  {
90  QString dummy = QString ("%1").arg(i);
91  const std::string & s = current_ds -> getLabelAt ( i );
92  const QString label = s.c_str();
93  QtSortedCheckListItem * item =
95  item->setText( 1, QString ("%1").arg(label));
96  }
97 }
98 
99 void
102 {
103  m_CutListView->clear();
104  m_cut_list.clear ();
105 
106  const std::vector < PlotterBase * > & cutlist = getDataCutList();
107  std::vector < PlotterBase * >::const_iterator it = cutlist.begin();
108 
109  unsigned int i = 0;
110 
111  for ( ; it != cutlist.end(); ++ it )
112  {
113  PlotterBase * plotter = *it;
114  CutPlotter * cp = dynamic_cast < CutPlotter * > (plotter);
115  cp -> fillCutList ( m_cut_list );
116 
117  Range cutrange = cp -> getCutRange();
118  QString dummy = QString ("%1").arg(i);
119  const std::string & label = cp -> getLabel(Axes::X);
120  double min = cutrange.low();
121  double max = cutrange.high();
122  const std::string & inversion = (cp -> getCutInversion())?"Inverted":"";
123 
124  QtSortedCheckListItem * item =
126  item->setText( 1, QString("%1").arg ( label.c_str() ) );
127  item->setText( 2, QString("%1").arg(min));
128  item->setText( 3, QString("%1").arg(max));
129  item->setText( 5, QString("%1").arg( inversion.c_str() ) );
130 
131  Cut2DPlotter * cp2 = dynamic_cast < Cut2DPlotter * > (cp);
132  if (cp2)
133  {
134  item->setText( 4, QString("2D-X"));
135 
136  // For Cut2DPlotter, update the information for the Y axis
137  ++i;
138  Range cutrange = cp2 -> getCutRangeY();
139  QString dummy = QString("%1").arg(i);
140  const std::string & label = cp2-> getLabel(Axes::Y);
141  double min = cutrange.low();
142  double max = cutrange.high();
143 
144  QtSortedCheckListItem * item
145  = new QtSortedCheckListItem (m_CutListView,dummy);
146  item->setText( 1, QString("%1").arg ( label.c_str() ) );
147  item->setText( 2, QString("%1").arg(min));
148  item->setText( 3, QString("%1").arg(max));
149  item->setText( 4, QString("2D-Y"));
150  item->setText( 5, QString("%1").arg(inversion.c_str()));
151  ++i;
152  }
153  else
154  {
155  item->setText( 4, QString ("1D"));
156  ++i;
157  }
158  }
159  cutNumber = i;
160 
161  assert ( m_cut_list.size() == cutNumber );
162 }
163 
164 void
167 {
168  m_CutListView->addColumn (QString ("Index"),20 );
169  m_CutListView->addColumn (QString("Cut Name"), 20 );
170  m_CutListView->addColumn (QString("Min"), 20 );
171  m_CutListView->addColumn (QString("Max"), 20 );
172  m_CutListView->addColumn (QString("2D Cut"), 5 );
173  m_CutListView->addColumn (QString("Inversion"),5);
174 
175  m_ColumnListView->addColumn (QString("Index"), 20 );
176  m_ColumnListView->addColumn (QString("Column Name"), 40 );
177 
178 }
179 
180 void
183 {
185  controller -> setCurrentIndex ( item );
186 
187  // Set the ComboBox.
188  m_ntuple_in-> setCurrentItem ( item );
190  updateCutList();
191 
192 }
193 
194 void
196 getPlotterList ( std::vector < PlotterBase * > & plotterlist )
197 {
198  plotterlist.clear();
199  CanvasWindow * canvaz = WindowController::instance () -> currentCanvas();
200 
201  if ( canvaz !=0 ) {
202  canvaz -> fillPlotterList ( plotterlist );
203  }
204 }
205 
206 const std::vector < PlotterBase * > &
209 {
210  vector < PlotterBase * > plotterlist;
211  getPlotterList ( plotterlist );
212 
213  CutController * cutcontroller = CutController::instance();
214  const DataSource * current_ds
215  = DataSourceController::instance() -> getCurrent();
216 
217  return cutcontroller->getCutList ( plotterlist, current_ds );
218 }
219 
220 
221 void
224 {
227  DataSource * ds = ds_controller -> getCurrent();
228 
229  std::string name( (m_ntuple_out->text()).latin1() );
230 
231  if ( name.size()==0 ) name = "<no name>"; // default title
232 
233  setColumnList();
234  setCutList();
235 
236  NTuple * nt = ntc->createNTuple (m_column_list, m_cut_list, ds);
237 
238  // Do nothing if no column or row selected.
239  if ((nt->rows()==0) || (nt->columns()==0)) return;
240 
241  // Register the new NTuple.
242  ntc-> registerNTuple ( name, nt );
243 
244  // Update the dialog and inspector.
245  updateNTupleIn();
246  WindowController::instance () -> getInspector() -> update();
247 }
248 
249 void
252 {
253 
254  string filename = QtFileDialog::getExportTupleFilename ( this );
255 
256  if (filename.empty()==true) return;
257 
258 
259  // Write data to the file.
260  string::size_type pos = filename.find_last_of ('.' );
261  const string suffix = filename.substr (pos);
262 
264  DataSource * ds = ds_controller -> getCurrent();
265 
266  std::string name( (m_ntuple_out->text()).latin1() );
267 
268  if ( name.size()==0 ) name = "<no name>"; // default title
269 
270  setColumnList();
271  setCutList();
272 
273  if ( suffix == ".fits" || suffix == ".gz" )
274  {
275 #ifdef HAVE_CFITSIO
277  // int retVal =
278 // fc -> writeNTupleToFile ( m_column_list, m_cut_list, ds, filename, name);
279  fc -> writeNTupleToFile ( ds, filename, name, m_column_list, m_cut_list);
280 #else
281  QString message ( "Sorry, can not create FITS file.\n"
282  "The application was not built with\n"
283  "optional FITS support." );
284 
285  QMessageBox::critical ( this, // parent
286  "Create NTuple", // caption
287  message,
288  QMessageBox::Ok,
289  Qt::NoButton );
290 #endif
291  }
292  else
293  {
295  // Need to deal with retVal;
296  // int retVal =
297  ntc->createNTupleToFile (m_column_list, m_cut_list, ds, filename, name);
298  }
299 
300  accept();
301 
302 }
303 
304 void
307 {
308 #if QT_VERSION < 0x040000
309  QListViewItem * item = m_CutListView -> firstChild();
310 #else
311  Q3ListViewItem * item = m_CutListView -> firstChild();
312 #endif
313 
314  for (unsigned int i = 0; i<cutNumber; i++ )
315  {
316 
317  QtSortedCheckListItem * item2 =
318  dynamic_cast < QtSortedCheckListItem * > (item);
319  item2 -> setOn ( yes );
320  item = item->nextSibling();
321  }
322 }
323 
324 void
327 {
328 #if QT_VERSION < 0x040000
329  QListViewItem * item = m_ColumnListView -> firstChild();
330 #else
331  Q3ListViewItem * item = m_ColumnListView -> firstChild();
332 #endif
333  for (unsigned int i = 0; i<columnNumber; i++ )
334  {
335 
336  QtSortedCheckListItem * item2 =
337  dynamic_cast < QtSortedCheckListItem * > (item);
338  item2 -> setOn ( yes );
339  item = item->nextSibling();
340  }
341 }
342 
343 void
346 {
347 
348  m_column_list.clear();
349 
350 #if QT_VERSION < 0x040000
351  QListViewItem * item = m_ColumnListView -> firstChild();
352 #else
353  Q3ListViewItem * item = m_ColumnListView -> firstChild();
354 #endif
355 
356  for (unsigned int i = 0; i<columnNumber; i++ )
357  {
358 
359  QtSortedCheckListItem * item2 =
360  dynamic_cast < QtSortedCheckListItem * > (item);
361  if (item2->isOn()) {
362 
363  std::string name( (item->text(1)).latin1() );
364  m_column_list.push_back(name);
365  }
366 
367  item = item->nextSibling();
368  }
369 }
370 
371 void CreateNTuple::
373 {
374  // Keep newNTuple unchanged if there is no cuts.
375  if ( cutNumber == 0 ) return;
376 
377  // First remove the cuts not used.
378 #if QT_VERSION < 0x040000
379  QListViewItem * item = m_CutListView -> firstChild();
380 #else
381  Q3ListViewItem * item = m_CutListView -> firstChild();
382 #endif
383  vector < const TupleCut * >::iterator first = m_cut_list.begin ();
384  while ( first != m_cut_list.end() ) {
385  QtSortedCheckListItem * item2
386  = dynamic_cast < QtSortedCheckListItem * > ( item );
387  if ( item2 -> isOn () == false ) {
388  first = m_cut_list.erase ( first );
389  }
390  else {
391  first++;
392  }
393  item = item -> nextSibling ();
394  }
395 }

Generated for HippoDraw Class Library by doxygen