CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/Alignment/KalmanAlignmentAlgorithm/src/KalmanAlignmentDataCollector.cc

Go to the documentation of this file.
00001 
00002 #include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentDataCollector.h"
00003 
00004 #include "TGraph.h"
00005 #include "TNtuple.h"
00006 #include "TFile.h"
00007 #include "TH1F.h"
00008 
00009 using namespace std;
00010 
00011 
00012 KalmanAlignmentDataCollector* KalmanAlignmentDataCollector::theDataCollector = new KalmanAlignmentDataCollector();
00013 
00014 
00015 KalmanAlignmentDataCollector::KalmanAlignmentDataCollector( void ) {}
00016 
00017 
00018 KalmanAlignmentDataCollector::KalmanAlignmentDataCollector( const edm::ParameterSet & config ) : theConfiguration( config ) {}
00019 
00020 
00021 KalmanAlignmentDataCollector::~KalmanAlignmentDataCollector() {}
00022 
00023 
00024 KalmanAlignmentDataCollector* KalmanAlignmentDataCollector::get( void )
00025 {
00026   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00027   return theDataCollector;
00028 }
00029 
00030 
00031 void KalmanAlignmentDataCollector::configure( const edm::ParameterSet & config )
00032 {
00033   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00034   theDataCollector->config( config );
00035 }
00036 
00037 
00038 void KalmanAlignmentDataCollector::fillHistogram( string histo_name, float data )
00039 {
00040   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00041   theDataCollector->fillTH1F( histo_name, data );
00042 }
00043 
00044 
00045 void KalmanAlignmentDataCollector::fillHistogram( string histo_name, int histo_number, float data )
00046 {
00047   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00048   theDataCollector->fillTH1F( histo_name, histo_number, data );
00049 }
00050 
00051 
00052 void KalmanAlignmentDataCollector::fillGraph( string graph_name, float x_data, float y_data )
00053 {
00054   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00055   theDataCollector->fillTGraph( graph_name, x_data, y_data );
00056 }
00057 
00058 
00059 void KalmanAlignmentDataCollector::fillGraph( string graph_name, int graph_number, float x_data, float y_data )
00060 {
00061   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00062   theDataCollector->fillTGraph( graph_name, graph_number, x_data, y_data );
00063 }
00064 
00065 
00066 void KalmanAlignmentDataCollector::fillNtuple( std::string ntuple_name, float data )
00067 {
00068   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00069   theDataCollector->fillTNtuple( ntuple_name, data );
00070 }
00071 
00072 
00073 void KalmanAlignmentDataCollector::write( void )
00074 {
00075   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00076   theDataCollector->writeToTFile();
00077 }
00078 
00079 
00080 void KalmanAlignmentDataCollector::write( string file_name, string mode )
00081 {
00082   //if ( !theDataCollector ) theDataCollector = new KalmanAlignmentDataCollector();
00083   theDataCollector->writeToTFile( file_name, mode );
00084 }
00085 
00086 
00087 void KalmanAlignmentDataCollector::clear( void )
00088 {
00089   if ( theDataCollector ) theDataCollector->clearData();
00090 }
00091 
00092 
00093 void KalmanAlignmentDataCollector::config( const edm::ParameterSet & config )
00094 {
00095   theConfiguration = config;
00096 }
00097 
00098 
00099 void KalmanAlignmentDataCollector::fillTH1F( string histo_name, float data )
00100 {
00101   if ( theHistoData.find( histo_name ) == theHistoData.end() )
00102   {
00103     theHistoData[histo_name] = vector< float > ( 1, data );
00104   }
00105   else
00106   {
00107     theHistoData[histo_name].push_back( data );
00108   }
00109 
00110   return;
00111 }
00112 
00113 
00114 void KalmanAlignmentDataCollector::fillTH1F( string histo_name, int histo_number, float data )
00115 {
00116   string full_histo_name = histo_name + toString( histo_number );
00117   fillTH1F( full_histo_name, data );
00118 
00119   return;
00120 }
00121 
00122 
00123 void KalmanAlignmentDataCollector::fillTGraph( string graph_name, float x_data, float y_data )
00124 {
00125   if ( theXGraphData.find( graph_name ) == theXGraphData.end() )
00126   {
00127     theXGraphData[graph_name] = vector< float > ( 1, x_data );
00128     theYGraphData[graph_name] = vector< float > ( 1, y_data );
00129   }
00130   else
00131   {
00132     theXGraphData[graph_name].push_back( x_data );
00133     theYGraphData[graph_name].push_back( y_data );
00134   }
00135 
00136   return;
00137 }
00138 
00139 
00140 void KalmanAlignmentDataCollector::fillTGraph( string graph_name, int graph_number, float x_data, float y_data )
00141 {
00142   string full_graph_name = graph_name + toString( graph_number );
00143   fillTGraph( full_graph_name, x_data, y_data );
00144 
00145   return;
00146 }
00147 
00148 
00149 void KalmanAlignmentDataCollector::fillTNtuple( std::string ntuple_name, float data )
00150 {
00151   if ( theNtupleData.find( ntuple_name ) == theNtupleData.end() )
00152   {
00153     theNtupleData[ntuple_name] = vector< float > ( 1, data );
00154    }
00155   else
00156   {
00157     theNtupleData[ntuple_name].push_back( data );
00158   }
00159 }
00160 
00161 
00162 void KalmanAlignmentDataCollector::writeToTFile( void )
00163 {
00164   string fileName = theConfiguration.getUntrackedParameter< string >( "FileName", "KalmanAlignmentData.root" );
00165   string fileMode = theConfiguration.getUntrackedParameter< string >( "Mode", "RECREATE" );
00166   writeToTFile( fileName, fileMode );
00167 }
00168 
00169 
00170 void KalmanAlignmentDataCollector::writeToTFile( string file_name, string mode )
00171 {
00172   int nBins = theConfiguration.getUntrackedParameter< int >( "NBins", 200 );
00173   double xMin = theConfiguration.getUntrackedParameter< double >( "XMin", -10. );
00174   double xMax = theConfiguration.getUntrackedParameter< double >( "XMax", 10. );
00175 
00176   TFile* file = new TFile( file_name.c_str(), mode.c_str() );
00177 
00178   if ( !theHistoData.empty() )
00179   {
00180     map< string, vector< float > >::iterator itH = theHistoData.begin();
00181     vector< float >::iterator itV;
00182     TH1F* tempHisto;
00183 
00184     while ( itH != theHistoData.end() )
00185     {
00186       tempHisto = new TH1F( itH->first.c_str(), itH->first.c_str(), nBins, xMin, xMax );
00187 
00188       itV = itH->second.begin();
00189       while ( itV != itH->second.end() ) {
00190         tempHisto->Fill( *itV );
00191         itV++;
00192       }
00193 
00194       tempHisto->Write();
00195       delete tempHisto;
00196 
00197       ++itH;
00198     }
00199   }
00200 
00201   if ( !theXGraphData.empty() )
00202   {
00203     map< string, vector< float > >::iterator itXG = theXGraphData.begin();
00204     map< string, vector< float > >::iterator itYG = theYGraphData.begin();
00205 
00206     float* xData;
00207     float* yData;
00208 
00209     TGraph* tempGraph;
00210 
00211     while ( itXG != theXGraphData.end() )
00212     {
00213       int nData = itXG->second.size();
00214 
00215       xData = new float[nData];
00216       yData = new float[nData];
00217 
00218       for ( int iData = 0; iData < nData; iData++ )
00219       {
00220         xData[iData] = itXG->second[iData];
00221         yData[iData] = itYG->second[iData];
00222       }
00223 
00224       tempGraph = new TGraph( nData, xData, yData );
00225       tempGraph->SetName( itXG->first.c_str() );
00226       tempGraph->SetTitle( itXG->first.c_str() );
00227 
00228       tempGraph->Write();
00229       delete tempGraph;
00230 
00231       delete[] xData;
00232       delete[] yData;
00233 
00234       ++itXG;
00235       ++itYG;
00236     }
00237   }
00238 
00239 
00240   if ( !theNtupleData.empty() )
00241   {
00242     map< string, vector< float > >::iterator itN = theNtupleData.begin();
00243 
00244     TNtuple* ntuple;
00245 
00246     while ( itN != theNtupleData.end() )
00247     {
00248       ntuple = new TNtuple( itN->first.c_str(), itN->first.c_str(), itN->first.c_str() );
00249 
00250       vector< float >::iterator itD = itN->second.begin(), itDEnd = itN->second.end();
00251       while ( itD != itDEnd )
00252       {
00253         ntuple->Fill( *itD );
00254         ++itD;
00255       }
00256 
00257       ntuple->Write();
00258       delete ntuple;
00259 
00260       ++itN;
00261     }
00262   }
00263 
00264 
00265   file->Write();
00266   file->Close();
00267   delete file;
00268 
00269   return;
00270 }
00271 
00272 
00273 void KalmanAlignmentDataCollector::clearData( void )
00274 {
00275   theHistoData.clear();
00276   theXGraphData.clear();
00277   theYGraphData.clear();
00278 }
00279 
00280 
00281 string KalmanAlignmentDataCollector::toString( int i )
00282 {
00283   char temp[10];
00284   snprintf( temp, sizeof(temp), "%u", i );
00285 
00286   return string( temp );
00287 }