CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Validation/RecoJets/src/FitHist.cc

Go to the documentation of this file.
00001 #include "Validation/RecoJets/interface/FitHist.h"
00002 #include "Validation/RecoJets/interface/CompMethods.h"
00003 #include "Validation/RecoJets/interface/FitHist_fwd.h"
00004 #include "Validation/RecoJets/interface/RootPostScript.h"
00005 
00006 using namespace std;
00007 
00008 void 
00009 FitHist::configBlockFit(ConfigFile& cfg)
00010 {
00011   //-----------------------------------------------
00012   // read all configurables defined in FitHisto-
00013   // grams from config file. Throw human readable 
00014   // exception when misspellings occure
00015   //-----------------------------------------------
00016   try{
00017     //-----------------------------------------------
00018     // histogram steering
00019     //-----------------------------------------------
00020     readVector( cfg.read<std::string>( "titleIndex"), axesIndex_);
00021     readLabels( cfg.read<std::string>( "xAxesFit" ), xAxesFit_ );
00022     readLabels( cfg.read<std::string>( "yAxesFit" ), yAxesFit_ );
00023     
00024     //-----------------------------------------------
00025     // histogram manipulations
00026     //-----------------------------------------------
00027     readVector( cfg.read<std::string>( "targetLabel" ), targetHistList_);
00028     fitFuncName_ = cfg.read<std::string>( "fitFunctionName" );
00029     fitFuncTitle_= cfg.read<std::string>( "fitFunctionTitle");
00030     fitFuncType_ = cfg.read<int>        ( "fitFunctionType" );
00031     fitFuncLowerBound_= cfg.read<double>( "fitLowerBound" );
00032     fitFuncUpperBound_= cfg.read<double>( "fitUpperBound" );
00033     evalType_ = cfg.read<int>( "evalType" );
00034   }
00035   catch(...){
00036     std::cerr << "ERROR during reading of config file" << std::endl;
00037     std::cerr << "      misspelled variables in cfg ?" << std::endl;
00038     std::cerr << "      [--called in configBlockFit]"  << std::endl;
00039     std::exit(1);
00040   }
00041 }
00042 
00043 bool 
00044 FitHist::checkTargetHistList()
00045 {
00046   //-----------------------------------------------
00047   // check list of target histogram labels, return 
00048   // true if all elements are supported and false 
00049   // otherwise
00050   //-----------------------------------------------
00051   bool statusOk=true;
00052   for(unsigned int idx=0; idx<targetHistList_.size(); ++idx){
00053     if( statusOk ) statusOk=isInFitTargetList(targetHistList_[idx]);
00054   }
00055   return statusOk;
00056 }
00057 
00058 bool 
00059 FitHist::isInFitTargetList(std::string& label)
00060 {
00061   //-----------------------------------------------
00062   // check if given target prefix is suported
00063   //-----------------------------------------------
00064   TString target(label);
00065   if( !(target.CompareTo(FitTarget::Cal   ) ||
00066         target.CompareTo(FitTarget::Res   ) ||
00067         target.CompareTo(FitTarget::Sigma ) ||
00068         target.CompareTo(FitTarget::Mean  ) )){
00069     std::cerr << "ERROR while filling target histogram" << std::endl;
00070     std::cerr << "      can't find prefix: "  << target << std::endl;
00071     return false;
00072   }
00073   return true;
00074 }
00075 
00076 TH1F& 
00077 FitHist::findFitHistogram(const TObjArray& hist, TString& zip, TString& name, int& bin)
00078 {
00079   //---------------------------------------------
00080   // returns fit histogram of bin 'bin'to 
00081   // corresponding target histogram
00082   //---------------------------------------------
00083 
00084   //prepare reference name
00085   TString refName( name );
00086   refName+="_";
00087   refName+=bin;
00088   refName.Remove(0, refName.First('_')+1); //chop of prefix
00089   
00090   //---------------------------------------------
00091   // loop array of histograms & search for ref
00092   //---------------------------------------------
00093   for(int idx=0; idx<hist.GetEntriesFast(); ++idx){
00094     TH1F& cmp = *((TH1F*)(hist)[idx]);
00095     TString buffer( cmp.GetName() );
00096     if( buffer.BeginsWith( zip ) ){
00097       TString cmpName( cmp.GetName() );        // chop off root directory and 
00098       cmpName.Remove(0, cmpName.First('/')+1); // file from histogram name  
00099       if( cmpName.BeginsWith(FitTarget::Fit) && cmpName.Contains(refName) ){
00100         return cmp;
00101       }
00102     }
00103   }
00104   std::cout << "WARNING: could not find required histogram fit_" 
00105        << refName << "_x" << std::endl
00106        << "         return reference to Null" << std::endl;
00107   return *(TH1F*)0;
00108 }
00109 
00110 TH1F& 
00111 FitHist::findTargetHistogram(const TObjArray& hist, TString& zip, TString& name, TString& target)
00112 {
00113   //---------------------------------------------
00114   // returns target histogram to corresponding 
00115   // fit histogram
00116   //---------------------------------------------
00117 
00118   //prepare reference name
00119   TString refName( name );
00120   refName.Remove(0, refName.First('_')+1); //chop of prefix
00121   refName.Remove(refName.Last('_'), refName.Length()); //chop of postfix
00122 
00123   //---------------------------------------------
00124   // loop array of histograms & search for ref
00125   //---------------------------------------------
00126   for(int idx=0; idx<hist.GetEntriesFast(); ++idx){
00127     TH1F& cmp = *((TH1F*)(hist)[idx]);
00128     TString buffer( cmp.GetName() );
00129     if( buffer.BeginsWith( zip ) ){
00130       // prepare compare string
00131       TString cmpName( cmp.GetName() );        // chop off root directory and 
00132       cmpName.Remove(0, cmpName.First('/')+1); // file from histogram name  
00133       if( cmpName.BeginsWith(target) && cmpName.Contains(refName) ){
00134         return cmp;
00135       }
00136     }
00137   }
00138   std::cout << "WARNING: could not find required histogram " 
00139        << target << refName << std::endl
00140        << "         return reference to Null" << std::endl;
00141   return *(TH1F*)0;
00142 }
00143 
00144 double 
00145 FitHist::normalize(TString& buffer, double val)
00146 { 
00147   return ( (!buffer.CompareTo(FitTarget::Res ) && val>0.) ) ? 1./val : 1.; 
00148 }
00149 
00150 void 
00151 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, Quantile& func)
00152 {
00153   if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00154     htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00155   }
00156   if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00157     double norm=normalize(buffer, func.value( hfit ));
00158     htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError  ( bin, norm*func.spreadError( hfit ) );
00159   }
00160 }
00161 
00162 void 
00163 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, MaximalValue& func)
00164 {
00165   if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00166     htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00167   }
00168   if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00169     double norm=normalize(buffer, func.value( hfit ));
00170     htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError  ( bin, norm*func.spreadError( hfit ) );
00171   }
00172 }
00173 
00174 void 
00175 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, HistogramMean& func)
00176 {
00177   if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00178     htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00179   }
00180   if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00181     double norm=normalize(buffer, func.value( hfit ));
00182     htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError  ( bin, norm*func.spreadError( hfit ) );
00183   }
00184 }
00185 
00186 void 
00187 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin, TString& buffer, StabilizedGauss& func)
00188 {
00189   if( !buffer.CompareTo(FitTarget::Cal) || !buffer.CompareTo(FitTarget::Mean ) ){
00190     htarget.SetBinContent( bin, func.value( hfit ) ); htarget.SetBinError( bin, func.valueError( hfit ) );
00191   }
00192   if( !buffer.CompareTo(FitTarget::Res) || !buffer.CompareTo(FitTarget::Sigma) ){
00193     double norm=normalize(buffer, func.value( hfit ));
00194     htarget.SetBinContent( bin, norm*func.spread( hfit ) ); htarget.SetBinError  ( bin, norm*func.spreadError( hfit ) );
00195   }
00196 }
00197 
00198 void 
00199 FitHist::fillTargetHistogramBin(TH1F& htarget, TH1F& hfit, int bin)
00200 {
00201   if( !hfit.GetEntries()>0 ) return;
00202 
00203   //-----------------------------------------------
00204   // fill corresponding bin in target histogram
00205   //-----------------------------------------------
00206   TString buffer(htarget.GetName());
00207   buffer.Remove(0, buffer.First('/')+1);
00208   buffer.Remove(buffer.First('_')+1, buffer.Length());
00209   
00210   switch(evalType_){
00211   case kStabilizedGauss:
00212     {
00213       StabilizedGauss func(fitFuncName_.c_str());
00214       fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00215       break;
00216     }
00217   case kHistogramMean:
00218     {
00219       HistogramMean func;
00220       fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00221       break;
00222     }
00223   case kMaximalValue:
00224     {
00225       MaximalValue func(0.9, 0.05);
00226       fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00227       break;
00228     }
00229   case kQuantile:
00230     {
00231       Quantile func(0.5, 0.05);
00232       fillTargetHistogramBin(htarget, hfit, bin, buffer, func);
00233       break;
00234     } 
00235   default:
00236     
00237     break;
00238   }
00239 }
00240 
00241 void 
00242 FitHist::setFitHistogramAxes(TH1F& hist, int idx)
00243 {
00244   //-----------------------------------------------
00245   // get proper axes label for fit histograms; get
00246   // the proper histogram idx from int the vector
00247   // axesIndex_, as the same label will be used
00248   // many times for each fit bin thi saves from 
00249   // repeatedly passing the same labels.
00250   //-----------------------------------------------
00251   if(idx<(int)axesIndex_.size()){
00252     int jdx = axesIndex_[idx];
00253     if( jdx<(int)xAxesFit_.size() && jdx<(int)yAxesFit_.size() ){
00254       setAxesStyle( hist, xAxesFit_[jdx].c_str(), yAxesFit_[jdx].c_str() );
00255     }
00256     else if( jdx<(int)xAxesFit_.size() ){
00257       setAxesStyle( hist, xAxesFit_[jdx].c_str(), "events"  );
00258     }
00259   }
00260   else{
00261     setAxesStyle( hist, hist.GetName(), "events" );     
00262   }
00263 }
00264 
00265 void 
00266 FitHist::addBinLabelToFitHist(const TObjArray& hist, int& bin, TString& name, TString& zip)
00267 {
00268   //---------------------------------------------
00269   // add pave text to fitted histograms keeping 
00270   // information of the corresponding bin edges
00271   //---------------------------------------------
00272   TPaveText* text  = new TPaveText(0.25, 0.85, 0.95, 0.95, "NDC");
00273   text->SetBorderSize(   0 );
00274   text->SetFillStyle( 4000 );
00275   text->SetTextAlign(   12 );
00276   text->SetTextSize ( 0.06 );
00277   text->SetTextColor(    1 );
00278   text->SetTextFont (   62 );
00279 
00280   char labelname[100];
00281   TString buffer(targetHistList_[0]); buffer+="_";
00282   TH1F&  target = findTargetHistogram(hist, zip, name, buffer);
00283   double lowerEdge = target.GetBinLowEdge(bin+1);
00284   double upperEdge = target.GetBinLowEdge(bin+1)+target.GetBinWidth(bin+1);
00285   sprintf(labelname, "[%3.0f GeV; %3.0f GeV]", lowerEdge, upperEdge );//FIXME: this does not need to be GeV only
00286   text->AddText( labelname );
00287   text->Draw();
00288 }
00289 
00290 void
00291 FitHist::addParLabelToFitHist(const TH1F& hist)
00292 {
00293   if( hist.GetFunction(fitFuncName_.c_str()) ){
00294     TPaveText* pars  = new TPaveText(0.40, 0.55, 0.95, 0.70, "NDC");
00295     pars->SetBorderSize(   0 );
00296     pars->SetFillStyle( 4000 );
00297     pars->SetTextAlign(   12 );
00298     pars->SetTextSize ( 0.06 );
00299     pars->SetTextColor(    1 );
00300     pars->SetTextFont (   62 );
00301    
00302     char parstring[100]; 
00303     if(fitFuncType_==0){
00304       sprintf(parstring, "#mu=%3.2f ; #sigma=%3.2f",  
00305               hist.GetFunction(fitFuncName_.c_str())->GetParameter( 1 ), 
00306               hist.GetFunction(fitFuncName_.c_str())->GetParameter( 2 ) );
00307     }
00308     pars->AddText( parstring );
00309     pars->Draw();
00310   }
00311 }
00312 
00313 void 
00314 FitHist::fitAndDrawPs()
00315 {
00316   //-----------------------------------------------
00317   // define canvas
00318   //-----------------------------------------------
00319   TCanvas *canv = new TCanvas("canv", "fit histograms", 600, 600);
00320   setCanvasStyle( *canv );
00321   canv->SetGridx( 1 ); canv->SetLogx ( 0 );
00322   canv->SetGridy( 1 ); canv->SetLogy ( 0 );
00323 
00324   //-----------------------------------------------
00325   // loop histograms via the list of histogram
00326   // names stored in histList_, open a new page 
00327   // for each histogram; open a new file for each 
00328   // sample.
00329   //-----------------------------------------------
00330   std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00331   for(int idx=0; hist!=sampleList_.end(); ++hist, ++idx){
00332     //-----------------------------------------------
00333     // open output file
00334     //-----------------------------------------------
00335     TString output( writeTo_.c_str() );
00336     output += "/inspectFit_";
00337     if(outputLabelList_.size()>=sampleList_.size())
00338       output += outputLabelList_[idx];
00339     else
00340       output == idx;
00341     output += ".ps";
00342     TPostScript psFile( output, 111 ); //112 for portrat
00343     //---------------------------------------------
00344     // loop histograms via the list of histogram 
00345     // names stored in histList_, open a new page 
00346     // for each histogram; fit any histogram con-
00347     // taining the prefix 'fit'
00348     //---------------------------------------------
00349     int label=0;
00350     for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00351       TH1F& hfit = *((TH1F*)(*hist)[jdx]); //recieve histogram
00352       // prepare compare string
00353       TString cmp( hfit.GetName() );            // chop off root directory and 
00354       cmp.Remove(0, cmp.First('/')+1);          // file from histogram name  
00355       //prepare zip code
00356       TString zip( hfit.GetName() );            // chop off histogram name keep 
00357       zip.Remove(zip.First('/'), zip.Length()); // file and directory
00358       if ( cmp.BeginsWith(FitTarget::Fit) ){ //search for prefix 'fit'
00359         psFile.NewPage();
00360         if(verbose_){
00361           std::cout << std::endl << "about to fit histogram: " 
00362                << hfit.GetName() << " as " << cmp << std::endl; 
00363         }
00364         //-----------------------------------------------
00365         // determine proper bin by choping off everything
00366         // but the postfix which should correspond to the 
00367         // bin of NameScheme
00368         //-----------------------------------------------
00369         TString buffer( cmp );
00370         int bin = buffer.Remove(0, buffer.Last('_')+1).Atoi();
00371 
00372         hfit.SetLineWidth( 5 );
00373         hfit.SetLineColor( 4 );
00374         hfit.SetLineStyle( 1 );
00375         hfit.SetMarkerStyle( 20 );
00376         hfit.SetMarkerSize( 2.0 );
00377         hfit.SetMarkerColor(  4 );
00378         hfit.SetMaximum(1.5*hfit.GetMaximum());
00379         setFitHistogramAxes( hfit, label++ );
00380         //hfit.Fit( "gaus" );
00381 
00382         //do stable fit
00383         StabilizedGauss func(fitFuncName_.c_str(), fitFuncType_, fitFuncLowerBound_, fitFuncUpperBound_);
00384         func.fit(hfit);
00385 
00386         hfit.Draw("esame");
00387         //---------------------------------------------
00388         // add TPaveLabel to keep info of binning
00389         // recieved from target hitogram
00390         //---------------------------------------------
00391         addParLabelToFitHist( hfit );   
00392         addBinLabelToFitHist(*hist, bin, cmp, zip);
00393 
00394         //---------------------------------------------
00395         // add legend to fitted histogram
00396         //---------------------------------------------
00397         TLegend* leg = new TLegend(0.25,0.70,0.95,0.87);
00398         setLegendStyle( *leg );
00399         leg->AddEntry( &hfit, legend(idx).c_str(), "PL" );
00400         leg->AddEntry( hfit.GetFunction(fitFuncName_.c_str()), fitFuncTitle_.c_str(),  "L" );
00401         leg->Draw( "same" );
00402         canv->RedrawAxis( );
00403         canv->Update( );
00404         if(jdx<((*hist).GetEntriesFast()-1)) delete leg;
00405       }
00406     }
00407     psFile.Close();
00408   }
00409   canv->Close();
00410   delete canv;
00411 }
00412 
00413 void 
00414 FitHist::fitAndDrawEps()
00415 {
00416   //-----------------------------------------------
00417   // define canvas
00418   //-----------------------------------------------
00419   TCanvas *canv = new TCanvas("canv", "fit histograms", 600, 600);
00420   setCanvasStyle( *canv );
00421   canv->SetGridx( 1 ); canv->SetLogx ( 0 );
00422   canv->SetGridy( 1 ); canv->SetLogy ( 0 );
00423 
00424   //-----------------------------------------------
00425   // loop histograms via the list of histogram
00426   // names stored in histList_, open a new page 
00427   // for each histogram; open a new file for each 
00428   // sample.
00429   //-----------------------------------------------
00430   std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00431   for(int idx=0; hist!=sampleList_.end(); ++hist, ++idx){
00432     //---------------------------------------------
00433     // loop histograms via the list of histogram 
00434     // names stored in histList_, open a new page 
00435     // for each histogram; fit any histogram con-
00436     // taining the prefix 'fit'
00437     //---------------------------------------------
00438     int label=0;
00439     for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00440       TH1F& hfit = *((TH1F*)(*hist)[jdx]); //recieve histogram
00441       // prepare compare string
00442       TString cmp( hfit.GetName() );            // chop off root directory and 
00443       cmp.Remove(0, cmp.First('/')+1);          // file from histogram name  
00444       //prepare zip code
00445       TString zip( hfit.GetName() );            // chop off histogram name keep 
00446       zip.Remove(zip.First('/'), zip.Length()); // file and directory
00447       if ( cmp.BeginsWith(FitTarget::Fit) ){ //search for prefix 'fit'
00448         //-----------------------------------------------
00449         // open output file
00450         //-----------------------------------------------
00451         TString output( writeTo_.c_str() );
00452         output += "/";
00453         output += histList_[ jdx ];
00454         output += "_";
00455         if(outputLabelList_.size()>=sampleList_.size())
00456           output += outputLabelList_[idx];
00457         else
00458           output == idx;
00459         output += ".eps";
00460         TPostScript psFile( output, 113 );
00461         psFile.NewPage();
00462         
00463         //-----------------------------------------------
00464         // determine proper bin by choping off everything
00465         // bit the postfix which should correspond to the 
00466         // bin of NameScheme
00467           //-----------------------------------------------
00468         TString buffer( cmp );
00469         int bin = buffer.Remove(0, buffer.Last('_')+1).Atoi();
00470         
00471         hfit.SetLineWidth( 5 );
00472         hfit.SetLineColor( 4 );
00473         hfit.SetLineStyle( 1 );
00474         hfit.SetMarkerStyle( 20 );
00475         hfit.SetMarkerSize( 2.0 );
00476         hfit.SetMarkerColor(  4 );
00477         setFitHistogramAxes( hfit, label++ );
00478         //hfit.Fit( "gaus" );
00479         
00480         //do stable fit
00481         StabilizedGauss func(fitFuncName_.c_str(), fitFuncType_, fitFuncLowerBound_, fitFuncUpperBound_);
00482         func.fit(hfit);
00483         
00484         hfit.Draw("esame");
00485         //---------------------------------------------
00486         // add TPaveLabel to keep info of binning
00487         // recieved from target hitogram
00488         //---------------------------------------------
00489         addParLabelToFitHist( hfit );   
00490         addBinLabelToFitHist( *hist, bin, cmp, zip );
00491         
00492         //---------------------------------------------
00493         // add legend to fitted histogram
00494         //---------------------------------------------
00495         TLegend* leg = new TLegend(0.25,0.70,0.95,0.87);
00496         setLegendStyle( *leg );
00497         leg->AddEntry( &hfit, legend(idx).c_str(), "PL" );
00498         leg->AddEntry( hfit.GetFunction(fitFuncName_.c_str()), fitFuncTitle_.c_str(),  "L" );
00499         leg->Draw( "same" );
00500         canv->RedrawAxis( );
00501         canv->Update( );
00502         psFile.Close();
00503         delete leg;
00504       }
00505     }
00506   }
00507   canv->Close();
00508   delete canv;
00509 }
00510 
00511 void 
00512 FitHist::fillTargetHistograms()
00513 {
00514   //---------------------------------------------
00515   // fill all target histograms 
00516   //---------------------------------------------
00517   for(unsigned int idx=0; idx<targetHistList_.size(); ++idx){
00518     fillTargetHistogram(targetHistList_[idx]);
00519   }
00520 }
00521 
00522 void 
00523 FitHist::fillTargetHistogram(std::string& target)
00524 {
00525   //---------------------------------------------
00526   // loop array of samples via sampleList_, which 
00527   // contains all histograms in TObjectArrays for 
00528   // each sample
00529   //---------------------------------------------
00530   std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00531   for(; hist!=sampleList_.end(); ++hist){
00532     //---------------------------------------------
00533     // loop histograms & search for prefix target;
00534     // check if it's contained in list of valid 
00535     // targets first
00536     //---------------------------------------------
00537     TString buffer( target );
00538     if( isInFitTargetList(target) ){
00539       for(int jdx=0; jdx<(*hist).GetEntriesFast(); ++jdx){
00540         TH1F& htarget = *((TH1F*)(*hist)[jdx]);
00541         // prepare compare string
00542         TString cmp( htarget.GetName() );         // chop off root directory and 
00543         cmp.Remove(0, cmp.First('/')+1);          // file from histogram name  
00544         //prepare zip code
00545         TString zip( htarget.GetName() );         // chop off histogram name keep 
00546         zip.Remove(zip.First('/'), zip.Length()); // file and directory
00547         if( cmp.BeginsWith( buffer ) ){ //found target hist
00548           //---------------------------------------------
00549           // now fill the bins of the target hist
00550           //---------------------------------------------
00551           for(int kdx=0; kdx<htarget.GetNbinsX(); ++kdx){
00552             TH1F& hfit = findFitHistogram(*hist, zip, cmp, kdx);
00553             fillTargetHistogramBin(htarget, hfit, (kdx+1));
00554           }
00555         }
00556       }
00557     }
00558   }
00559 }
00560 
00561 void 
00562 FitHist::writeFitOutput()
00563 {
00564   //---------------------------------------------
00565   // write filled target histogram to output file
00566   // if specified
00567   //---------------------------------------------
00568   if( isOutputRequested() ){
00569     //-----------------------------------------------
00570     // open hist file for book keeping of hist names
00571     //-----------------------------------------------
00572     TString name( output_ );
00573     name.Remove(name.Last('.'), name.Length()); 
00574     name+=".hist"; //replace .root by .hist
00575     ofstream histFile(name, std::ios::out);
00576 
00577     //-----------------------------------------------
00578     // open root output file and create directory
00579     // if necessary
00580     //-----------------------------------------------
00581     TFile file( output_.c_str(), "update" );
00582     if( !file.GetDirectory(rootOutDir_.c_str()) ) 
00583       // create new directory if it does not yet exist
00584       file.mkdir( rootOutDir_.c_str(), rootOutDir_.c_str() );
00585     else 
00586       // clean-up directory if it was existing already 
00587       (file.GetDirectory(rootOutDir_.c_str()))->Delete("*;*");
00588     file.cd(rootOutDir_.c_str());
00589     
00590     // loop over requested target histogram labels if desired
00591     for(unsigned int jdx=0; jdx<targetHistList_.size(); ++jdx){
00592       TString buffer( targetHistList_[jdx] ); 
00593       //-----------------------------------------------
00594       // loop and write out histograms
00595       //-----------------------------------------------
00596       std::vector<TObjArray>::const_iterator hist = sampleList_.begin();
00597       for( ;hist!=sampleList_.end(); ++hist){
00598         for(int idx=0; idx<(int)histList_.size(); ++idx){  
00599           TString cmp( ((TH1F*)(*hist)[idx])->GetName() );
00600           if( cmp.BeginsWith( buffer ) ){ //found target hist
00601             histFile << ((TH1F*)(*hist)[idx])->GetName() << "\n";
00602             ((TH1F*)(*hist)[idx])->Write();
00603           }
00604         } // end of histList_ loop
00605       }
00606     } // end of targetHistList_ loop
00607     file.Close();
00608   }
00609 }