Ejemplo n.º 1
0
int main () {
  char user_input[20];
  long id;
  unsigned short boxes;
  float weight, weights[5];
  while (1) {
    printf( "a)load, b)unload, c)get weights d)count boxes, e)is train full f)reset q)quit \n");
    fgets(user_input,sizeof(user_input)-1, stdin);
    switch (user_input[0]) {
      case 'a':
        printf("id? ");
        fgets(user_input,sizeof(user_input)-1, stdin);
        id = atoi( user_input );
        printf( "boxes? " );
        fgets(user_input,sizeof(user_input)-1, stdin);
        boxes = atoi( user_input );
        printf( "weight? " );
        fgets(user_input,sizeof(user_input)-1, stdin);
        weight = (float) atof( user_input );
        LoadTrain(id,boxes,weight);
        break;
      case 'b':
        printf( "id? " );
        fgets(user_input,sizeof(user_input)-1, stdin);
        id = atoi( user_input );
        UnloadTrain(id, &boxes, &weight);
        printf( "Unloaded %d  boxes, %f weight\n", (int) boxes, weight );
        break;
      case 'c':
        GetWeights(weights);
        { int i;
          printf("weight summary: " );
          for (i=0 ; i<5; i++) {
            printf(" %f ", weights[i] );
          }
          printf("\n");
        }
        break;
      case 'd':
        printf("Total boxes %d \n", CountBoxes() ) ;
        break;
      case 'e':
        if ( IsTrainFull() ) {
          printf("Train is full\n" );
        } else {
          printf("Train is not full\n" );
        };
        break;
      case 'f':
        ClearTrain();
        break;
      case 'q':
        exit(1);
      default: 
        printf( "Unrecognized option\n" );
    }
  }
  return 0;
}
Ejemplo n.º 2
0
void Interpolate(int n) {
  FreeMem();
  GetMem(n);

  GetMaxMin();
  GetNodesAndValues(n);
  GetWeights(n);
}
Ejemplo n.º 3
0
void THSSimFit::RunWithTrees(){
  fWS->Print();
  cout<<"THSRooFit::RunWithBins(); number of bins "<<fDataBins->GetN()<<endl;
  MakeTreeBins();
  TFile* tfile=new TFile(fOutDir+"DataEntries.root");
  TObjArray* oa=(TObjArray*)tfile->Get("DataBins");
  for(Int_t i=0;i<fDataBins->GetN();i++){
    TTree* DataTree=dynamic_cast<TTree*>(oa->At(i));
    THSRooFit* rf=CreateSubFitBins(DataTree);
    //Make fit object for events in this bin
    //retrieve from saved entries file
    RooAbsPdf* pdf=0;
    //    while(pdf=(RooAbsPdf*)itPdf->Next()){
    TTree* MCTree=0;
    for(Int_t ip=0;ip<rf->GetPDFs().getSize();ip++){
      pdf=(RooAbsPdf*)&(rf->GetPDFs()[ip]);
      THSEventsPDF* mpdf=0;
      cout<<"THSRooFit::RunWithBins(); PDFs "<<pdf->GetName()<<endl;
      if(mpdf=dynamic_cast<THSEventsPDF*>(pdf)){//Only applies to THSEventsPDF
	TFile* mfile=new TFile(fOutDir+TString("Bins")+mpdf->GetName()+".root");
	TObjArray* oam=(TObjArray*)mfile->Get("MCModelBins");
	cout<<mfile<<" "<<oam<<endl;
	//note the cloned entry list is deleted in THSEventsPDF
	MCTree=dynamic_cast<TTree*>(oam->At(i)->Clone());
	MCTree->SetDirectory(0);
	//mpdf->SetEntryList(MCList);
	mpdf->SetTree(MCTree);
	mfile->Close();
	delete oam;
	delete mfile;
	cout<<MCTree<<" "<<mpdf<<" "<<rf->GetAuxVars()<<endl;
	//COULD send it to the farm here
	mpdf->AddSmearedModel(0,rf->GetAuxVars());
	//	mpdf->SetVarRange(TString(fExpLimits[il].GetName()).Remove(0,2))->setRange(fWS->var(fExpLimits[il].GetName())->getMin(),fWS->var(fExpLimits[il].GetName())->getMax());
	delete MCTree;
      }
    }
    rf->TotalPDF();
    rf->Fit();
    rf->PlotDataModel();
    rf->sPlot();
    rf->SavePlots(fOutDir+TString("Plots")+fDataBins->GetBinName(i)+".root");
    AddWeightMap(rf->GetWeights()->GetMap());
    rf->GetWeights()->PrintWeight();
    rf->RemoveDataSet();//save memory
    delete rf;
  }
   tfile->Close();
   delete oa;
   delete tfile;

  cout<<"THSRooFit::RunWithBins() Done all Fits "<<endl;
  //AddSubWeights();
  GetWeights()->Print();
  ExportWeightsToFile(fOutDir+TString("Weights")+".root");
}
Ejemplo n.º 4
0
void THSSimFit::RunWithBins(){
  MakeBins();
  cout<<"THSSimFit::RunWithBins(); number of bins "<<fDataBins->GetN()<<endl;
  TDirectory *saveDir=gDirectory;
  TFile* tfile=new TFile(fOutDir+"DataEntries.root");
  saveDir->cd();
  TObjArray* oa=(TObjArray*)tfile->Get("DataBins");
  for(Int_t i=0;i<fDataBins->GetN();i++){
    TEntryList* DataList=dynamic_cast<TEntryList*>(oa->At(i));
    fTree->SetEntryList(DataList);
    // THSRooFit* rf=CreateSubFitBins(TNamed(fDataBins->GetBinName(i),TString("")));
    THSRooFit* rf=CreateSubFitBins(fTree);
    rf->SetName(fDataBins->GetBinName(i));
    //iterate over models and set their entry lists id THSEventsPdf
    RooAbsPdf* pdf=0;
    for(Int_t ip=0;ip<rf->GetPDFs().getSize();ip++){
      pdf=(RooAbsPdf*)&(rf->GetPDFs()[ip]);
      THSEventsPDF* mpdf=0;
      cout<<"THSSimFit::RunWithBins(); PDFs "<<pdf->GetName()<<endl;
      if(mpdf=dynamic_cast<THSEventsPDF*>(pdf)){//Only applies to THSEventsPDF
      	TFile* mfile=new TFile(fOutDir+TString("Bins")+mpdf->GetName()+".root");
      	TObjArray* oam=(TObjArray*)mfile->Get("MCModelBins");
      	//note the cloned entry list is deleted in THSEventsPDF
       	mpdf->SetTree(dynamic_cast<THSEventsPDF*>(fPDFs.find(mpdf->GetName()))->GetTree());
      	TEntryList* MCList=dynamic_cast<TEntryList*>(oam->At(i)->Clone());
      	MCList->SetDirectory(0);
      	mpdf->SetEntryList(MCList);
      	mfile->Close();
	delete oam;
      	delete mfile;
      	//COULD send it to the farm here
      	mpdf->AddSmearedModel(0,rf->GetAuxVars());
      }
    }
     
    if(!rf->GetModel()) rf->TotalPDF();
    rf->Fit();
    rf->PlotDataModel();
    rf->sPlot();
    rf->SavePlots(fOutDir+TString("Plots")+fDataBins->GetBinName(i)+".root");
    AddWeightMap(rf->GetWeights()->GetMap());
    rf->GetWeights()->PrintWeight();
    rf->RemoveDataSet();//save memory
    delete rf;
  }
   tfile->Close();
   delete oa;
   delete tfile;

  cout<<"THSRooFit::RunWithBins() Done all Fits "<<endl;
  // AddSubWeights();
  GetWeights()->PrintWeight();
  ExportWeightsToFile(fOutDir+TString("Weights")+".root");
}
Ejemplo n.º 5
0
    VOID InsertWeight( DWORD dwIndex, DWORD dwBoneIndex, FLOAT fBoneWeight )
    {
        assert( dwBoneIndex < 256 );

        BYTE* pIndices = GetIndices( dwIndex );
        FLOAT* pWeights = GetWeights( dwIndex );

        for( DWORD i = 0; i < dwVertexStride; ++i )
        {
            if( fBoneWeight > pWeights[i] )
            {
                for( DWORD j = (dwVertexStride - 1); j > i; --j )
                {
                    pIndices[j] = pIndices[j - 1];
                    pWeights[j] = pWeights[j - 1];
                }
                pIndices[i] = (BYTE)dwBoneIndex;
                pWeights[i] = fBoneWeight;
                break;
            }
        }
    }
