Advertisement
Guest User

Boltzmann Fit in C++ with ROOT Cern

a guest
Jun 27th, 2013
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <TApplication.h>
  4. #include <TF1.h>
  5. #include <TMath.h>
  6. #include <string>
  7. #include <TCanvas.h>
  8. #include <TGraph.h>
  9. #include <TAxis.h>
  10. #include <TGraphErrors.h>
  11. #include <TLegend.h>
  12. #include <TStyle.h>
  13.  
  14. #include <vector>
  15. #include <algorithm>
  16. #include <fstream>
  17. #include <sstream>
  18.  
  19. using namespace std;
  20.  
  21. template<typename T>
  22. T StringToNumber(const std::string& numberAsString)
  23. {
  24.     T valor;
  25.  
  26.     std::stringstream stream(numberAsString);
  27.     stream >> valor;
  28.     if (stream.fail()) {
  29.         std::runtime_error e(numberAsString);
  30.         throw e;
  31.     }
  32.     return valor;
  33. }
  34.  
  35. double Boltzmann(double* x, double* par){
  36.     // Boltzmann equation
  37.     Double_t xx = x[0];
  38.     Double_t A1 = par[0];
  39.     Double_t A2 = par[1];
  40.     Double_t x0 = par[2];
  41.     Double_t dx = par[3];
  42.  
  43.     return (par[0] - par[1])/(1+exp(xx-par[2])/par[3]) + par[1];
  44. }
  45.  
  46. struct data
  47. {
  48.     int rows;
  49.     int colunms;
  50.     vector<string> items;
  51. };
  52.  
  53. data readData(string fileloc){
  54.     cout << "Read in Date from File: " << fileloc << endl;
  55.     ifstream inputFile(fileloc);
  56.     string line;
  57.     string temp;
  58.     stringstream iss;
  59.     vector<string> items;
  60.     int rows = 0;
  61.     int colnms = 0;
  62.     while (getline(inputFile, line, '\n')) // Standard is \n...
  63.     {
  64.         iss << line;
  65.         while (getline(iss, temp, '\t')){
  66.             items.push_back(temp);
  67.             colnms++;
  68.         }
  69.         iss.clear();
  70.         rows++;
  71.     }
  72.  
  73.     colnms = colnms/rows;
  74.  
  75.     cout << "Reihen: " << rows << endl;
  76.     cout << "Spalten: " << colnms << endl << endl;
  77.  
  78.     data retVal = {rows, colnms, items};
  79.  
  80.  
  81.  
  82.     return retVal;
  83. }
  84.  
  85. int main(int argc, char **argv){
  86.    
  87.     /*
  88.         Read in DATA - tab is delimiter, be careful with your data (there has to be a \t before \n)
  89.     */
  90.  
  91.     data Data = readData("data.dat");
  92.  
  93.     vector<double> tempature;
  94.     vector<double> R_s;
  95.     vector<double> sigR_s;
  96.  
  97.     tempature.reserve(Data.rows);
  98.     R_s.reserve(Data.rows);
  99.     sigR_s.reserve(Data.rows);
  100.  
  101.  
  102.     double temp1,temp2,temp3 = 0;
  103.     for(unsigned int i = 0; i < Data.items.size(); ++i){
  104.         if((i % 3) == 0){
  105.             temp1 = StringToNumber<double>(Data.items[i]);
  106.             //cout << "temp1: " << temp1 << endl;
  107.             tempature.push_back(temp1);
  108.         } else if ((i % 3) == 1){
  109.             temp2 = StringToNumber<double>(Data.items[i]);
  110.             //cout << "temp2: " << temp2 << endl;
  111.             R_s.push_back(temp2);
  112.         } else if ((i % 3) == 2) {
  113.             temp3 = StringToNumber<double>(Data.items[i]);
  114.             //cout << "temp3: " << temp3 << endl;
  115.             sigR_s.push_back(temp3);
  116.         } else {
  117.             cout << "Something weird happens!";
  118.         }
  119.         temp1, temp2, temp3 = 0;
  120.     }
  121.  
  122.  
  123.     /*
  124.         Plot and Fit data
  125.     */
  126.  
  127.     double* Atemperature = &tempature[0];
  128.     double* AR_s = &R_s[0];
  129.     double* AsigR_s = &sigR_s[0];
  130.  
  131.     TApplication theApp("App",&argc,argv);
  132.  
  133.     TCanvas* can1 = new TCanvas("Squid Resistance", "Squid Resistance",0 ,0 ,900 , 650);
  134.     can1->SetGridx(1);
  135.     can1->SetGridy(1);
  136.  
  137.     TGraphErrors* errorgraph1 = new TGraphErrors(R_s.size(), Atemperature, AR_s, NULL, AsigR_s);
  138.     errorgraph1->SetTitle("Squid Resistance");
  139.     errorgraph1->GetXaxis()->SetTitle("Temperature [K]");
  140.     errorgraph1->GetYaxis()->SetTitle("Squid Resistance");
  141.     errorgraph1->SetMarkerStyle(kFullCircle);
  142.     errorgraph1->SetMarkerColor(kMagenta);
  143.  
  144.    
  145.     TF1* frenMittel = new TF1("BoltzmannEq", Boltzmann, 70, 150, 4);
  146.     frenMittel->SetLineColor(kRed);
  147.     frenMittel->SetParameters(12.19297,67.16567,98.93677,0.59634);
  148.     frenMittel->SetParNames("A1", "A2", "x0", "dx");
  149.     errorgraph1->Fit("BoltzmannEq");
  150.     errorgraph1->Draw("AP");
  151.  
  152.     gStyle->SetOptFit(1111);
  153.  
  154.     //TLegend* leg = new TLegend(0, 1, 0.3, 0.8);
  155.     //leg->SetHeader("Legend");
  156.     //leg->AddEntry(errorgraph1, "Data", "ep");
  157.     //leg->AddEntry(frenMittel, "Fit", "l");
  158.     //leg->Draw();
  159.  
  160.    
  161.     can1->Update();
  162.  
  163.  
  164.  
  165.    
  166.     theApp.Run();
  167.     return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement