Ejemplo n.º 1
0
	void generateFitAndCalculatePvalue( XMLConfigReader * xmlFile, ParameterSet* parSet, MinimiserConfiguration * theMinimiser, FitFunctionConfiguration * theFunction, ParameterSet* argumentParameterSet, int nData, int repeats, vector<double> * pvalues)
	{
		PDFWithData * pdfAndData = xmlFile->GetPDFsAndData()[0];
		pdfAndData->SetPhysicsParameters( parSet );
		IPDF * pdf = pdfAndData->GetPDF();
		vector<IPDF*> vectorPDFs;
		vectorPDFs.push_back(pdf);
		PhaseSpaceBoundary * phase = xmlFile->GetPhaseSpaceBoundaries()[0];
		EdStyle * greigFormat = new EdStyle();
		greigFormat->SetStyle();

		ParameterSet* parSetFromFit = NULL;

		// Set up to be able to generate some MC data
		DataSetConfiguration * dataConfig = pdfAndData->GetDataSetConfig();
		dataConfig->SetSource( "Foam" );
		bool model = false;
		double pvalue = 0.;
		unsigned int ulTime = static_cast<unsigned int>( time( NULL ));
		for ( int i = 0; i < repeats; )
		{
			cout << "Ensemble " << i << endl;

			// First, generate some data from this PDF
			pdfAndData->SetPhysicsParameters( parSet );
			pdf->SetRandomFunction( (int)ulTime );
			pdf->SetMCCacheStatus( false );
			MemoryDataSet * subset = (MemoryDataSet*)dataConfig->MakeDataSet( phase, pdf, nData );
			vector<IDataSet*> vectorData;
			vectorData.push_back(subset);

			if ( model ) {
				// Use the same PDF for the large MC dataset (the Model approach)
				pdfAndData->SetPhysicsParameters( parSet );
			}
			else {
				// Fit the generated data (the Fit I approach)
				FitResult * gofResult = FitAssembler::DoFit( theMinimiser, theFunction, argumentParameterSet, vectorPDFs, vectorData, xmlFile->GetConstraints() );
				if ( gofResult->GetFitStatus() == 3 ) {
					delete parSetFromFit;
					parSetFromFit = gofResult->GetResultParameterSet()->GetDummyParameterSet();
					pdfAndData->SetPhysicsParameters( parSetFromFit );
					i++; // let us go to the next iteration of the loop so that we always get "repeats" good results
				}
			}
			// Generate large sample of MC
			ulTime = static_cast<unsigned int>( time( NULL ));
			pdf->SetRandomFunction( (int)ulTime );
			pdf->SetMCCacheStatus( false );
			MemoryDataSet * mcData = (MemoryDataSet*)dataConfig->MakeDataSet( phase, pdf, 10*nData );

			// Finally calculate the p-value relative to the large MC sample
			pvalue = GoodnessOfFit::pValueFromPoint2PointDissimilarity( subset, mcData );
			//pvalue = GoodnessOfFit::pValueFromPoint2PointDissimilarity( data, mcData );
			pvalues->push_back(pvalue);
			//delete subset;
			//delete mcData;
		}
		parSet = parSetFromFit;  // Need this so that we have the correct parameters during the second call of this function
	}