Ejemplo n.º 6
0
int main(int argc, char *argv[]){
  SetStyle();
  bool use_mc(false);
  char opt(' ');
  while(( opt=getopt(argc, argv, "m") )!=-1){
    switch(opt){
    case 'm':
      use_mc=true;
      break;
    default:
      break;
    }
  }

  {TTree crap;}
  std::vector<std::string> observed_names(0), signal_names;
  signal_names.push_back("raw_plots_and_values/SMS-TChiHH_2b2b_2J_mChargino-400_mLSP-1_TuneZ2star_8TeV-madgraph-tauola_Summer12-START53_V19_FSIM-v1_AODSIM_UCSB1871_v71_SyncSkim.root");
  if(use_mc){
    observed_names.push_back("raw_plots_and_values/BJets_HT-1000ToInf_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1895_v71_SyncSkim.root");//6
    observed_names.push_back("raw_plots_and_values/BJets_HT-250To500_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1893_v71_SyncSkim.root");//7
    observed_names.push_back("raw_plots_and_values/BJets_HT-500To1000_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1894_v71_SyncSkim.root");//8
    observed_names.push_back("raw_plots_and_values/TTJets_FullLeptMGDecays_8TeV-madgraph-tauola_Summer12_DR53X-PU_S10_START53_V7C-v2_AODSIM_UCSB1883_v71_SyncSkim.root");//9
    observed_names.push_back("raw_plots_and_values/TTJets_SemiLeptMGDecays_8TeV-madgraph-tauola_Summer12_DR53X-PU_S10_START53_V7C-v1_AODSIM_UCSB1884_v71_SyncSkim.root");//10
    observed_names.push_back("raw_plots_and_values/TTJets_HadronicMGDecays_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A_ext-v1_AODSIM_UCSB1880_v71_SyncSkim.root");//11
    observed_names.push_back("raw_plots_and_values/TTH_HToBB_M-125_8TeV-pythia6_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1855_v71_SyncSkim.root");//12
    observed_names.push_back("raw_plots_and_values/TTWJets_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1857_v71_SyncSkim.root");//13
    observed_names.push_back("raw_plots_and_values/TTZJets_8TeV-madgraph_v2_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1856_v71_SyncSkim.root");//14
    observed_names.push_back("raw_plots_and_values/Tbar_s-channel_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1864_v71_SyncSkim.root");//15
    observed_names.push_back("raw_plots_and_values/Tbar_t-channel_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1865_v71_SyncSkim.root");//16
    observed_names.push_back("raw_plots_and_values/Tbar_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1866_v71_SyncSkim.root");//17
    observed_names.push_back("raw_plots_and_values/T_s-channel_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1860_v71_SyncSkim.root");//18
    observed_names.push_back("raw_plots_and_values/T_t-channel_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1861_v71_SyncSkim.root");//19
    observed_names.push_back("raw_plots_and_values/T_tW-channel-DR_TuneZ2star_8TeV-powheg-tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1862_v71_SyncSkim.root");//20
    observed_names.push_back("raw_plots_and_values/W2JetsToLNu_TuneZ2Star_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1877_v71_SyncSkim.root");//21
    observed_names.push_back("raw_plots_and_values/W3JetsToLNu_TuneZ2Star_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1878_v71_SyncSkim.root");//22
    observed_names.push_back("raw_plots_and_values/W4JetsToLNu_TuneZ2Star_8TeV-madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1879_v71_SyncSkim.root");//23
    observed_names.push_back("raw_plots_and_values/ZJetsToNuNu_100_HT_200_TuneZ2Star_8TeV_madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1887_v71_SyncSkim.root");//24
    observed_names.push_back("raw_plots_and_values/ZJetsToNuNu_200_HT_400_TuneZ2Star_8TeV_madgraph_ext_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1889_v71_SyncSkim.root");//25
    observed_names.push_back("raw_plots_and_values/ZJetsToNuNu_200_HT_400_TuneZ2Star_8TeV_madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1888_v71_SyncSkim.root");//26
    observed_names.push_back("raw_plots_and_values/ZJetsToNuNu_400_HT_inf_TuneZ2Star_8TeV_madgraph_ext_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1891_v71_SyncSkim.root");//27
    observed_names.push_back("raw_plots_and_values/ZJetsToNuNu_400_HT_inf_TuneZ2Star_8TeV_madgraph_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1890_v71_SyncSkim.root");//28
    observed_names.push_back("raw_plots_and_values/WH_WToLNu_HToBB_M-125_8TeV-powheg-herwigpp_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1858_v71_SyncSkim.root");//29
    observed_names.push_back("raw_plots_and_values/ZH_ZToBB_HToBB_M-125_8TeV-powheg-herwigpp_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1868_v71_SyncSkim.root");//30
    observed_names.push_back("raw_plots_and_values/WW_TuneZ2star_8TeV_pythia6_tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1874_v71_SyncSkim.root");//31
    observed_names.push_back("raw_plots_and_values/ZZ_TuneZ2star_8TeV_pythia6_tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1876_v71_SyncSkim.root");//32
    observed_names.push_back("raw_plots_and_values/WbbJetsToLNu_Massive_TuneZ2star_8TeV-madgraph-pythia6_tauola_Summer12_DR53X-PU_S10_START53_V7A-v1_AODSIM_UCSB1859_v71_SyncSkim.root");//33
  }else{
    observed_names.push_back("raw_plots_and_values/MET_Run2012A-13Jul2012-v1_AOD_UCSB1852_v71_SyncSkim.root");//0
    observed_names.push_back("raw_plots_and_values/MET_Run2012B-13Jul2012-v1_AOD_UCSB1853_v71_SyncSkim.root");//1
    observed_names.push_back("raw_plots_and_values/MET_Run2012C-24Aug2012-v1_AOD_UCSB1854_v71_SyncSkim.root");//2
    observed_names.push_back("raw_plots_and_values/MET_Run2012C-PromptReco-v2_AOD_UCSB1867_v71_SyncSkim.root");//3
    observed_names.push_back("raw_plots_and_values/MET_Run2012D-PromptReco-v1_AOD_UCSB1869_v71_SyncSkim.root");//4
    observed_names.push_back("raw_plots_and_values/MET_Run2012D-PromptReco-v1_AOD_UCSB1870_v71_SyncSkim.root");//5
  }
  std::vector<TFile*> observed_files(0), signal_files(0);
  GetFiles(observed_files, observed_names);
  GetFiles(signal_files, signal_names);

  std::vector<TTree*> observed_trees(0), signal_trees(0);
  GetTrees(observed_trees, observed_files);
  GetTrees(signal_trees, signal_files);

  WeightCalculator weightCalc(19399.0);
  std::vector<double> observed_weights(0), signal_weights;
  GetWeights(observed_weights, observed_names, weightCalc);
  GetWeights(signal_weights, signal_names, weightCalc);

  std::vector<ABCDCount> observed_counts(0), signal_counts;
  GetCounts(observed_counts, observed_trees, observed_weights);
  GetCounts(signal_counts, signal_trees, signal_weights);

  KillTrees(observed_trees);
  KillTrees(signal_trees);
  KillFiles(observed_files);
  KillFiles(signal_files);

  ABCDCalculator abcd_calculator(observed_counts, signal_counts);
  const unsigned int num_params(abcd_calculator.GetNumberOfParameters());
  TMinuit minuit(num_params);
  minuit.SetPrintLevel(1);

  double strategy[1]={2.0};
  int useless(0);
  minuit.mnexcm("SET STR", strategy, 1, useless);
  MinuitFunctor<ABCDCalculator>::SetFunctor(&abcd_calculator);
  MinuitFunctor<ABCDCalculator>::SetNumParams(num_params);
  minuit.SetFCN(MinuitFunctor<ABCDCalculator>::Function);
  minuit.SetMaxIterations(std::numeric_limits<int32_t>::max());
  std::cout << "before" << std::endl;
  for(unsigned int i(0); i<num_params; ++i){
    SetParameter(i, num_params, minuit);
  }
  std::cout << "middle" << std::endl;
  minuit.Migrad();
  minuit.Migrad();
  minuit.Migrad();
  minuit.mnmnos();
  minuit.mnmnos();
  minuit.mnmnos();
  std::cout << "after" << std::endl;
}
Ejemplo n.º 7
0
void THSEventsFit::RunWeights(Int_t Nbins){
  if(!fDataBins) {cout<<"Error : Need to define at least 1 bin for this class e.g. RF->LoadBinVars(\"Eg\",1,3,4);"<<endl; exit(0);}
  DefineSets();
  MakeBins();
  cout<<"THSEventsFit::RunWithBins(); number of bins "<<fDataBins->GetN()<<endl;
  TDirectory *saveDir=gDirectory;
  THSBins* savedBins=new THSBins("HSDataBins",fOutDir+"DataEntries.root");
  fTree->SetBranchStatus("*",0);
  for(Int_t i=0;i<fVariables.getSize();i++){//only copy variable branches for speed
    fTree->SetBranchStatus(fVariables[i].GetName(),1);
  }
  //but always need ID branch
  if(fTree->GetBranch(fIDBranchName)){
   fTree->SetBranchStatus(fIDBranchName,1);
  }
 
  for(Int_t i=0;i<fDataBins->GetN();i++){
    Long64_t PdfN[GetPDFs().getSize()];
    for(Int_t ipdf=0;ipdf<GetPDFs().getSize();ipdf++)  PdfN[ipdf]=0;
    Long64_t TotalN=0;
    THSsPlot* rf=static_cast<THSsPlot*>(CreateSubFitBins(savedBins->GetBinnedTree(fTree,i),kFALSE));
    //iterate over models and set their entry lists id THSEventsPdf
    RooAbsPdf* pdf=0;
    for(Int_t ip=0;ip<rf->GetPDFs().getSize();ip++){
      pdf=(RooAbsPdf*)&(rf->GetPDFs()[ip]);
      THSEventsPDF* mpdf=0;
      cout<<"THSEventsFit::RunWithBins(); PDFs "<<pdf->GetName()<<endl;
      if(mpdf=dynamic_cast<THSEventsPDF*>(pdf)){//Only applies to THSEventsPDF
	THSBins* savedMCBins=new THSBins("MCModelBins",fOutDir+TString("Bins")+mpdf->GetName()+".root");
  	mpdf->SetTree(savedMCBins->GetBinnedTree(dynamic_cast<THSEventsPDF*>(fPDFs.find(mpdf->GetName()))->GetTree(),i));
	delete savedMCBins;
       	PdfN[ip]=mpdf->AddSmearedModel(0,rf->GetAuxVars());
	cout<<"CHCK "<<PdfN[ip]<<" "<<TotalN<<endl;
	TotalN+=PdfN[ip];
	cout<<"CHCK "<<PdfN[ip]<<" "<<TotalN<<endl;
	if(PdfN[ip]<10) {
	  cout<<"THSEventsFitfrom::RunWithBins() no events found for "<<fDataBins->GetBinName(i)<<" MODEL: "<<pdf->GetName()<<" probably no events kinmatically allowed in this bin" <<endl;
	  rf->GetPDFsp()->remove(rf->GetPDFs()[ip]);
	  rf->GetYieldsp()->remove(*(rf->GetWorkSpace()->var(fYld+pdf->GetName())));
	  rf->GetWorkSpace()->removeSet("Yields");
	  rf->GetWorkSpace()->removeSet("PDFs");
	  rf->GetWorkSpace()->defineSet("Yields",rf->GetYields());
	  rf->GetWorkSpace()->defineSet("PDFs",rf->GetPDFs());
	}
      }
    }
    for(Int_t ip=0;ip<GetPDFs().getSize();ip++){
 	pdf=(RooAbsPdf*)&(rf->GetPDFs()[ip]);
	THSEventsPDF* mpdf=0;
 	if(mpdf=dynamic_cast<THSEventsPDF*>(pdf)){
	  if(Double_t(PdfN[ip])/TotalN<fAccFrac){
	    cout<<" THSEventsFit::InitialiseFit()  "<<GetName()<<" MODEL: "<<GetPDFs()[ip].GetName()<<" N events below acceptance fraction so not considered significant enough background " <<PdfN[ip] <<" out of "<<TotalN<<" "<<fAccFrac<<endl;
	    GetPDFsp()->remove(GetPDFs()[ip]);
	    GetYieldsp()->remove(*(GetWorkSpace()->var(fYld+pdf->GetName())));
	    GetWorkSpace()->removeSet("Yields");
	    GetWorkSpace()->removeSet("PDFs");
	    GetWorkSpace()->defineSet("Yields",GetYields());
	    GetWorkSpace()->defineSet("PDFs",GetPDFs());	

	  }
	}
    }
    if(rf->GetPDFs().getSize()) rf->TotalPDF();
    else {
      cout<<"THSEventsFit::RunWithBins() no model found for "<<fDataBins->GetBinName(i)<<" probably no events kinmatically allowed in this bin" <<endl;
      rf->RemoveDataSet();//save memory
      delete rf;
      continue;
    }
    cout<<fData->numEntries()<<endl;
    if(rf->GetDataSet()->numEntries()<2) {delete rf;continue;}
    rf->FitMany(Nbins);
    rf->sPlot();
    rf->SavePlots("");//save plots for each bin fit
    if(rf->GetWeights()){
      rf->GetWeights()->PrintWeight();
      AddWeightMap(rf->GetWeights());
      rf->GetWeights()->Save();
    }
    rf->RemoveDataSet();//save memory
    delete rf;
  }
  //tfile->Close();
   delete savedBins;
   //delete tfile;
  fTree->SetBranchStatus("*",1);
  cout<<"THSRooFit::RunWithBins() Done all Fits "<<endl;
  // AddSubWeights();
  if(fWeights)GetWeights()->PrintWeight();
  if(fWeights) GetWeights()->SortWeights();
}
Ejemplo n.º 8
0
bool HClusterDlg::Run(vector<wxInt64>& clusters)
{
    // NOTE input_data should be retrieved first!!
    // get input: weights (auto)
    weight = GetWeights(columns);
    
    double* pwdist = NULL;
    if (dist == 'e') {
        pwdist = DataUtils::getPairWiseDistance(input_data, weight, rows,
                                                columns,
                                                DataUtils::EuclideanDistance);
    } else {
        pwdist = DataUtils::getPairWiseDistance(input_data, weight, rows,
                                                columns,
                                                DataUtils::ManhattanDistance);
    }

    fastcluster::auto_array_ptr<t_index> members;
    if (htree != NULL) {
        delete[] htree;
        htree = NULL;
    }
    htree = new GdaNode[rows-1];
    fastcluster::cluster_result Z2(rows-1);

    if (method == 's') {
        fastcluster::MST_linkage_core(rows, pwdist, Z2);
    } else if (method == 'w') {
        members.init(rows, 1);
        fastcluster::NN_chain_core<fastcluster::METHOD_METR_WARD, t_index>(rows, pwdist, members, Z2);
    } else if (method == 'm') {
        fastcluster::NN_chain_core<fastcluster::METHOD_METR_COMPLETE, t_index>(rows, pwdist, NULL, Z2);
    } else if (method == 'a') {
        members.init(rows, 1);
        fastcluster::NN_chain_core<fastcluster::METHOD_METR_AVERAGE, t_index>(rows, pwdist, members, Z2);
    }

    delete[] pwdist;

    std::stable_sort(Z2[0], Z2[rows-1]);
    t_index node1, node2;
    int i=0;
    fastcluster::union_find nodes(rows);
    for (fastcluster::node const * NN=Z2[0]; NN!=Z2[rows-1]; ++NN, ++i) {
        // Find the cluster identifiers for these points.
        node1 = nodes.Find(NN->node1);
        node2 = nodes.Find(NN->node2);
        // Merge the nodes in the union-find data structure by making them
        // children of a new node.
        nodes.Union(node1, node2);

        node2 = node2 < rows ? node2 : rows-node2-1;
        node1 = node1 < rows ? node1 : rows-node1-1;

        //cout << i<< ":" << node2 <<", " <<  node1 << ", " << Z2[i]->dist <<endl;
        //cout << i<< ":" << htree[i].left << ", " << htree[i].right << ", " << htree[i].distance <<endl;
        htree[i].left = node1;
        htree[i].right = node2;
        htree[i].distance = Z2[i]->dist;
    }
    clusters.clear();
    int* clusterid = new int[rows];
    cutoffDistance = cuttree (rows, htree, n_cluster, clusterid);
    for (int i=0; i<rows; i++) {
        clusters.push_back(clusterid[i]+1);
    }
    delete[] clusterid;
    clusterid = NULL;

    return true;
}