Author: Lukas Breitwieser
In this tutorial we will show how to create a histogram of all agent diameters in the simulation and fit a function to the data.
Let's start by setting up BioDynaMo notebooks.
%jsroot on
gROOT->LoadMacro("${BDMSYS}/etc/rootlogon.C");
INFO: Created simulation object 'simulation' with UniqueName='simulation'.
We want to define a function that creates a cell at a certain position with diameters drawn from a gaussian distribution with $\mu=20$ and $\sigma=5$. The smallest diameter should be larger then $2.0$.
simulation.GetResourceManager()->ClearAgents();
auto rng = simulation.GetRandom()->GetGausRng(20, 5);
auto create_cell = [&](const Real3& position) {
Cell* cell = new Cell(position);
real_t diameter = std::max(2.0, rng.Sample());
cell->SetDiameter(diameter);
return cell;
};
Now that we defined create_cell
we can use it to create 400 cells on a plane with $z = 0$, $xmin = ymin = -200$, $xmax = ymax = 200$, and spacing = 20 in both dimensions.
auto f = [](const real_t* x, const real_t* params) { return 0.0; };
ModelInitializer::CreateAgentsOnSurface(f, {}, -200, 200, 20, -200, 200, 20,
create_cell);
simulation.GetScheduler()->FinalizeInitialization();
VisualizeInNotebook(300, 300);
The next step is to create a histogram object with 100 bins in the interval [2, 40].
The second line creates a function which fills the histogram with the diameter of the given agent.
The third line calls the function fill
for each agent, thus adding all diameters to the histogram.
TH1F h("myHisto","Agent Diameter Histogram;Diameter;Count", 100, 2, 40);
auto fill = L2F([&](Agent* a, AgentHandle){ h.Fill(a->GetDiameter()); });
simulation.GetResourceManager()->ForEachAgent(fill);
input_line_117:3:1: warning: 'fill' shadows a declaration with the same name in the 'std' namespace; use '::fill' to reference this declaration auto fill = L2F([&](Agent* a, AgentHandle){ h.Fill(a->GetDiameter()); }); ^
Let's draw the final histogram.
Before we have to create a TCanvas
object in order to display the result in this notebook.
We also modify the default color and create a grid.
TCanvas c("", "", 400, 300);
h.SetFillColor(kBlue - 10);
c.SetGrid();
h.Draw();
c.Draw();
Finally, we can try to fit a function to the data in the histogram.
Since we drew samples from a gaussian random number generator when we created our cells, we expect that a gaussian will fit our data.
h.Fit("gaus", "S");
h.Draw();
c.Draw();
**************************************** Minimizer is Minuit2 / Migrad Chi2 = 73.1872 NDf = 63 Edm = 3.38341e-08 NCalls = 79 Constant = 9.50544 +/- 0.709907 Mean = 19.7314 +/- 0.323457 Sigma = 5.40166 +/- 0.317423 (limited)