# Graphing a 3D Map of a Crystal

**URL:** <https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120>\
**Category:** Newbie\
**Created:** [July 11, 2019, 2:06pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120 "2019-07-11T14:06:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 11, 2019, 2:06pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/1 "2019-07-11T14:06:47Z")

</div>

Hi,

I am trying to develop an application to create a 3D map of optical transmittance as a function of position (example attached). Where would be a good place to start?

Thanks

 ![Transmittance_transverse_3D](https://root-forum.cern.ch/uploads/default/original/3X/c/6/c651a7cf057d4f933b943cc75861988c5d22e0ee.jpeg)

Root Version: 6.18/00  
Platform: OsX 10.14.5

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 11, 2019, 2:50pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/2 "2019-07-11T14:50:52Z")

</div>

May be many [3D polyline](https://root.cern/doc/master/classTPolyLine3D.html)

---

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 11, 2019, 5:48pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/3 "2019-07-11T17:48:33Z")

</div>

Do you happen to know the name of this diagram? I’m trying to find existing documentation on it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 22, 2019, 9:25am UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/4 "2019-07-22T09:25:23Z")

</div>

> [@Ethan\_Nguyen](#):
>
> Do you happen to know the name of this diagram?

I am not sure this kind of diagram has a name.  
If it has one I do not know it.

---

<div class="post-metadata">

**Author:** ![OSchaile](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/e495f1/32.png) [@OSchaile](https://root-forum.cern.ch/u/OSchaile)\
**Post date:** [July 23, 2019, 8:41am UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/5 "2019-07-23T08:41:35Z")

</div>

Hi,

how did you generate this plot (with ROOT?) ,  
if so: can you provide the macro  
or did you find it somewhere?  
Can you provide the URL of the source

Cheers  
Otto

---

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 23, 2019, 2:04pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/6 "2019-07-23T14:04:39Z")

</div>

Someone else made this plot with MatLab, but our lab lost the code. However, I replicated producing one face in ROOT with the TH2 Poly class. I’ll enclose the macro and photo.

```auto
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <fstream>

void Crystal72_2D_2(){

  struct input_t
    
  //Define array structure for input
    
  {       
    Double_t transmittance;
  };
 
  input_t input;
  
  // Reads data from ONE column of data
  
  FILE *f = fopen("Crystal72_2.txt","r");
  char line[400];
  
  // Creates array

  Double_t transArray[196];

  
  Int_t j=0;
  TFile *file = new TFile("test.root","RECREATE");
  for(Int_t i;fgets(&line[0],500,f);i++){  
    if (line[0] == '#'){
      continue;
    }else{
      sscanf(&line[0], "%lf", &input.transmittance);
    }

      transArray[j]=input.transmittance;
      j++;
      
   }

  
  //Finds the average
  float avg = 0.0; 
  float sum = 0.0;
  int size;
  
  size = sizeof(transArray) / sizeof(transArray[j]);

  for (int i = 0; i < size; ++i)
   {
   sum += transArray[i];
  }
  avg = ((float)sum)/size; //or cast sum to double before division

  //cout << "Average " << avg << "\n";

  // Calcuates deviance
  
  Double_t dev[size];
  
  for(int i = 0; i < size; i ++)
  {
      dev[i]= transArray[i] - avg;
      //cout << "Deviance " << dev[i] << "\n";
  }

  //Set-up canvas/graph

  TCanvas *ch2p2 = new TCanvas("ch2p2","ch2p2",1000,300);
  gStyle->SetPalette(kRainBow);
  TH2Poly *h2p = new TH2Poly();
  h2p->SetName("Boxes");
  h2p->SetTitle("Boxes");
  Int_t i,l;
  Int_t nx = 200;
  Int_t ny = 1;
  Double_t xval1,yval1,xval2,yval2;
  Double_t dx=1, dy=4;
  xval1 = .5;
  xval2 = dx + xval1;
  yval1 = -4;
  yval2 = dy;

  // In the x, there is a bin per mm.
  // In the y, there is only one bin. In this sense, "y" is meaningless.
  // Optical transmittance is reflected in the z.

  // This "for" loop adds "nx" bins in increments of "dx".
  for (i = 0; i<nx; i++) {
     h2p->AddBin(xval1, yval1, xval2, yval2);
     xval1 = xval2;
     xval2 = xval2+(dx);
     
     //Since y is meaningless, the weight is used to create the different shades of color
     
     h2p->Fill(i, 1 , dev[i]);
  }

  // Sets the range and removes the y-axis.
  
  h2p->GetXaxis()->SetRangeUser(0,200);
  h2p->GetYaxis()->SetRangeUser(0,100);
  h2p->GetZaxis()->SetRangeUser(-3,2);
  h2p->GetYaxis()->SetLabelOffset(999);
  h2p->GetYaxis()->SetLabelSize(0);
  

  // Sets the title, removes the stats box, and draws graph. 
  h2p->SetTitle("Sample 72 (620 nm) - Position vs Deviance in Optical Transmittance; Position(mm); TEST; TEST; TEST");
  h2p->SetStats(0);
  h2p->Draw("COLZ");
   
  return ch2p2;
}

```

 ![Sample%2072%20-%20OT%20Deviance](https://root-forum.cern.ch/uploads/default/original/3X/5/7/57886d4e11b27383bbb16aab01852ca27c03d560.png)  
[Crystal72\_2D\_2.C](https://root-forum.cern.ch/uploads/short-url/jBxndMTGeJie5cppKZyb0zLtq7S.C) (2.4 KB)  
[Crystal72\_2.txt](https://root-forum.cern.ch/uploads/short-url/p5hzFW1mVU8lOtvPq43sDN58A6N.txt) (2.1 KB)

---

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 23, 2019, 2:14pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/7 "2019-07-23T14:14:46Z")

</div>

Does anyone know how to label the weight (the colored bar on the right)? I tried doing this:

```auto
h2p->GetZaxis->SetTitle(Optical Transmittance Deviance (%));

```

However, I get “error: reference to non-static member function must be called; did you mean to call it with no arguments?”

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 23, 2019, 2:49pm UTC](https://root-forum.cern.ch/t/graphing-a-3d-map-of-a-crystal/35120/8 "2019-07-23T14:49:28Z")

</div>

```auto
  // Sets the title, removes the stats box, and draws graph.
  h2p->SetTitle("Sample 72 (620 nm) - Position vs Deviance in Optical Transmittance; Position(mm); Y Axis; Z Axis");
  h2p->GetZaxis()->SetTitleOffset(0.5);
  h2p->SetStats(0);
  h2p->Draw("COLZ");

```
