00001
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015
00016 #include "CreateNTuple.h"
00017
00018 #include "CanvasWindow.h"
00019 #include "WindowController.h"
00020 #include "Inspector.h"
00021 #include "QtFileDialog.h"
00022 #include "QtSortedCheckListItem.h"
00023
00024 #include "qcombobox.h"
00025 #include "qlineedit.h"
00026 #include "qmessagebox.h"
00027
00028 #include "datasrcs/DataSourceController.h"
00029 #include "datasrcs/NTupleController.h"
00030 #include "datasrcs/NTuple.h"
00031 #include "datasrcs/TupleCut.h"
00032
00033 #include "controllers/CutController.h"
00034 #include "plotters/Cut2DPlotter.h"
00035
00036 #include "fits/FitsController.h"
00037
00038 #include <cassert>
00039
00040 using std::string;
00041 using std::vector;
00042
00043 using namespace hippodraw;
00044
00045 CreateNTuple::
00046 CreateNTuple ( QWidget * parent, const char * name )
00047 : CreateNTupleBase ( parent, name )
00048 {
00049 updateNTupleIn();
00050 initListViews ();
00051 currentDataSourceChanged (0);
00052 }
00053
00054
00055 void
00056 CreateNTuple::
00057 updateNTupleIn ()
00058 {
00059 const vector < string > & nt_vector
00060 = DataSourceController::instance() -> getNTupleNames ();
00061 if ( nt_vector.empty () ) {
00062 m_ntuple_in -> clear ();
00063 return;
00064 }
00065
00066 unsigned int count = m_ntuple_in -> count ();
00067 if ( count == nt_vector.size () ) return;
00068
00069 m_ntuple_in -> clear();
00070 vector < string > ::const_iterator first = nt_vector.begin();
00071 while ( first != nt_vector.end() ) {
00072 const string & name = *first++;
00073 m_ntuple_in->insertItem ( name.c_str() );
00074 }
00075
00076 }
00077
00078 void
00079 CreateNTuple::
00080 updateColumnList ()
00081 {
00082
00083 m_ColumnListView->clear();
00084
00085 DataSource * current_ds = DataSourceController::instance() -> getCurrent();
00086 columnNumber = current_ds -> columns();
00087
00088 for ( unsigned int i = 0; i < columnNumber; i++ )
00089 {
00090 QString dummy = QString ("%1").arg(i);
00091 const std::string & s = current_ds -> getLabelAt ( i );
00092 const QString label = s.c_str();
00093 QtSortedCheckListItem * item =
00094 new QtSortedCheckListItem (m_ColumnListView,dummy);
00095 item->setText( 1, QString ("%1").arg(label));
00096 }
00097 }
00098
00099 void
00100 CreateNTuple::
00101 updateCutList()
00102 {
00103 m_CutListView->clear();
00104 m_cut_list.clear ();
00105
00106 const std::vector < PlotterBase * > & cutlist = getDataCutList();
00107 std::vector < PlotterBase * >::const_iterator it = cutlist.begin();
00108
00109 unsigned int i = 0;
00110
00111 for ( ; it != cutlist.end(); ++ it )
00112 {
00113 PlotterBase * plotter = *it;
00114 CutPlotter * cp = dynamic_cast < CutPlotter * > (plotter);
00115 cp -> fillCutList ( m_cut_list );
00116
00117 Range cutrange = cp -> getCutRange();
00118 QString dummy = QString ("%1").arg(i);
00119 const std::string & label = cp -> getLabel(Axes::X);
00120 double min = cutrange.low();
00121 double max = cutrange.high();
00122 const std::string & inversion = (cp -> getCutInversion())?"Inverted":"";
00123
00124 QtSortedCheckListItem * item =
00125 new QtSortedCheckListItem (m_CutListView,dummy);
00126 item->setText( 1, QString("%1").arg ( label.c_str() ) );
00127 item->setText( 2, QString("%1").arg(min));
00128 item->setText( 3, QString("%1").arg(max));
00129 item->setText( 5, QString("%1").arg( inversion.c_str() ) );
00130
00131 Cut2DPlotter * cp2 = dynamic_cast < Cut2DPlotter * > (cp);
00132 if (cp2)
00133 {
00134 item->setText( 4, QString("2D-X"));
00135
00136
00137 ++i;
00138 Range cutrange = cp2 -> getCutRangeY();
00139 QString dummy = QString("%1").arg(i);
00140 const std::string & label = cp2-> getLabel(Axes::Y);
00141 double min = cutrange.low();
00142 double max = cutrange.high();
00143
00144 QtSortedCheckListItem * item
00145 = new QtSortedCheckListItem (m_CutListView,dummy);
00146 item->setText( 1, QString("%1").arg ( label.c_str() ) );
00147 item->setText( 2, QString("%1").arg(min));
00148 item->setText( 3, QString("%1").arg(max));
00149 item->setText( 4, QString("2D-Y"));
00150 item->setText( 5, QString("%1").arg(inversion.c_str()));
00151 ++i;
00152 }
00153 else
00154 {
00155 item->setText( 4, QString ("1D"));
00156 ++i;
00157 }
00158 }
00159 cutNumber = i;
00160
00161 assert ( m_cut_list.size() == cutNumber );
00162 }
00163
00164 void
00165 CreateNTuple::
00166 initListViews()
00167 {
00168 m_CutListView->addColumn (QString ("Index"),20 );
00169 m_CutListView->addColumn (QString("Cut Name"), 20 );
00170 m_CutListView->addColumn (QString("Min"), 20 );
00171 m_CutListView->addColumn (QString("Max"), 20 );
00172 m_CutListView->addColumn (QString("2D Cut"), 5 );
00173 m_CutListView->addColumn (QString("Inversion"),5);
00174
00175 m_ColumnListView->addColumn (QString("Index"), 20 );
00176 m_ColumnListView->addColumn (QString("Column Name"), 40 );
00177
00178 }
00179
00180 void
00181 CreateNTuple::
00182 currentDataSourceChanged ( int item )
00183 {
00184 DataSourceController * controller = DataSourceController::instance ();
00185 controller -> setCurrentIndex ( item );
00186
00187
00188 m_ntuple_in-> setCurrentItem ( item );
00189 updateColumnList();
00190 updateCutList();
00191
00192 }
00193
00194 void
00195 CreateNTuple::
00196 getPlotterList ( std::vector < PlotterBase * > & plotterlist )
00197 {
00198 plotterlist.clear();
00199 CanvasWindow * canvaz = WindowController::instance () -> currentCanvas();
00200
00201 if ( canvaz !=0 ) {
00202 canvaz -> fillPlotterList ( plotterlist );
00203 }
00204 }
00205
00206 const std::vector < PlotterBase * > &
00207 CreateNTuple::
00208 getDataCutList ( )
00209 {
00210 vector < PlotterBase * > plotterlist;
00211 getPlotterList ( plotterlist );
00212
00213 CutController * cutcontroller = CutController::instance();
00214 const DataSource * current_ds
00215 = DataSourceController::instance() -> getCurrent();
00216
00217 return cutcontroller->getCutList ( plotterlist, current_ds );
00218 }
00219
00220
00221 void
00222 CreateNTuple::
00223 createButtonClicked()
00224 {
00225 DataSourceController * ds_controller = DataSourceController::instance();
00226 NTupleController * ntc = NTupleController::instance();
00227 DataSource * ds = ds_controller -> getCurrent();
00228
00229 std::string name( (m_ntuple_out->text()).latin1() );
00230
00231 if ( name.size()==0 ) name = "<no name>";
00232
00233 setColumnList();
00234 setCutList();
00235
00236 NTuple * nt = ntc->createNTuple (m_column_list, m_cut_list, ds);
00237
00238
00239 if ((nt->rows()==0) || (nt->columns()==0)) return;
00240
00241
00242 ntc-> registerNTuple ( name, nt );
00243
00244
00245 updateNTupleIn();
00246 WindowController::instance () -> getInspector() -> update();
00247 }
00248
00249 void
00250 CreateNTuple::
00251 createFileButtonClicked()
00252 {
00253
00254 string filename = QtFileDialog::getExportTupleFilename ( this );
00255
00256 if (filename.empty()==true) return;
00257
00258
00259
00260 string::size_type pos = filename.find_last_of ('.' );
00261 const string suffix = filename.substr (pos);
00262
00263 DataSourceController * ds_controller = DataSourceController::instance();
00264 DataSource * ds = ds_controller -> getCurrent();
00265
00266 std::string name( (m_ntuple_out->text()).latin1() );
00267
00268 if ( name.size()==0 ) name = "<no name>";
00269
00270 setColumnList();
00271 setCutList();
00272
00273 if ( suffix == ".fits" || suffix == ".gz" )
00274 {
00275 #ifdef HAVE_CFITSIO
00276 FitsController * fc = FitsController::instance();
00277
00278
00279 fc -> writeNTupleToFile ( ds, filename, name, m_column_list, m_cut_list);
00280 #else
00281 QString message ( "Sorry, can not create FITS file.\n"
00282 "The application was not built with\n"
00283 "optional FITS support." );
00284
00285 QMessageBox::critical ( this,
00286 "Create NTuple",
00287 message,
00288 QMessageBox::Ok,
00289 Qt::NoButton );
00290 #endif
00291 }
00292 else
00293 {
00294 NTupleController * ntc = NTupleController::instance();
00295
00296
00297 ntc->createNTupleToFile (m_column_list, m_cut_list, ds, filename, name);
00298 }
00299
00300 accept();
00301
00302 }
00303
00304 void
00305 CreateNTuple::
00306 cutListCheckBox_toggled ( bool yes )
00307 {
00308 #if QT_VERSION < 0x040000
00309 QListViewItem * item = m_CutListView -> firstChild();
00310 #else
00311 Q3ListViewItem * item = m_CutListView -> firstChild();
00312 #endif
00313
00314 for (unsigned int i = 0; i<cutNumber; i++ )
00315 {
00316
00317 QtSortedCheckListItem * item2 =
00318 dynamic_cast < QtSortedCheckListItem * > (item);
00319 item2 -> setOn ( yes );
00320 item = item->nextSibling();
00321 }
00322 }
00323
00324 void
00325 CreateNTuple::
00326 columnListCheckBox_toggled ( bool yes )
00327 {
00328 #if QT_VERSION < 0x040000
00329 QListViewItem * item = m_ColumnListView -> firstChild();
00330 #else
00331 Q3ListViewItem * item = m_ColumnListView -> firstChild();
00332 #endif
00333 for (unsigned int i = 0; i<columnNumber; i++ )
00334 {
00335
00336 QtSortedCheckListItem * item2 =
00337 dynamic_cast < QtSortedCheckListItem * > (item);
00338 item2 -> setOn ( yes );
00339 item = item->nextSibling();
00340 }
00341 }
00342
00343 void
00344 CreateNTuple::
00345 setColumnList()
00346 {
00347
00348 m_column_list.clear();
00349
00350 #if QT_VERSION < 0x040000
00351 QListViewItem * item = m_ColumnListView -> firstChild();
00352 #else
00353 Q3ListViewItem * item = m_ColumnListView -> firstChild();
00354 #endif
00355
00356 for (unsigned int i = 0; i<columnNumber; i++ )
00357 {
00358
00359 QtSortedCheckListItem * item2 =
00360 dynamic_cast < QtSortedCheckListItem * > (item);
00361 if (item2->isOn()) {
00362
00363 std::string name( (item->text(1)).latin1() );
00364 m_column_list.push_back(name);
00365 }
00366
00367 item = item->nextSibling();
00368 }
00369 }
00370
00371 void CreateNTuple::
00372 setCutList()
00373 {
00374
00375 if ( cutNumber == 0 ) return;
00376
00377
00378 #if QT_VERSION < 0x040000
00379 QListViewItem * item = m_CutListView -> firstChild();
00380 #else
00381 Q3ListViewItem * item = m_CutListView -> firstChild();
00382 #endif
00383 vector < const TupleCut * >::iterator first = m_cut_list.begin ();
00384 while ( first != m_cut_list.end() ) {
00385 QtSortedCheckListItem * item2
00386 = dynamic_cast < QtSortedCheckListItem * > ( item );
00387 if ( item2 -> isOn () == false ) {
00388 first = m_cut_list.erase ( first );
00389 }
00390 else {
00391 first++;
00392 }
00393 item = item -> nextSibling ();
00394 }
00395 }