Ejemplo n.º 2
0
int main( int argc, char* argv[] )
{
    EdStyle* thisStyle = new EdStyle();

    thisStyle->SetStyle();

    gStyle->SetOptStat(0);

    //      Mute ROOT
    gErrorIgnoreLevel = kFatal;

    struct PlottingConfig thisConfig = PlottingConfig();

    ProcessInputOptions( thisConfig, argc, argv );

    if( !thisConfig.cutString.empty() && !thisConfig.weightName.empty() )
    {
        cout << "WARNING!!! You have made a Cut and are making a weighted plot!!!" << endl;
        cout << "Be Careful!!!" << endl;
    }

    TTree* inputTree = ROOT_File_Processing::GetFirstTree( thisConfig.inputFileName, "READ" );

    if( thisConfig.requestedParameters.empty() )
    {
        thisConfig.requestedParameters =  StringOperations::TString2string( TTree_Processing::get_branch_names( inputTree ) );
        if( !thisConfig.weightName.empty() )
        {
            vector<string> allParameters;
            for( unsigned int i=0; i< thisConfig.requestedParameters.size(); ++i )
            {
                if( thisConfig.requestedParameters[i] != thisConfig.weightName ) allParameters.push_back( thisConfig.requestedParameters[i] );
            }
            thisConfig.requestedParameters = allParameters;
        }
    }

    TString plotDistsOutput = "PlotDists_Output";

    cout << "Output Plots Sotred in: " << plotDistsOutput << endl;

    gSystem->mkdir( plotDistsOutput );

    vector<Double_t>* weightData = NULL;

    if( !thisConfig.weightName.empty() )
    {
        weightData = TTree_Processing::Buffer_Branch( inputTree, thisConfig.weightName, thisConfig.cutString );
    }

    for( unsigned int i=0; i< thisConfig.requestedParameters.size(); ++i )
    {
        string thisParameterName = thisConfig.requestedParameters[i];

        vector<Double_t>* thisData = TTree_Processing::Buffer_Branch( inputTree, thisParameterName, thisConfig.cutString );

        TString CanvasName = "Canvas_";
        CanvasName.Append( thisParameterName );

        TCanvas* thisCanvas = EdStyle::RapidFitCanvas( CanvasName );

        thisCanvas->SetLogy( thisConfig.setLogY );
        thisCanvas->SetLogx( thisConfig.setLogX );

        TString TH1Name = "TH1_";
        TH1Name.Append( thisParameterName );

        Double_t thisMin=0.;
        Double_t thisMax=0.;

        if( thisConfig.explicit_min <= -DBL_MAX )	thisMin = Template_Functions::get_minimum( *thisData );
        else						thisMin = thisConfig.explicit_min;

        if( thisConfig.explicit_max >= DBL_MAX )	thisMax = Template_Functions::get_maximum( *thisData );
        else						thisMax = thisConfig.explicit_max;

        TH1* thisTH1 = new TH1D( TH1Name, TH1Name, thisConfig.num_bins, thisMin, thisMax );

        for( unsigned int j=0; j< thisData->size(); ++j )
        {
            double weight=0.;
            if( weightData == NULL )	weight = 1.;
            else				weight = weightData->at(j);

            SafeFill( thisTH1, thisData->at(j), weight, thisMin, thisMax );

        }

        thisTH1->Draw( thisConfig.drawOptions.c_str() );
        thisCanvas->Update();

        thisTH1->GetXaxis()->SetRangeUser( thisMin, thisMax );
        thisCanvas->Update();

        TString XaxisLabel = EdStyle::GetParamRootName( thisParameterName );
        XaxisLabel.Append( " " );
        XaxisLabel.Append( EdStyle::GetParamRootUnit( thisParameterName ) );

        thisTH1->GetXaxis()->SetTitle( XaxisLabel );

        TString YaxisLabel = "Candidates / ( ";
        stringstream thisStream;
        thisStream << setprecision(2) << fabs(thisMax-thisMin)/((double)thisConfig.num_bins);
        YaxisLabel.Append( thisStream.str().c_str() );
        TString YaxisUnit = EdStyle::GetParamRootUnit( thisParameterName );
        if( StringProcessing::is_empty( YaxisUnit ) )
        {
        }
        else
        {
            YaxisLabel.Append( " " );
            YaxisLabel.Append( YaxisUnit );
        }
        YaxisLabel.Append( " )" );

        thisTH1->GetYaxis()->SetTitle( YaxisLabel );

        thisCanvas->Update();

        double ymin = thisTH1->GetBinContent( thisTH1->GetMinimumBin() ) - thisTH1->GetBinError( thisTH1->GetMinimumBin() );
        double ymax = thisTH1->GetBinContent( thisTH1->GetMaximumBin() ) + thisTH1->GetBinError( thisTH1->GetMaximumBin() );

        if( thisConfig.y_min > -DBL_MAX )		ymin = thisConfig.y_min;
        if( thisConfig.y_max < DBL_MAX )		ymax = thisConfig.y_max;

        thisTH1->GetYaxis()->SetRangeUser( ymin, ymax );
        thisTH1->SetMinimum( ymin );
        thisTH1->SetMaximum( ymax );

        thisCanvas->Update();

        TString localPDFName = thisParameterName.c_str();

        localPDFName.Append( "_Output.pdf" );

        TString TotalPDFName = plotDistsOutput;
        TotalPDFName.Append("/");
        TotalPDFName.Append( localPDFName );

        thisCanvas->Print( TotalPDFName );

        delete thisData;
    }

}