void PlotRakeBunch( const TString &sim, Int_t time, Int_t index = 0, const TString &options="") {
  
#ifdef __CINT__  
  gSystem->Load("libplasma.so");
#endif

  PlasmaGlob::Initialize();

  TString opt = options;
 
  // Palettes!
  gROOT->Macro("PlasmaPalettes.C");

  if(opt.Contains("grid")) {
    gStyle->SetPadGridX(1);
    gStyle->SetPadGridY(1);
  }
  gStyle->SetLabelFont(42,"xyz");
  gStyle->SetTextFont(62);
 

  // Load PData
  PData *pData = PData::Get(sim.Data());
  pData->LoadFileNames(time);
  if(!pData->IsInit()) return;

  Bool_t CYL = kFALSE;
  if(sim.Contains("cyl")) CYL = kTRUE; 
    
  Bool_t ThreeD = kFALSE;
  if(sim.Contains("3D")) ThreeD = kTRUE; 

  // Some plasma constants
  Double_t n0 = pData->GetPlasmaDensity();
  Double_t kp = pData->GetPlasmaK();
  Double_t skindepth = 1.;
  if(kp!=0.0) skindepth = 1/kp;
  Double_t E0 = pData->GetPlasmaE0();

  // Time in OU
  Float_t Time = pData->GetRealTime();
  // z start of the plasma in normalized units.
  Float_t zStartPlasma = pData->GetPlasmaStart()*kp;
  // z start of the beam in normalized units.
  Float_t zStartBeam = pData->GetBeamStart()*kp;
  
  opt += "comovcenter";

  // Centering time and z position:
  Double_t shiftz = pData->Shift(opt);
  TString sshiftz = Form("(x1-%f)",shiftz);

  if(opt.Contains("center")) {
    Time -= zStartPlasma;
    if(opt.Contains("comov"))      // Centers on the head of the beam.
      Time += zStartBeam;
  } 
  
  // Spatial coordinates intervals:
  Float_t x1Min = -4.3;
  Float_t x1Max = -3.9;
  Float_t x2Min = -0.5;
  Float_t x2Max =  0.5;
  Float_t x3Min = -0.5;
  Float_t x3Max =  0.5;

  // Momentum coordinates intervals:
  Float_t p1Min =  6500.01;
  Float_t p1Max =  10099.99;
  Float_t p2Min = -20.0;
  Float_t p2Max =  20.0;
  Float_t p3Min = -20.0;
  Float_t p3Max =  20.0;

  // Bining, intervals, labels, etc.
  Int_t xNbin = 200;
  Int_t yNbin = 200;

  if(sim.Contains("DR")) {
    xNbin = 200;
    yNbin = 200;
   
    // p1Min =  2200.01;
    // p1Max =  3399.99;
    // p1Min =  3500.01;
    // p1Max =  4999.99;
    p1Min =  1800.01;
    p1Max =  3999.99;

    // x1Min = -4.0;
    // x1Max = -3.2;
    x1Min = 8637.0;
    x1Max = 8639.0;
    x2Min = -0.5;
    x2Max =  0.5;
    x3Min = -0.5;
    x3Max =  0.5;
  } else if(sim.Contains("flash") && sim.Contains(".G.") ) {
    
    x1Min = -6.3;
    x1Max = -5.0;

    //p1Min =  0.001;
    p1Min =  650.001;
    p1Max =  1499.99;
    
  } else if(sim.Contains("facet_v23kA.G.A")) {
    x1Min = -7.8;
    x1Max = -7.1;

    x2Min = -1.0;
    x2Max =  1.0;
    x3Min = -1.0;
    x3Max =  1.0;
    // t=150
    p1Min =  1100.001;
    p1Max =  1999.99;
  } else if(sim.Contains("facet_v23kA.G")) {
    x1Min = -7.8;
    x1Max = -7.0;

    x2Min = -1.0;
    x2Max =  1.0;
    x3Min = -1.0;
    x3Max =  1.0;
    // t=60
     p1Min =  350.01;
     p1Max =  799.99;
    // t = 100
    // p1Min =  700.01;
    // p1Max =  1399.99;
    // t=105
    //p1Min =  800.001;
    //p1Max =  1399.99;
    // t=183
    // p1Min =  1500.001;
    // p1Max =  2499.99;
    // t=235
    // p1Min =  2000.01;
    // p1Max =  3499.99; 
    // t=310
    // p1Min =  2800.01;
    // p1Max =  4799.99; 
  }
  
  
  // Get phasespace histos
  Int_t Nspecies = pData->NSpecies();
  if(index>Nspecies-1) {
    return;
  }
  if(!pData->GetRawFileName(index)) {
    return;    
  }
  TH1F *hX1 = NULL;
  TH1F *hP1 = NULL;
  TH2F *hP1X1 = NULL;
  TH2F *hP2X2 = NULL;


  cout << Form("\n1. Getting data... ") << endl; 
  char cutString[512];
  sprintf(cutString,"TMath::Abs(q)*(%s > %.1f && %s < %.1f && x2 > %.1f && x2 < %.1f && x3 > %.1f && x3 < %.1f)",sshiftz.Data(),x1Min,sshiftz.Data(),x1Max,x2Min,x2Max,x3Min,x3Max); 
  TCut Cut = cutString;
  cout << Form("   (applied cut: \n %s)",cutString) << endl;
  TTree *tree = pData->GetTreeRaw(pData->GetRawFileName(index)->c_str(),opt);
  
  char hName[24];
  char dCommand[128];

  cout << Form("\n2. Dumping 1D histograms.. ") << endl;
  
  sprintf(hName,"hX1");
  hX1 = (TH1F*) gROOT->FindObject(hName);
  if(hX1) delete hX1;
  hX1 = new TH1F(hName,"",xNbin,x1Min,x1Max);
  sprintf(dCommand,"%s>>%s",sshiftz.Data(),hName);
  cout << Form("   - x1. ") << endl;    
  tree->Draw(dCommand,Cut,"goff");

  sprintf(hName,"hP1");
  hP1 = (TH1F*) gROOT->FindObject(hName);
  if(hP1) delete hP1;
  hP1 = new TH1F(hName,"",yNbin,p1Min,p1Max);
  sprintf(dCommand,"p1>>%s",hName);
  cout << Form("   - p1. ") << endl;    
  tree->Draw(dCommand,Cut,"goff");

  cout << Form("\n3. Dumping 2D histograms.. ") << endl;

  sprintf(hName,"hP1X1");
  hP1X1 = (TH2F*) gROOT->FindObject(hName);
  if(hP1X1) delete hP1X1;
  hP1X1 = new TH2F(hName,"",xNbin,x1Min,x1Max,yNbin,p1Min,p1Max);
  sprintf(dCommand,"p1:%s>>%s",sshiftz.Data(),hName);
  cout << Form("   - p1 vs. x1 ") << endl;    
  tree->Draw(dCommand,Cut,"goff");
  
  sprintf(hName,"hP2X2");
  hP2X2 = (TH2F*) gROOT->FindObject(hName);
  if(hP2X2) delete hP2X2;
  hP2X2 = new TH2F(hName,"",xNbin,x2Min,x2Max,yNbin,p2Min,p2Max);
  sprintf(dCommand,"p2:x2>>%s",hName);
  cout << Form("   - p2 vs. x2 ") << endl;    
  tree->Draw(dCommand,Cut,"goff");
  
  hX1->GetXaxis()->CenterTitle();
  hX1->GetYaxis()->CenterTitle();
  hX1->GetZaxis()->CenterTitle();
  hP1X1->GetXaxis()->CenterTitle();
  hP1X1->GetYaxis()->CenterTitle();
  hP1X1->GetZaxis()->CenterTitle();
  hP2X2->GetXaxis()->CenterTitle();
  hP2X2->GetYaxis()->CenterTitle();
  hP2X2->GetZaxis()->CenterTitle();
   
  // Integrated long. emittance:

  cout << Form("\n4. Calculating integrated quantities.. ") << endl;

  Double_t xmean = 0.0;
  Double_t ymean = 0.0;
  Double_t x2mean = 0.0;
  Double_t y2mean = 0.0;
  Double_t xymean = 0.0;
  Double_t Ntotal = 0.0;
  for(Int_t i=1;i<=xNbin;i++) {
    Double_t x = hP1X1->GetXaxis()->GetBinCenter(i);
    // if(x<xmin || x>xmax) continue;
    for(Int_t j=1;j<=yNbin;j++) {
      Double_t y = hP1X1->GetYaxis()->GetBinCenter(j);
      // if(y<ymin || y>ymax) continue;
      Double_t value = TMath::Abs(hP1X1->GetBinContent(i,j));
      xmean += x*value;
      ymean += y*value;
      x2mean += x*x*value;
      y2mean += y*y*value;
      xymean += x*y*value;

      Ntotal += value;
    }
  }
  
  xmean  /= Ntotal;
  ymean  /= Ntotal;
  x2mean /= Ntotal;
  y2mean /= Ntotal;
  xymean /= Ntotal;

  Double_t xrms2  = x2mean - xmean*xmean;
  Double_t yrms2  = y2mean - ymean*ymean;
  Double_t xrms   = TMath::Sqrt(xrms2);
  Double_t yrms   = TMath::Sqrt(yrms2);
  Double_t xyrms2 = xymean - xmean*ymean;

  Double_t emittance = TMath::Sqrt(xrms2*yrms2 - xyrms2*xyrms2);
  
  // cout << " xrms = " << xrms << endl;
  
  // Sliced emittance:
  // --------------------------------------------------------------------------

  cout << Form("\n5. Slicing ") << endl;


  // Bining for sliced quantities:
  // const Int_t SNbin = 7;
  // Float_t sBinLim[SNbin+1] = {-4.16,-4.14,-4.12,-4.10,-4.08,-4.06,-4.04,-4.02};
  // const Int_t SNbin = 7;
  // Float_t sBinLim[SNbin+1] = {-4.20,-4.17,-4.14,-4.12,-4.10,-4.08,-4.06,-4.00};
  // const Int_t SNbin = 8;
  // Float_t sBinLim[SNbin+1] = {-3.93,-3.87,-3.81,-3.75,-3.69,-3.63,-3.57,-3.51,-3.45};
  
  Int_t SNbin = 7;
  Float_t nsigma = 2;
  Float_t x1BinMin = -4.16;
  Float_t x1BinMax = -4.02;
  if(sim.Contains("DR")) {
    SNbin = 150; 
    nsigma = 1.4;
  } else if(sim.Contains("facet_v23kA.G.A")) {
    SNbin = 46;
    nsigma = 1;
    x1BinMin = -7.55;
    x1BinMax = -7.25;
  } else if(sim.Contains("facet_v23kA.G")) {
    SNbin = 10;
    nsigma = 1;
    x1BinMin = -7.55;
    x1BinMax = -7.15;
  }
  
  Float_t *sBinLim = new Float_t[SNbin+1];

  if(opt.Contains("rms")) {
    sBinLim[0] = xmean - nsigma*xrms;
    sBinLim[SNbin] = xmean + nsigma*xrms;
  } else {
    sBinLim[0] = x1BinMin;
    sBinLim[SNbin] = x1BinMax;
  }

  Float_t slbinSize = (sBinLim[SNbin] - sBinLim[0])/SNbin;
  
  for(Int_t i=1;i<SNbin;i++) {
    sBinLim[i] = sBinLim[i-1] + slbinSize;
  }
  
  TH1F **hP1sl = new TH1F*[SNbin];
  TH2F **hP2X2sl = new TH2F*[SNbin];

  cout << Form("\n   - Dumping in %i bins ",SNbin) << endl;

  
  for(Int_t k=0;k<SNbin;k++) {

    cout<< Form("k = %i : (x1 > %f && x1 < %f)",k,sBinLim[k],sBinLim[k+1]) << endl; 

    sprintf(hName,"hP2X2sl_%2i",k);
    hP2X2sl[k] = (TH2F*) gROOT->FindObject(hName);
    if(hP2X2sl[k]) delete hP2X2sl[k];
    hP2X2sl[k] = new TH2F(hName,"",xNbin,x2Min,x2Max,yNbin,p2Min,p2Max);

    char zCutString[128];
    sprintf(zCutString,"(%s > %f && %s < %f)",sshiftz.Data(),sBinLim[k],sshiftz.Data(),sBinLim[k+1]); 
    TCut zCut = zCutString;
    tree->Project(hName,"p2:x2",Cut + zCut);

    sprintf(hName,"hP1sl_%2i",k);
    hP1sl[k] = (TH1F*) gROOT->FindObject(hName);
    if(hP1sl[k]) delete hP1sl[k];
    hP1sl[k] = new TH1F(hName,"",yNbin,p1Min,p1Max);
    tree->Project(hName,"p1",Cut + zCut);

  }
  

  cout << Form("\n6. Calculating sliced quantities.. ") << endl;

  TGraph *gemit = NULL;
  TGraph *gYrms = NULL;
  TGraph *gErms = NULL;
  TGraph *gErmsB = NULL;
 
  Double_t * sxmean = new Double_t[SNbin];
  Double_t * symean = new Double_t[SNbin];
  Double_t * sx2mean = new Double_t[SNbin];
  Double_t * sy2mean = new Double_t[SNbin];
  Double_t * sxymean = new Double_t[SNbin];
  Double_t * sNtotal = new Double_t[SNbin];
  Double_t * sxrms2 = new Double_t[SNbin];  
  Double_t * syrms2 = new Double_t[SNbin]; 
  Double_t * sxrms = new Double_t[SNbin];  
  Double_t * syrms = new Double_t[SNbin];  
  Double_t * sxyrms2 = new Double_t[SNbin];
 
  Double_t * xbin = new Double_t[SNbin];
  Double_t * semittance = new Double_t[SNbin];

  Double_t * sNEtotal = new Double_t[SNbin]; 
  Double_t * sEmean = new Double_t[SNbin];
  Double_t * sE2mean = new Double_t[SNbin];
  Double_t * sErms = new Double_t[SNbin];

  for(Int_t k=0;k<SNbin;k++) {
    sxmean[k] = symean[k] = sx2mean[k] = sy2mean[k] = sxymean[k] 
      = sNtotal[k] = sxrms2[k] = syrms2[k] = sxrms[k] = syrms[k]
      = sxyrms2[k] = xbin[k] = semittance[k] = 0.0;
    sNEtotal[k] = sEmean[k] = sE2mean[k] = sErms[k] = 0.0;
    
    xbin[k] = (sBinLim[k] + sBinLim[k+1])/2.;
    
    for(Int_t i=1;i<=xNbin;i++) {
      Double_t x = hP2X2sl[k]->GetXaxis()->GetBinCenter(i);
      // if(x<xmin || x>xmax) continue;
      for(Int_t j=1;j<=yNbin;j++) {
	Double_t y = hP2X2sl[k]->GetYaxis()->GetBinCenter(j);
	// if(y<ymin || y>ymax) continue;
	Double_t value = TMath::Abs(hP2X2sl[k]->GetBinContent(i,j));
	sxmean[k] += x*value;
	symean[k] += y*value;
	sx2mean[k] += x*x*value;
	sy2mean[k] += y*y*value;
	sxymean[k] += x*y*value;
	
	sNtotal[k] += value;
      }	
    }
    
    for(Int_t i=1;i<=yNbin;i++) {
      Double_t y = hP1sl[k]->GetXaxis()->GetBinCenter(i);
      Double_t value = TMath::Abs(hP1sl[k]->GetBinContent(i));
      sEmean[k] += y*value;
      sE2mean[k] += y*y*value;
      sNEtotal[k] += value;
    }
    
    sxmean[k]  /= sNtotal[k];
    symean[k]  /= sNtotal[k];
    sx2mean[k] /= sNtotal[k];
    sy2mean[k] /= sNtotal[k];
    sxymean[k] /= sNtotal[k];
      
    sxrms2[k]  = sx2mean[k] - sxmean[k]*sxmean[k];
    syrms2[k]  = sy2mean[k] - symean[k]*symean[k];
    sxrms[k]   = TMath::Sqrt(sxrms2[k]);
    syrms[k]   = TMath::Sqrt(syrms2[k]);
    sxyrms2[k] = sxymean[k] - sxmean[k]*symean[k];
      
    semittance[k] = TMath::Sqrt(sxrms2[k]*syrms2[k] - sxyrms2[k]*sxyrms2[k]);

    sEmean[k]  /= sNEtotal[k];
    sE2mean[k] /= sNEtotal[k];
    sErms[k]   =  TMath::Sqrt(sE2mean[k] - sEmean[k]*sEmean[k]);
    
    
    cout << " Bunch properties: " << endl;
    cout << Form("  xMean = %7.3f   yMean = %7.3f",sxmean[k],symean[k]) << endl;
    cout << Form("  xRms  = %7.3f   yRms  = %7.3f",sxrms[k],syrms[k]) << endl;
    cout << Form("  Emittance = %7.3f",semittance[k]) << endl;

    cout << Form("  Emean = %7.3f   Erms = %7.3f",sEmean[k],sErms[k]) << endl;
    

  }

  // Charge
  Double_t dx1 = pData->GetDX(0);
  Double_t dx2 = pData->GetDX(1);
  Double_t dx3 = pData->GetDX(2);
  
  hX1->Scale(dx1*dx2*dx3);
  Double_t Charge = hX1->Integral();
  
  // Charge *= dx1*dx2*dx3;
 
  if(opt.Contains("units")) {
    Double_t dV = skindepth * skindepth * skindepth;
    Charge *= n0 * dV * (PConst::ElectronCharge/PUnits::picocoulomb);
    cout << Form(" Integrated charge (RAW) of specie %3i = %8f pC",index,Charge) << endl;
  } else {
    cout << Form(" Integrated charge (RAW) of specie %3i = %8.4f n0 * kp^-3",index,Charge) << endl;
  }
  

  // Chaning to user units: 
  // --------------------------
  
  if(opt.Contains("units") && n0) {
    
    Int_t NbinsX = hP1X1->GetNbinsX();
    Double_t xMin = skindepth * hP1X1->GetXaxis()->GetXmin() / PUnits::um;
    Double_t xMax = skindepth * hP1X1->GetXaxis()->GetXmax() / PUnits::um;
    Int_t NbinsY = hP1X1->GetNbinsY();
    Double_t yMin = hP1X1->GetYaxis()->GetXmin() * pData->GetBeamMass() / PUnits::GeV;
    Double_t yMax = hP1X1->GetYaxis()->GetXmax() * pData->GetBeamMass() / PUnits::GeV;
    hP1X1->SetBins(NbinsX,xMin,xMax,NbinsY,yMin,yMax);
    // Converting electron density
    Double_t dVb = skindepth * skindepth * skindepth;
    Double_t dX = (xMax-xMin)/NbinsX; 
    Double_t dE = (yMax-yMin)/NbinsY; 
    for(Int_t j=0;j<hP1X1->GetNbinsX();j++) {
      for(Int_t k=0;k<hP1X1->GetNbinsY();k++) {
	Double_t binValue =  fabs(hP1X1->GetBinContent(j,k) * dx1 * dx2 * dx3 * dVb * n0 *
				  (PConst::ElectronCharge/PUnits::picocoulomb));
     	//cout << Form(" value = %f",binValue) << endl;
	hP1X1->SetBinContent(j,k,binValue);
	
      }
    }
    
    if(opt.Contains("comov"))
      hP1X1->GetXaxis()->SetTitle("#zeta [#mum]");
    else
      hP1X1->GetXaxis()->SetTitle("z [#mum]");
    
    hP1X1->GetYaxis()->SetTitle("p_{z} [GeV/c]");
    
    hP1X1->GetZaxis()->SetTitle("dQ/d#zetadp_{z} [pC]");

    hP1->SetBins(NbinsY,yMin,yMax);
    hP1->GetYaxis()->SetTitle("p_{z} [GeV/c]");

    hX1->SetBins(NbinsX,xMin,xMax);
    Double_t binSize = (xMax - xMin)/NbinsX;

    Double_t dV = skindepth * skindepth * skindepth;
    Double_t  lightspeed =  PConst::c_light / (PUnits::um/PUnits::femtosecond);
    cout << Form("Speed of light = %f",lightspeed) << endl;
    hX1->Scale(TMath::Abs(n0 * dV * (PConst::ElectronCharge/PUnits::picocoulomb) * (lightspeed/binSize)));
    
    // hX1->Scale(TMath::Abs((PUnits::um/skindepth)*(PConst::ElectronCharge/PUnits::picocoulomb)*PConst::c_light));
    
    // hX1->GetYaxis()->SetTitle("I[kA]");
    hX1->GetYaxis()->SetTitle("");
    if(opt.Contains("comov"))
      hX1->GetXaxis()->SetTitle("#zeta [#mum]");
    else
      hX1->GetXaxis()->SetTitle("z [#mum]");
    
    
    xmean *= skindepth / PUnits::um;
    xrms  *= skindepth / PUnits::um;
    ymean *= pData->GetBeamMass() / PUnits::GeV;
    yrms  *= pData->GetBeamMass() / PUnits::GeV;
    
    emittance *= (skindepth / PUnits::um);
    
    for(Int_t k=0;k<SNbin;k++) {
      xbin[k] *= skindepth / PUnits::um;

      sxmean[k] *= skindepth / PUnits::um;
      sxrms[k]  *= skindepth / PUnits::um;
      symean[k] *= pData->GetBeamMass() / PUnits::MeV;
      syrms[k] *= pData->GetBeamMass() / PUnits::MeV;
      
      semittance[k] *= (skindepth / PUnits::um);

      sEmean[k] *= pData->GetBeamMass() / PUnits::GeV;
      sErms[k]  *= 100 * pData->GetBeamMass() / PUnits::GeV / ymean; //sEmean[k];
      // sErms[k]  *= pData->GetBeamMass() / PUnits::GeV;

    }

  }


  // Create the graph with the emittances:
  gemit = new TGraph(SNbin,xbin,semittance);
  gYrms = new TGraph(SNbin,xbin,sxrms);
  gErms = new TGraph(SNbin,xbin,sErms);
  
  
  // Profile energy for p1 vs x1:
  TString pname = hP1X1->GetName();
  pname += "_pfx";
  TProfile *hP1X1prof = (TProfile*) gROOT->FindObject(pname.Data());
  if(hP1X1prof) { delete hP1X1prof; hP1X1prof = NULL; }
  hP1X1prof = hP1X1->ProfileX("_pfx",1,-1,"s");

  // get the errors from the profile:
  Int_t NP1X1Bins = hP1X1prof->GetNbinsX();
  Double_t *x1bins = new Double_t[NP1X1Bins];
  Double_t *eRms   = new Double_t[NP1X1Bins];
  for(Int_t i=1;i<=hP1X1prof->GetNbinsX();i++) {
    x1bins[i] = hP1X1prof->GetBinCenter(i);
    eRms[i] = 100 * hP1X1prof->GetBinError(i) / hP1X1prof->GetBinContent(i);
  }
  gErmsB = new TGraph(NP1X1Bins,x1bins,eRms);
  
  // Vertical Energy histogram:
  // --------------------------------------------------------------------------------   
  TGraph *gP1left = NULL;
  if(hP1) {
    Double_t *yarray   = new Double_t[yNbin];
    Double_t *xarray   = new Double_t[yNbin];
    
    // This is for the right side:
    // Double_t xMax = x1Min + (x1Max-x1Min) * 0.9;
    // Double_t xMin = x1Max;
    // And this for left:
    Double_t xMin = hX1->GetXaxis()->GetXmin();
    Double_t xMax = hX1->GetXaxis()->GetXmin() + (hX1->GetXaxis()->GetXmax()
						  -hX1->GetXaxis()->GetXmin()) * 0.2;
    Double_t EneMax = hP1->GetMaximum();
    // cout << Form("  EneMax = %f ", EneMax) << endl;
 
    for(Int_t j=0; j<yNbin; j++) {
      yarray[j] = hP1->GetBinCenter(j+1);
      xarray[j] = ((xMax-xMin)/EneMax)*hP1->GetBinContent(j+1) + xMin;

      // cout << Form("  x = %f  y = %f ", xarray[j],yarray[j]) << endl;
    }

    gP1left = new TGraph(yNbin,xarray,yarray);
    gP1left->SetLineColor(PlasmaGlob::elecLine);
    gP1left->SetLineWidth(2);
    gP1left->SetFillStyle(1001);
    gP1left->SetFillColor(PlasmaGlob::elecFill);
       
  }

  

  // Plotting
  // -----------------------------------------------
    
  // Canvas setup
  // Create the canvas and the pads before the Frame loop
  // Resolution:
  Int_t sizex = 800;
  Int_t sizey = 600;
  if(opt.Contains("hres")) {
    Int_t sizex = 1600;
    Int_t sizey = 1200;    
  }
  
  TCanvas *C = new TCanvas("C1","Evolution of Injection",sizex,sizey);
  C->cd();

  // Set palette:
  PPalette * pPalette = (PPalette*) gROOT->FindObject("electron");
  pPalette->cd();

  // Float_t Max  = hP1X1->GetMaximum();
  // Float_t Min  = hP1X1->GetMinimum();
  
  // hP1X1->GetZaxis()->SetRangeUser(Min,Max); 


  // Text objects
  TPaveText *textTime = new TPaveText(0.55,0.8,0.82,0.9,"NDC");
  PlasmaGlob::SetPaveTextStyle(textTime,32); 
  textTime->SetTextColor(kGray+2);
  char ctext[128];
  if(opt.Contains("units") && pData->GetPlasmaDensity()) 
    sprintf(ctext,"z = %5.1f mm", Time * skindepth / PUnits::mm);
  else
    sprintf(ctext,"t = %5.1f #omega_{p}^{-1}",Time);
  textTime->AddText(ctext);
 
  TPaveText *textDen = new TPaveText(0.15,0.85,0.48,0.9,"NDC");
  PlasmaGlob::SetPaveTextStyle(textDen,12); 
  textDen->SetTextColor(kOrange+10);
  if(opt.Contains("units") && pData->GetPlasmaDensity())
    sprintf(ctext,"n_{0} = %5.2f x 10^{17} / cc", n0 / (1e17/PUnits::cm3));
  else if(pData->GetBeamDensity() && pData->GetPlasmaDensity())
    sprintf(ctext,"n_{b}/n_{0} = %5.2f", pData->GetBeamDensity()/n0);
  textDen->AddText(ctext);

  TPaveText *textWav = new TPaveText(0.15,0.2,0.48,0.25,"NDC");
  PlasmaGlob::SetPaveTextStyle(textWav,12); 
  textWav->SetTextColor(kGray+2);
  sprintf(ctext,"#lambda_{p} = %5.2f #mum", pData->GetPlasmaWaveLength() / PUnits::um);
  textWav->AddText(ctext);

  TPaveText *textCharge = new TPaveText(0.15,0.25,0.48,0.3,"NDC");
  PlasmaGlob::SetPaveTextStyle(textCharge,12); 
  textCharge->SetTextColor(kGray+2);
  if(opt.Contains("units") && pData->GetPlasmaDensity())
    sprintf(ctext,"Charge = %5.2f pC", Charge);
  else
    sprintf(ctext,"Charge = %5.2f n0#timeskp^{-3}", Charge);    
  textCharge->AddText(ctext);

  TPaveText *textMom = new TPaveText(0.55,0.03,0.82,0.13,"NDC");
  PlasmaGlob::SetPaveTextStyle(textMom,32); 
  textMom->SetTextColor(kGray+3);
  textMom->SetTextFont(62);
  if(opt.Contains("units") && pData->GetPlasmaDensity())
    sprintf(ctext,"#LTp_{z}#GT = %5.2f GeV/c", ymean);
  else
    sprintf(ctext,"Mom = %5.2f mc", ymean);    
  textMom->AddText(ctext);


  TPaveText *textInfo = new TPaveText(0.55,0.52,0.82,0.75,"NDC");
  PlasmaGlob::SetPaveTextStyle(textInfo,32); 
  textInfo->SetTextColor(kGray+2);
  textInfo->SetTextFont(42);
  sprintf(ctext,"Charge = %5.2f pC",Charge);
  textInfo->AddText(ctext);
  sprintf(ctext,"#LT#zeta#GT_{rms} = %5.2f #mum",xrms);
  textInfo->AddText(ctext);
  sprintf(ctext,"#LTp_{z}#GT_{rms} = %5.2f GeV/c",yrms);
  textInfo->AddText(ctext);
  // sprintf(ctext,"#epsilon_{N} = %5.2f #mum",emittance);
  // textInfo->AddText(ctext);
  
  // Setup Pad layout:
  const Int_t NFrames = 2;
  TPad **pad = new TPad*[NFrames];
  TH1F *hFrame[NFrames];

  Double_t lMargin = 0.15;
  Double_t rMargin = 0.18;
  Double_t bMargin = 0.15;
  Double_t tMargin = 0.04;
  Double_t vSpacing = 0.00; 
  Double_t hStep = (1.-lMargin-rMargin);
  Double_t vStep = (1.- bMargin - tMargin - (NFrames-1) * vSpacing) / NFrames;
  
  Float_t vposd = 0.0;
  Float_t vposu = 0.0;
  Float_t vmard = 0.0;
  Float_t vmaru = 0.0;
  Float_t vfactor = 0.0;
  Float_t hposl = 0.0;
  Float_t hposr = 1.0;
  Float_t hmarl = lMargin;
  Float_t hmarr = rMargin;
  Float_t hfactor = 1.0;
  
  // Actual Plotting!
  // ------------------------------------------------------------
  
  
  for(Int_t k=0;k<NFrames;k++) {

    // PLOTTING!
    if(k==0) {
      vposd = 0.0;
      vposu = bMargin + vStep;
      vfactor = vposu-vposd;  
      vmard = bMargin / vfactor;
      vmaru = 0.0;
    } else if(k == NFrames-1) {
      vposd = vposu + vSpacing;
      vposu = vposd + vStep + tMargin;
      vfactor = vposu-vposd;   
      vmard = 0.0;
      vmaru = tMargin / (vposu-vposd);
    } else {
      vposd = vposu + vSpacing;
      vposu = vposd + vStep; 
      vfactor = vposu-vposd;
      vmard = 0.0;
      vmaru = 0.0;
    } 
    hfactor = hposl-hposr;

    char name[16];
    sprintf(name,"pad_%i",k);
    pad[k] = new TPad(name,"",hposl,vposd,hposr,vposu);
    // // cout << Form("%f %f %f %f",hposl,vposd,hposr,vposu) << endl;
    // // cout << Form("%f %f %f %f",hmarl,vmard,hmarr,vmaru) << endl;
    pad[k]->SetLeftMargin(hmarl);
    pad[k]->SetRightMargin(hmarr);  
    pad[k]->SetBottomMargin(vmard);
    pad[k]->SetTopMargin(vmaru);
    pad[k]->SetFrameLineWidth(3);
    
    sprintf(name,"hFrame_%i",k);  
    hFrame[k] = (TH1F*) gROOT->FindObject(name);
    if(hFrame[k]) delete hFrame[k];
    hFrame[k] = (TH1F*) hX1->Clone(name);
    hFrame[k]->Reset();
    
    hFrame[k]->GetXaxis()->CenterTitle();
    hFrame[k]->GetYaxis()->CenterTitle();
    hFrame[k]->GetZaxis()->CenterTitle();
    hFrame[k]->SetLabelFont(42,"xyz");
    hFrame[k]->SetTitleFont(42,"xyz");
  
    hFrame[k]->SetNdivisions(505,"xyz");
  
    hFrame[k]->SetTickLength(0.04,"xyz");
    hFrame[k]->SetTickLength(0.04*vfactor,"y");
  
    hFrame[k]->GetYaxis()->SetLabelSize(0.04/vfactor);
    hFrame[k]->GetYaxis()->SetLabelOffset(0.02);
  
    hFrame[k]->GetYaxis()->SetTitleSize(0.05/vfactor);
    hFrame[k]->GetYaxis()->SetTitleOffset(1.2*vfactor);

    if(k==0) {  
      hFrame[k]->GetXaxis()->SetLabelSize(0.08);
      hFrame[k]->GetXaxis()->SetLabelOffset(0.02);
      hFrame[k]->GetXaxis()->SetTitleSize(0.12);
      hFrame[k]->GetXaxis()->SetTitleOffset(1.0);
    } else {
      hFrame[k]->GetXaxis()->SetLabelSize(0.0);
      hFrame[k]->GetXaxis()->SetTitleSize(0.0);
    }


  }

  // Ranges!!
  Double_t yMin =  999.9;
  Double_t yMax =  -999.9;
  for(Int_t k=0;k<SNbin;k++) {
    if(semittance[k]<yMin)
      yMin = semittance[k];
    
    if(semittance[k]>yMax)
      yMax = semittance[k];

    if(sErms[k]<yMin)
      yMin = sErms[k];
    
    if(sErms[k]>yMax)
      yMax = sErms[k];
  }

  for(Int_t k=1;k<=xNbin;k++) {
    Double_t value = hX1->GetBinContent(k);
    if(value<yMin)
      yMin = value;
    
    if(value>yMax)
      yMax = value;

  }
  
  C->cd();

  pad[1]->Draw();
  pad[1]->cd();

  if(opt.Contains("logz")) {
    gPad->SetLogz(1);
  } else {
    gPad->SetLogz(0);
  }
  
  hFrame[1]->GetYaxis()->SetRangeUser(hP1X1->GetYaxis()->GetXmin(),hP1X1->GetYaxis()->GetXmax());

  if(opt.Contains("units"))
    hFrame[1]->GetYaxis()->SetTitle("p_{z} [GeV/c]");
  
  hFrame[1]->Draw();

  gP1left->SetLineWidth(2);
  gP1left->Draw("F");
  gP1left->Draw("L");

  TLine lZmean(xmean,hP1X1->GetYaxis()->GetXmin(),xmean,hP1X1->GetYaxis()->GetXmax());
  lZmean.SetLineColor(kGray+2);
  lZmean.SetLineStyle(2);
  lZmean.Draw();

  TLine lPmean(hP1X1->GetXaxis()->GetXmin(),ymean,hP1X1->GetXaxis()->GetXmax(),ymean);
  lPmean.SetLineColor(kGray+2);
  lPmean.SetLineStyle(2);
  lPmean.Draw();


  hP1X1->GetYaxis()->SetNdivisions(503);
  hP1X1->GetZaxis()->SetNdivisions(503);
  hP1X1->GetZaxis()->SetRangeUser(0.001*hP1X1->GetMaximum(),hP1X1->GetMaximum());
  hP1X1->GetZaxis()->SetLabelSize(0.05);
  hP1X1->GetZaxis()->SetTitleSize(0.04);
  hP1X1->GetZaxis()->SetTitleFont(42);

  hP1X1->Draw("colzsame");
  // hP1X1->SetContour(20);
  // hP1X1->Draw("contzsame");
  // hP1X1prof->SetMarkerStyle(1);
  // hP1X1prof->SetLineWidth(2);
  // hP1X1prof->Draw("zsame");

  //hP1->Draw("C");
  
  gPad->Update();

  TPaletteAxis *palette = (TPaletteAxis*)hP1X1->GetListOfFunctions()->FindObject("palette");
  if(palette) {
    Float_t y1 = gPad->GetBottomMargin();
    Float_t y2 = 1 - gPad->GetTopMargin();
    Float_t x1 = 1 - gPad->GetRightMargin();
    palette->SetY2NDC(y2 - 0.04);
    palette->SetY1NDC(y1 + 0.04);
    palette->SetX1NDC(x1 + 0.01);
    palette->SetX2NDC(x1 + 0.04);
    
    palette->SetLabelFont(42);
    
    palette->SetLabelSize(0.08);
    //palette->SetLabelOffset(0.005/vfactor);
    palette->SetTitleSize(0.10);
    //    palette->SetTitleOffset(9999.0*vfactor);
    palette->SetTitleOffset(0.6);
    
    palette->SetBorderSize(2);
    palette->SetLineColor(1);
  }


  textTime->Draw();
  textInfo->Draw();
  // textCharge->Draw();
  textMom->Draw();
   
  gPad->RedrawAxis(); 

  // Bottom plot -----------------------------------------

  C->cd();

  pad[0]->Draw();
  pad[0]->cd();

  hFrame[0]->GetYaxis()->SetRangeUser(0.0,1.1*yMax);
  hFrame[0]->Draw();
    

  hX1->SetLineWidth(2);
  hX1->SetFillStyle(1001);
  hX1->SetFillColor(PlasmaGlob::elecFill);
  // hX1->SetLineColor(kBlue);
  hX1->Draw("FL same");
  //hX1->Draw("C");

  TLine lZmean2(xmean,0.0,xmean,1.1*yMax);
  lZmean2.SetLineColor(kGray+2);
  lZmean2.SetLineStyle(2);
  lZmean2.Draw();

  Int_t markerSize = 1.2; 
  Int_t lineWidth  = 2.0;   

  gYrms->SetMarkerStyle(20);
  gYrms->SetLineStyle(1);
  gYrms->SetMarkerColor(kGray+1);
  gYrms->SetMarkerSize(markerSize); 
  gYrms->SetLineColor(kGray+1);
  gYrms->SetLineWidth(lineWidth);
  gYrms->Draw("PL");
  
  // hP2X2sl[0]->Draw("colz");
  gemit->SetMarkerStyle(20);
  //  gemit->SetMarkerColor(kMagenta-2);
  gemit->SetMarkerColor(kGray+2);
  gemit->SetMarkerSize(markerSize);
  gemit->SetLineWidth(lineWidth);
  gemit->SetLineColor(kGray+2);
  gemit->Draw("PL");

  gErms->SetMarkerStyle(20);
  gErms->SetMarkerSize(markerSize);
  gErms->SetMarkerColor(kOrange+10);
  gErms->SetLineColor(kOrange+10);
  gErms->SetLineWidth(lineWidth);
  gErms->Draw("PL");


  TLegend *Leg;
  if(!sim.Contains("DR")) 
    Leg=new TLegend(0.55,0.60,1 - gPad->GetRightMargin() - 0.02,0.95);
  else
    Leg=new TLegend(gPad->GetLeftMargin() + 0.02, 1.0-gPad->GetTopMargin()-0.20,
		    gPad->GetLeftMargin() + 0.30, 1.0-gPad->GetTopMargin()-0.02);
  
  
  PlasmaGlob::SetPaveStyle(Leg);
  Leg->SetTextAlign(12);
  Leg->SetTextColor(kGray+3);
  Leg->SetTextFont(42);
  Leg->SetLineColor(1);
  Leg->SetBorderSize(0);
  Leg->SetFillColor(0);
  Leg->SetFillStyle(1001);
  Leg->SetFillStyle(0); // Hollow
 
  Leg->AddEntry(hX1  ,"Current [kA]","L");
  //  Leg->AddEntry(gErms,"Energy spread (GeV)","PL");
  Leg->AddEntry(gErms,"Energy spread [%]","PL");
  Leg->AddEntry(gemit,"Emittance [#mum]","PL");
  Leg->AddEntry(gYrms,"Bunch width [#mum]","PL");
 
  Leg->Draw();

  gPad->RedrawAxis(); 

  gPad->Update();
  

  // Print to file --------------------------------------
  
  C->cd();
  
  // Print to a file
  // Output file
  TString fOutName = Form("./%s/Plots/RakeBunch/RakeBunch",sim.Data());
  fOutName += Form("-%s_%i",sim.Data(),time);

  PlasmaGlob::imgconv(C,fOutName,opt);
  // ---------------------------------------------------------
  
}
void PlotChargeFieldFocus2D( const TString &sim, Int_t time, Int_t zoom=2, Int_t Nbins=2, const TString &options="") {
  
#ifdef __CINT__  
  gSystem->Load("libplasma.so");
#endif

  PlasmaGlob::Initialize();

  // Palettes!
  gROOT->Macro("PlasmaPalettes.C");

  // Init Units table
  PUnits::UnitsTable::Get();
  
  // Load PData
  PData *pData = PData::Get(sim.Data());
  pData->LoadFileNames(time);
  if(!pData->IsInit()) return;

  TString opt = options;
 
  // More makeup
  if(opt.Contains("grid")) {
    gStyle->SetPadGridX(1);
    gStyle->SetPadGridY(1);
  }
 
  gStyle->SetTitleFont(42);
  gStyle->SetStatFont(42);
  gStyle->SetTextFont(42);
  gStyle->SetTitleFont(42,"xyz");
  gStyle->SetLabelFont(42,"xyz");

  // Some plasma constants
  Double_t n0 = pData->GetPlasmaDensity();
  Double_t kp = pData->GetPlasmaK();
  Double_t skindepth = 1.;
  if(kp!=0.0) skindepth = 1/kp;
  Double_t E0 = pData->GetPlasmaE0();

  // Some beam properties:
  Float_t Ebeam = pData->GetBeamEnergy() * PUnits::MeV;
  Float_t gamma = Ebeam / PConst::ElectronMassE;
  Float_t vbeam = TMath::Sqrt(1 - 1/(gamma*gamma));
  // cout << Form(" - Bunch gamma      = %8.4f", gamma ) << endl;
  // cout << Form(" - Bunch velocity   = %8.4f c", vbeam ) << endl;
  Float_t nb = pData->GetBeamDensity();
  
  // Time in OU
  Float_t Time = pData->GetRealTime();
  // z start of the plasma in normalized units.
  Float_t zStartPlasma = pData->GetPlasmaStart()*kp;
  // z start of the beam in normalized units.
  Float_t zStartBeam = pData->GetBeamStart()*kp;
  // z start of the neutral in normalized units.
  Float_t zStartNeutral = pData->GetNeutralStart()*kp;
  // z end of the neutral in normalized units.
  Float_t zEndNeutral = pData->GetNeutralEnd()*kp;
  
  if(opt.Contains("center")) {
    Time -= zStartPlasma;
    if(opt.Contains("comov"))      // Centers on the head of the beam.
      Time += zStartBeam;
  }
  Float_t shiftz = pData->Shift(opt);
  //  cout << "Shift = " << shiftz << endl;
  
  // Calculate the "axis range" in number of bins. If Nbins==0 a RMS width is taken.
  Double_t rms0 = pData->GetBeamRmsY() * kp;
  if(pData->IsCyl())  rms0  = pData->GetBeamRmsR() * kp;
  
  Int_t FirstyBin = 0;
  Int_t LastyBin = 0;
  if(Nbins==0) { 
    Nbins =  TMath::Nint(rms0 / pData->GetDX(1));
  }
  
  // Slice width limits.
  if(!pData->IsCyl()) {
    FirstyBin = pData->GetNX(1)/2 + 1 - Nbins;
    LastyBin =  pData->GetNX(1)/2 + Nbins;
  } else {
    FirstyBin = 1; 
    LastyBin  = Nbins;
  }
  // -------------------------------------------------------------------------------

  // Get charge density histos
  Int_t Nspecies = pData->NSpecies();
  TH2F **hDen2D = new TH2F*[Nspecies];
  TH1F **hDen1D = new TH1F*[Nspecies];
  for(Int_t i=0;i<Nspecies;i++) {

    hDen2D[i] = NULL;
    hDen1D[i] = NULL;
    
    if(!pData->GetChargeFileName(i)) 
      continue;

    cout << Form(" Getting charge density of specie: ") << i << endl;

    
    char hName[24];
    sprintf(hName,"hDen2D_%i",i);
    hDen2D[i] = (TH2F*) gROOT->FindObject(hName);
    if(hDen2D[i]) delete hDen2D[i];

    if(!pData->Is3D())
      hDen2D[i] = pData->GetCharge(i,opt);
    else
      hDen2D[i] = pData->GetCharge2DSliceZY(i,-1,Nbins,opt+"avg");

    //  cout << Form(" Charge density of specie '%s' loaded into histogram '%s'",pData->GetSpeciesName(i).c_str(),hName) << endl;
    
    hDen2D[i]->SetName(hName);
    hDen2D[i]->GetXaxis()->CenterTitle();
    hDen2D[i]->GetYaxis()->CenterTitle();
    hDen2D[i]->GetZaxis()->CenterTitle();
    
    if(opt.Contains("comov"))
      hDen2D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
    else
      hDen2D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");
    
    if(pData->IsCyl()) 
      hDen2D[i]->GetYaxis()->SetTitle("r [c/#omega_{p}]");
    else
      hDen2D[i]->GetYaxis()->SetTitle("y [c/#omega_{p}]");

    hDen2D[i]->GetZaxis()->SetTitle("n [n_{0}]");
    
    if(!opt.Contains("1dline")) continue;
    
    sprintf(hName,"hDen1D_%i",i);
    hDen1D[i] = (TH1F*) gROOT->FindObject(hName);
    if(hDen1D[i]) delete hDen1D[i];
    
    if(pData->Is3D()) {
      hDen1D[i] = pData->GetH1SliceZ3D(pData->GetChargeFileName(i)->c_str(),"charge",-1,Nbins,-1,Nbins,opt+"avg");
    } else if(pData->IsCyl()) { // Cylindrical: The first bin with r>0 is actually the number 1 (not the 0).
      hDen1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",1,Nbins,opt+"avg");
    } else { // 2D cartesian
      hDen1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",-1,Nbins,opt+"avg");
    }
    
    //  cout << Form(" Charge density of specie '%s' loaded into histogram '%s'",pData->GetSpeciesName(i).c_str(),hName) << endl;
    
    hDen1D[i]->SetName(hName);
    hDen1D[i]->GetXaxis()->CenterTitle();
    hDen1D[i]->GetYaxis()->CenterTitle();
       
    if(opt.Contains("comov"))
      hDen1D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
    else
      hDen1D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");
    
    hDen1D[i]->GetYaxis()->SetTitle("n [n_{0}]");


  }
  
  // Get electric fields
  const Int_t Nfields = 3;
  TH2F **hE2D = new TH2F*[Nfields];
  TH1F **hE1D = new TH1F*[Nfields];
  for(Int_t i=0;i<Nfields;i++) {
    hE2D[i] = NULL;
    hE1D[i] = NULL;

    if(!pData->GetEfieldFileName(i))
      continue;

    cout << Form(" Getting electric field number ") << i+1 << endl;


    char hName[24];
    sprintf(hName,"hE2D_%i",i);
    hE2D[i] = (TH2F*) gROOT->FindObject(hName);
    if(hE2D[i]) delete hE2D[i];

    if(!pData->Is3D())
      hE2D[i] = pData->GetEField(i,opt);
    else
      hE2D[i] = pData->GetEField2DSliceZY(i,-1,Nbins,opt+"avg");
    
   
    hE2D[i]->SetName(hName);   
    hE2D[i]->GetXaxis()->CenterTitle();
    hE2D[i]->GetYaxis()->CenterTitle();
    hE2D[i]->GetZaxis()->CenterTitle();
    if(opt.Contains("comov"))
      hE2D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
    else
      hE2D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");
    
    if(pData->IsCyl()) 
      hE2D[i]->GetYaxis()->SetTitle("r [c/#omega_{p}]");
    else
      hE2D[i]->GetYaxis()->SetTitle("y [c/#omega_{p}]");
    
    if(i==0)
      hE2D[i]->GetZaxis()->SetTitle("E_{z} [E_{0}]");
    else if(i==1)
      hE2D[i]->GetZaxis()->SetTitle("E_{y} [E_{0}]");
    else if(i==2)
      hE2D[i]->GetZaxis()->SetTitle("E_{x} [E_{0}]");

    if(!opt.Contains("1dline")) continue;
     
    sprintf(hName,"hE1D_%i",i);
    hE1D[i] = (TH1F*) gROOT->FindObject(hName);
    if(hE1D[i]) delete hE1D[i];
    
    // 1D histograms
    char nam[3]; sprintf(nam,"e%i",i+1);
    if(pData->Is3D()) {
      
      if(i==0) 
	hE1D[i] = pData->GetH1SliceZ3D(pData->GetEfieldFileName(i)->c_str(),nam,-1,Nbins,-1,Nbins,opt+"avg");
      else  
	hE1D[i] = pData->GetH1SliceZ3D(pData->GetEfieldFileName(i)->c_str(),nam,-Nbins,Nbins,-Nbins,Nbins,opt+"avg");
      
    } else if(pData->IsCyl()) { // Cylindrical: The first bin with r>0 is actually the number 1 (not the 0).
      
      hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,1,Nbins,opt+"avg");
      
    } else { // 2D cartesian
      
      if(i==0) 
	hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,-1,Nbins,opt+"avg");
      else 
	hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,-Nbins,Nbins,opt+"avg");    
      
    }
    
    hE1D[i]->SetName(hName);
    if(opt.Contains("comov"))
      hE1D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
    else
      hE1D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");
    
    if(i==0)
      hE1D[i]->GetYaxis()->SetTitle("E_{z} [E_{0}]");
    else if(i==1)
      hE1D[i]->GetYaxis()->SetTitle("E_{y} [E_{0}]");
    else if(i==2)
      hE1D[i]->GetYaxis()->SetTitle("E_{x} [E_{0}]");
    
  }
  
  // Now, combine the electric field components into the total |E|
  // and calculate ionization probability for He:
  // Outter Helium electron
  Double_t Eion0 = 24.59 * PUnits::eV;
  Double_t Z     = 1;
  Double_t l     = 0;
  Double_t m     = 0;
  
  TH1F *hIonProb1D = NULL;
  if(opt.Contains("ionprob")) {
    hIonProb1D = (TH1F*) hE1D[0]->Clone("hIonProb1D");
    hIonProb1D->Reset();
    Int_t NbinsX = hE1D[0]->GetNbinsX();
    for(Int_t j=1;j<=NbinsX;j++) {
      Double_t E1 = hE1D[0]->GetBinContent(j);
      Double_t E2 = hE1D[1]->GetBinContent(j);
      Double_t E3 = hE1D[2]->GetBinContent(j);
      Double_t E  = TMath::Sqrt(E1*E1+E2*E2+E3*E3);
      
      E *= E0;
      
      Double_t IonProb = (PFunc::ADK(E,Eion0,Z,l,m)/PUnits::atomictime)*PUnits::femtosecond;
      hIonProb1D->SetBinContent(j,IonProb);
    }
    hIonProb1D->GetYaxis()->SetTitle("W_{ADK} [fs^{-1}]");
  }
  
  // Tunning the Histograms
  // ---------------------
  
  // Chaning to user units: 
  // --------------------------
  
  if(opt.Contains("units") && n0) {
    
    for(Int_t i=0;i<Nspecies;i++) {

      if(!hDen2D[i]) continue;

      Int_t NbinsX = hDen2D[i]->GetNbinsX();
      Float_t xMin = skindepth * hDen2D[i]->GetXaxis()->GetXmin() / PUnits::um;
      Float_t xMax = skindepth * hDen2D[i]->GetXaxis()->GetXmax() / PUnits::um;
      Int_t NbinsY = hDen2D[i]->GetNbinsY();
      Float_t yMin = skindepth * hDen2D[i]->GetYaxis()->GetXmin() / PUnits::um;
      Float_t yMax = skindepth * hDen2D[i]->GetYaxis()->GetXmax() / PUnits::um;
      hDen2D[i]->SetBins(NbinsX,xMin,xMax,NbinsY,yMin,yMax);
      // for(Int_t j=0;j<hDen2D[i]->GetNbinsX();j++) {
      // 	for(Int_t k=0;k<hDen2D[i]->GetNbinsY();k++) {
      // 	  hDen2D[i]->SetBinContent(j,k, hDen2D[i]->GetBinContent(j,k) * n0 / (1e15/PUnits::cm3) );
      // 	}
      // }

      if(pData->IsCyl())
	hDen2D[i]->GetYaxis()->SetTitle("r [#mum]");      
      else
	hDen2D[i]->GetYaxis()->SetTitle("y [#mum]");      

      if(opt.Contains("comov"))
	hDen2D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
      else
	hDen2D[i]->GetXaxis()->SetTitle("z [#mum]");
      
      //      hDen2D[i]->GetZaxis()->SetTitle("n [10^{15}/cm^{3}]");
      
      if(!hDen1D[i]) continue;
      
      hDen1D[i]->SetBins(NbinsX,xMin,xMax);
      
      if(opt.Contains("comov"))
    	hDen1D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
      else
    	hDen1D[i]->GetXaxis()->SetTitle("z [#mum]");
      
    }

    for(Int_t i=0;i<Nfields;i++) {
      Int_t NbinsX = hE2D[i]->GetNbinsX();
      Float_t xMin = skindepth * hE2D[i]->GetXaxis()->GetXmin() / PUnits::um;
      Float_t xMax = skindepth * hE2D[i]->GetXaxis()->GetXmax() / PUnits::um;
      Int_t NbinsY = hE2D[i]->GetNbinsY();
      Float_t yMin = skindepth * hE2D[i]->GetYaxis()->GetXmin() / PUnits::um;
      Float_t yMax = skindepth * hE2D[i]->GetYaxis()->GetXmax() / PUnits::um;
      hE2D[i]->SetBins(NbinsX,xMin,xMax,NbinsY,yMin,yMax);
      if(hE1D[i]) 
	hE1D[i]->SetBins(NbinsX,xMin,xMax);
	

      for(Int_t j=0;j<hE2D[i]->GetNbinsX();j++) {	
	for(Int_t k=0;k<hE2D[i]->GetNbinsY();k++) {
	  hE2D[i]->SetBinContent(j,k, hE2D[i]->GetBinContent(j,k) * ( E0 / (PUnits::GV/PUnits::m) ) );
	}
	
	if(!hE1D[i]) continue;
	hE1D[i]->SetBinContent(j, hE1D[i]->GetBinContent(j) * ( E0 / (PUnits::GV) ) );
	
      }
      
      if(pData->IsCyl())
	hE2D[i]->GetYaxis()->SetTitle("r [#mum]");      
      else
	hE2D[i]->GetYaxis()->SetTitle("y [#mum]");      
 
      if(opt.Contains("comov"))
	hE2D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
      else
	hE2D[i]->GetXaxis()->SetTitle("z [#mum]");
      
      if(i==0)
	hE2D[i]->GetZaxis()->SetTitle("E_{z} [GV/m]");
      else if(i==1)
	hE2D[i]->GetZaxis()->SetTitle("E_{y} [GV/m]");
      else if(i==2)
	hE2D[i]->GetZaxis()->SetTitle("E_{x} [GV/m]");


      if(!hE1D[i]) continue;
      if(opt.Contains("comov"))
	hE1D[i]->GetXaxis()->SetTitle("#zeta [mm]");
      else
	hE1D[i]->GetXaxis()->SetTitle("z [mm]");
      
      if(i==0)
	hE1D[i]->GetYaxis()->SetTitle("E_{z} [GV/m]");
      else if(i==1)
	hE1D[i]->GetYaxis()->SetTitle("E_{y} [GV/m]");
      else if(i==2)
	hE1D[i]->GetYaxis()->SetTitle("E_{x} [GV/m]");
      
      
    }
    if(hIonProb1D) {
      Int_t NbinsX = hIonProb1D->GetNbinsX();
      Float_t xMin = skindepth * hIonProb1D->GetXaxis()->GetXmin() / PUnits::um;
      Float_t xMax = skindepth * hIonProb1D->GetXaxis()->GetXmax() / PUnits::um;
      hIonProb1D->SetBins(NbinsX,xMin,xMax);
    }
  }
  
  // --------------------------------------------------- Vertical Zoom ------------

  Float_t range    = (hDen2D[0]->GetYaxis()->GetXmax() - hDen2D[0]->GetYaxis()->GetXmin())/zoom;
  Float_t midPoint = (hDen2D[0]->GetYaxis()->GetXmax() + hDen2D[0]->GetYaxis()->GetXmin())/2.;
  Double_t ymin = midPoint-range/2;
  Double_t ymax = midPoint+range/2;
  if(pData->IsCyl()) {
    ymin = hDen2D[0]->GetYaxis()->GetXmin();
    ymax = range;
  }
  hDen2D[0]->GetYaxis()->SetRangeUser(ymin,ymax);
  hE2D[0]->GetYaxis()->SetRangeUser(ymin,ymax);
  hE2D[1]->GetYaxis()->SetRangeUser(ymin,ymax);
  
  // ------------- z Zoom --------------------------------- Plasma palette -----------
  // Set the range of the plasma charge density histogram for maximum constrast 
  // using a dynamic palette wich adjust the nominal value to a certain color.
  

  Float_t density = 1;
  // if(opt.Contains("units") && n0)
  //   density = n0 / (1e17/PUnits::cm3);
  Float_t Base  = density;
  Float_t BaseB =  TMath::Nint(100*(nb/n0))/100.0;

  Float_t gMax  = hDen2D[0]->GetMaximum();
  Float_t gMin  = (0.1001) * Base;
  if(BaseB<Base) gMin  = (0.1001) * BaseB;

  if(gMax<Base) gMax = 1.1*Base;

  Float_t *Max = new Float_t[Nspecies];
  Float_t *Min = new Float_t[Nspecies];

  for(Int_t i=0;i<Nspecies;i++) {
    if(!hDen2D[i]) continue;
   
    Max[i] = hDen2D[i]->GetMaximum();
    Min[i] = 0.01*Max[i];
    //   if(i==1) = Min[i] = 1.001E-4;
    if(i==2) Min[i] = 1.001E-3;
    if(Max[i]>gMax) gMax = Max[i];
    
    hDen2D[i]->GetZaxis()->SetRangeUser(Min[i],Max[i]);
  }
  
  hDen2D[0]->GetZaxis()->SetRangeUser(gMin,gMax); 
  
  // if(hDen2D[1]) {
  //   hDen2D[1]->GetZaxis()->SetRangeUser(gMin,Max[1]);
  // } 

  // if(Nspecies>=3) {
  //   if(hDen2D[2]) {
  //     hDen2D[2]->GetZaxis()->SetRangeUser(gMin,Max[2]);
  //   } 
  // }
  
  // Dynamic plasma palette
  const Int_t plasmaDNRGBs = 3;
  const Int_t plasmaDNCont = 128;
  Double_t basePos = 0.5;
  if(gMax!=gMin) {
    if(opt.Contains("logz")) {
      Float_t a = 1.0/(TMath::Log10(gMax)-TMath::Log10(gMin));
      Float_t b = TMath::Log10(gMin);
      basePos = a*(TMath::Log10(Base) - b);
      
    } else {
      basePos = (1.0/(gMax-gMin))*(Base - gMin);
    }
  }

  Double_t plasmaDStops[plasmaDNRGBs] = { 0.00, basePos, 1.00 };
  Double_t plasmaDRed[plasmaDNRGBs]   = { 0.99, 0.90, 0.00 };
  Double_t plasmaDGreen[plasmaDNRGBs] = { 0.99, 0.90, 0.00 };
  Double_t plasmaDBlue[plasmaDNRGBs]  = { 0.99, 0.90, 0.00 };
   
  PPalette * plasmaPalette = (PPalette*) gROOT->FindObject("plasma");
  plasmaPalette->CreateGradientColorTable(plasmaDNRGBs, plasmaDStops, 
					  plasmaDRed, plasmaDGreen, plasmaDBlue, plasmaDNCont);
  
  // Change the range of z axis for the fields to be symmetric.
  Float_t Emax = hE2D[0]->GetMaximum();
  Float_t Emin = hE2D[0]->GetMinimum();
  if(Emax > TMath::Abs(Emin))
    Emin = -Emax;
  else
    Emax = -Emin;
  hE2D[0]->GetZaxis()->SetRangeUser(Emin,Emax); 

  Emax = hE2D[1]->GetMaximum();
  Emin = hE2D[1]->GetMinimum();
  if(Emax > TMath::Abs(Emin))
    Emin = -Emax;
  else
    Emax = -Emin;
  hE2D[1]->GetZaxis()->SetRangeUser(Emin,Emax); 
    

  
  // "Axis range" in Osiris units:
  Double_t ylow  = hDen2D[1]->GetYaxis()->GetBinLowEdge(FirstyBin);
  Double_t yup = hDen2D[1]->GetYaxis()->GetBinUpEdge(LastyBin);
  Double_t xmin = hDen2D[1]->GetXaxis()->GetXmin();
  Double_t xmax = hDen2D[1]->GetXaxis()->GetXmax();

  TLine *lineYzero = new TLine(xmin,0.0,xmax,0.0);
  lineYzero->SetLineColor(kGray+2);
  lineYzero->SetLineStyle(2);

  TLine *lineYup = new TLine(xmin,yup,xmax,yup);
  lineYup->SetLineColor(kGray+1);
  lineYup->SetLineStyle(2);
 
  TLine *lineYdown = new TLine(xmin,ylow,xmax,ylow);
  lineYdown->SetLineColor(kGray+1);
  lineYdown->SetLineStyle(2);

  zStartPlasma -= shiftz; 
  zStartNeutral -= shiftz; 
  zEndNeutral -= shiftz; 
  
  if(opt.Contains("units")) {
    zStartPlasma *= skindepth / PUnits::um;
    zStartNeutral *= skindepth / PUnits::um;
    zEndNeutral *= skindepth / PUnits::um;
  }

  //  cout << "Start plasma = " << zStartPlasma << endl;
  TLine *lineStartPlasma = new TLine(zStartPlasma,ymin,zStartPlasma,ymax);
  lineStartPlasma->SetLineColor(kRed);
  lineStartPlasma->SetLineStyle(1);
  lineStartPlasma->SetLineWidth(2);

  //  cout << "Start plasma = " << zStartNeutral << endl;
  TLine *lineStartNeutral = new TLine(zStartNeutral,ymin,zStartNeutral,ymax);
  lineStartNeutral->SetLineColor(kGray+1);
  lineStartNeutral->SetLineStyle(1);
  lineStartNeutral->SetLineWidth(2);

  //  cout << "End plasma = " << zEndNeutral << endl;
  TLine *lineEndNeutral = new TLine(zEndNeutral,ymin,zEndNeutral,ymax);
  lineEndNeutral->SetLineColor(kGray+1);
  lineEndNeutral->SetLineStyle(2);
  lineEndNeutral->SetLineWidth(2);
  
  // Plotting
  // -----------------------------------------------

  // Canvas setup
  TCanvas *C;
  if(opt.Contains("hres") && !opt.Contains("pdf")) // high resolution for plain grahics output.
    C = new TCanvas("C","2D Charge density, Accelerating and focusing fields",1500,2000);
  else
    C = new TCanvas("C","2D Charge density, Accelerating and focusing fields",750,1000);
 
  // Palettes setup
  TExec *exPlasma = new TExec("exPlasma","plasmaPalette->cd();");
  TExec *exHot    = new TExec("exHot","hotPalette->cd();");
  TExec *exElec   = new TExec("exElec","electronPalette->cd();");
  TExec *exField  = new TExec("exField","rbow2Palette->cd();");
  
  // Text objects
  TPaveText *textTime = new TPaveText(0.7,0.83,0.85,0.90,"NDC");
  PlasmaGlob::SetPaveTextStyle(textTime,32); 
  char ctext[128];
  if(opt.Contains("units") && n0) 
    sprintf(ctext,"z = %5.1f #mum", Time * skindepth / PUnits::um);
  else
    sprintf(ctext,"t = %5.1f #omega_{p}^{-1}",Time);
  textTime->AddText(ctext);
  
  TPaveText *textDen = new TPaveText(0.13,0.83,0.38,0.90,"NDC");
  PlasmaGlob::SetPaveTextStyle(textDen,12); 
  textDen->SetTextColor(kOrange+10);
  if(opt.Contains("units") && n0)
    sprintf(ctext,"n_{0} = %5.2f x 10^{15} / cm^{3}", 1e-15 * n0 * PUnits::cm3);
  else if(pData->GetBeamDensity() && n0)
    sprintf(ctext,"n_{b}/n_{0} = %5.2f", pData->GetBeamDensity()/n0);
  textDen->AddText(ctext);
  
  TPaveText *textWav = new TPaveText(0.13,0.75,0.38,0.82,"NDC");
  PlasmaGlob::SetPaveTextStyle(textWav,12); 
  textWav->SetTextColor(kGray+2);
  sprintf(ctext,"#lambda_{p} = %5.3f mm", skindepth * TMath::TwoPi() / PUnits::um);
  textWav->AddText(ctext);
  
  
  // Actual Plotting!
  // ------------------------------------------------------------

  // Output file
  TString fOutName = Form("./%s/Plots/ChargeFieldFocus2D/ChargeFieldFocus2D",pData->GetPath().c_str());
  fOutName += Form("-%s_%i",pData->GetName(),time);


  // Setup Pad layout:
  Double_t lMargin = 0.10;
  Double_t rMargin = 0.12;
  Double_t bMargin = 0.10;
  Double_t tMargin = 0.02;
  Double_t vSpacing = 0.01; 
  Double_t hStep = (1.-lMargin-rMargin);
  Double_t vStep = (1.-bMargin-tMargin)/3.;
 
  TPad *pad[3];

  // top plots
  pad[0] = new TPad("padt", "padt",0.00, bMargin + 2.*vStep + vSpacing,
		    lMargin+hStep+rMargin, 1.00);
  pad[0]->SetLeftMargin(1./(lMargin+hStep)*lMargin);
  pad[0]->SetRightMargin(1./(rMargin+hStep)*rMargin);  
  pad[0]->SetBottomMargin(0.0);                                   
  pad[0]->SetTopMargin(1./(tMargin+vStep)*tMargin);
  pad[0]->Draw();

  // middle plots
  pad[1] = new TPad("padm", "padm",0.00, bMargin + vStep + vSpacing,
		    lMargin + hStep + rMargin, bMargin + 2.*vStep );
  pad[1]->SetLeftMargin(1./(lMargin+hStep)*lMargin);
  pad[1]->SetRightMargin((1./(rMargin+hStep)*rMargin));
  pad[1]->SetBottomMargin(0.0);                                   
  pad[1]->SetTopMargin(0.);
  pad[1]->Draw();          

  // bottom plots
  pad[2] = new TPad("padb", "padb",0.00, 0.,lMargin+hStep+rMargin,bMargin+vStep);
  pad[2]->SetLeftMargin(1./(lMargin+hStep)*lMargin);
  pad[2]->SetRightMargin((1./(rMargin+hStep)*rMargin));
  pad[2]->SetBottomMargin(1./(bMargin+vStep)*bMargin);
  pad[2]->SetTopMargin(0.);
  pad[2]->Draw();       
  
  // Draw!
  pad[0]->cd(); // <---------------------------------------------- Top Plot ---------
  if(opt.Contains("logz")) {
    pad[0]->SetLogz(1);
  } else {
    pad[0]->SetLogz(0);
  }
  pad[0]->SetFrameLineWidth(3);  

  TH2F *hFrame = (TH2F*) gROOT->FindObject("hFrame1");
  if(hFrame) delete hFrame;
  hFrame = (TH2F*) hDen2D[0]->Clone("hFrame1");
  hFrame->Reset();

  hFrame->GetXaxis()->SetLabelOffset(999);

  hFrame->GetYaxis()->SetTitleSize(0.075);
  hFrame->GetYaxis()->SetTitleOffset(0.65);
  hFrame->GetYaxis()->SetLabelSize(0.065);
  hFrame->GetYaxis()->SetLabelOffset(0.02);
  hFrame->GetYaxis()->SetTickLength(0.02);
 
  hFrame->GetZaxis()->SetTitleSize(0.06);                        
  hFrame->GetZaxis()->SetTitleOffset(0.45);
  hFrame->GetZaxis()->SetLabelSize(0.06);  
  hFrame->GetZaxis()->SetTickLength(0.02);
  //  hFrame->GetZaxis()->SetNdivisions(505);

  hFrame->Draw("col");            

  if(Nspecies>=3) {
    if(hDen2D[2]) {
      exHot->Draw();
      hDen2D[2]->Draw("colz same");
    }
  }

  exPlasma->Draw();
  hDen2D[0]->Draw("colz same");
  
  if(hDen2D[1]) {
    exElec->Draw();
    hDen2D[1]->Draw("colz same");
  }
  
  if(opt.Contains("1dline")) {
    lineYzero->Draw();
    lineYdown->Draw();
    lineYup->Draw();
  }

  if(zStartPlasma>xmin && zStartPlasma<xmax)
    lineStartPlasma->Draw();
  if(zStartNeutral>xmin && zStartNeutral<xmax)
    lineStartNeutral->Draw();
  if(zEndNeutral>xmin && zEndNeutral<xmax)
    lineEndNeutral->Draw();
  
  pad[0]->Update();
  TPaletteAxis *palette = NULL;
  for(Int_t i=0;i<Nspecies;i++) {
    
    if(!hDen2D[i]) continue;
    palette = (TPaletteAxis*) hDen2D[i]->GetListOfFunctions()->FindObject("palette");
    if(!palette) continue;

    Float_t y1 = gPad->GetBottomMargin();
    Float_t y2 = 1 - gPad->GetTopMargin();
    Float_t x1 = gPad->GetLeftMargin();
    Float_t x2 = 1 - gPad->GetRightMargin();
    palette->SetY2NDC( (i+1)*(y2-y1)/Nspecies + y1);
    palette->SetY1NDC( i*(y2-y1)/Nspecies + y1);
    palette->SetX1NDC(x2 + 0.005);
    palette->SetX2NDC(x2 + 0.03);
    palette->SetTitleOffset(0.65);
    palette->SetTitleSize(0.07);
    palette->SetLabelSize(0.065);
    palette->SetBorderSize(2);
    palette->SetLineColor(1);
  
  }


  // 1D charge density plots:
  Float_t yaxismin  =  pad[0]->GetUymin();
  Float_t yaxismax  =  pad[0]->GetUymin() + 0.33*(pad[0]->GetUymax() - pad[0]->GetUymin()) - 0.00;
  
  //  Float_t denmin = (0.1001) * density;
  Float_t denmin = gMin;
  Float_t denmax = gMax;
  if(opt.Contains("logz")) {
    denmin = TMath::Log10(denmin);
    denmax = TMath::Log10(denmax);
  }
  
  for(Int_t i=0;i<Nspecies;i++) {
    if(!hDen1D[i]) continue;
    
    Float_t slope = (yaxismax - yaxismin)/(denmax - denmin);
    
    for(Int_t j=0;j<hDen1D[i]->GetNbinsX();j++) {
      Float_t content = hDen1D[i]->GetBinContent(j+1);
      if(opt.Contains("logz")) content = TMath::Log10(content); 
      
      if(content<denmin) 
	hDen1D[i]->SetBinContent(j+1,yaxismin);
      else 
	hDen1D[i]->SetBinContent(j+1,(content - denmin) * slope + yaxismin);
    }    

    hDen1D[i]->SetLineWidth(2);
    if(i==1) {
      hDen1D[i]->SetLineColor(PlasmaGlob::elecLine);
      hDen1D[i]->Draw("same C");
    } else if (i==2) {
      hDen1D[i]->SetLineColor(kOrange+8);
      hDen1D[i]->Draw("same C");
    }
  }
    
  textTime->Draw();
  //  textDen->Draw();
  //  textWav->Draw();
  
  pad[0]->RedrawAxis(); 

 
  pad[1]->cd(); // <--------------------------------------------- Mid Plot
  pad[1]->SetFrameLineWidth(3);  
    
  TH2F *hFrame2 = (TH2F*) gROOT->FindObject("hFrame2");
  if(hFrame2) delete hFrame2;
  hFrame2 = (TH2F*) hE2D[0]->Clone("hFrame2");
  hFrame2->Reset();

  Float_t yFactor = pad[0]->GetAbsHNDC()/pad[1]->GetAbsHNDC(); 

  hFrame2->GetXaxis()->SetLabelOffset(999);

  hFrame2->GetYaxis()->SetTitleSize(0.075*yFactor);
  hFrame2->GetYaxis()->SetTitleOffset(0.65/yFactor);
  hFrame2->GetYaxis()->SetLabelSize(0.0650*yFactor);
  hFrame2->GetYaxis()->SetLabelOffset(0.02/yFactor);
  hFrame2->GetYaxis()->SetTickLength(0.02/yFactor);
  
  hE2D[0]->GetZaxis()->SetTitleSize(0.06*yFactor);                        
  hE2D[0]->GetZaxis()->SetTitleOffset(0.45/yFactor);
  hE2D[0]->GetZaxis()->SetLabelSize(0.06*yFactor);  
  hE2D[0]->GetZaxis()->SetTickLength(0.02/yFactor);
  // hFrame2->GetZaxis()->SetNdivisions(505);
  
  hFrame2->Draw("col");

  exField->Draw();
  hE2D[0]->Draw("colz same");

  if(opt.Contains("1dline")) {
    lineYzero->Draw();
    lineYdown->Draw();
    lineYup->Draw();
  }
  if(zStartPlasma>xmin && zStartPlasma<xmax)
    lineStartPlasma->Draw();
  if(zStartNeutral>xmin && zStartNeutral<xmax)
    lineStartNeutral->Draw();
  if(zEndNeutral>xmin && zEndNeutral<xmax)
    lineEndNeutral->Draw();
  
  pad[1]->Update();
  
  palette = (TPaletteAxis*) hE2D[0]->GetListOfFunctions()->FindObject("palette");
  
  Float_t y1 = pad[1]->GetBottomMargin();
  Float_t y2 = 1 - pad[1]->GetTopMargin();
  Float_t x2 = 1 - pad[1]->GetRightMargin();
  palette->SetY2NDC(y2 - 0.01);
  palette->SetY1NDC(y1 + 0.01);
  palette->SetX1NDC(x2 + 0.005);
  palette->SetX2NDC(x2 + 0.03);
  palette->SetTitleSize(0.07*yFactor);
  palette->SetTitleOffset(0.65/yFactor);
  palette->SetLabelSize(0.065*yFactor);
  palette->SetBorderSize(2);
  palette->SetLineColor(1);
  
  
  // 1D electric field plots:
  if(hE1D[0]) {
    // yaxismin  =  pad[1]->GetUymin() + 0.16666*(pad[1]->GetUymax() - pad[1]->GetUymin());
    // yaxismax  =  pad[1]->GetUymax() - 0.16666*(pad[1]->GetUymax() - pad[1]->GetUymin());
    Float_t yaxismin  =  pad[1]->GetUymin();
    Float_t yaxismax  =  pad[1]->GetUymax();
    
    Float_t emin = hE1D[0]->GetMinimum();
    Float_t emax = hE1D[0]->GetMaximum();
    
    //Float_t slope = (yaxismax - yaxismin)/(emax - emin);
    Float_t slope = yaxismin/emin;
    
    for(Int_t j=0;j<hE1D[0]->GetNbinsX();j++) {
      Float_t content = hE1D[0]->GetBinContent(j+1);
      hE1D[0]->SetBinContent(j+1,(content - emin) * slope + yaxismin);
  }    
    
    hE1D[0]->SetLineWidth(2);
    hE1D[0]->SetLineColor(PlasmaGlob::elecLine);
    // hE1D[0]->SetLineColor(kGray+2);
    hE1D[0]->Draw("same C");
  }

  // Plot ionization probability:
  if(hIonProb1D) {
    Float_t ionmin = 0;
    Float_t ionmax = hIonProb1D->GetMaximum();
    
    Float_t slope = yaxismax/ionmax;
    
    for(Int_t j=0;j<hIonProb1D->GetNbinsX();j++) {
      Float_t content = hIonProb1D->GetBinContent(j+1);
      hIonProb1D->SetBinContent(j+1,content * slope);
    }    
    
    hIonProb1D->SetLineWidth(2);
    hIonProb1D->SetLineColor(kGray+2);
    hIonProb1D->Draw("same C");
  }
  

  pad[1]->RedrawAxis(); 

  pad[2]->cd();  // <--------------------------------------------- Bottom Plot
  pad[2]->SetFrameLineWidth(3);  
  
  TH2F *hFrame3 = (TH2F*) gROOT->FindObject("hFrame3");
  if(hFrame3) delete hFrame3;
  hFrame3 = (TH2F*) hE2D[1]->Clone("hFrame3");
  hFrame3->Reset();

  yFactor = pad[0]->GetAbsHNDC()/pad[2]->GetAbsHNDC();
  
  hFrame3->GetXaxis()->SetTitleSize(0.075);
  hFrame3->GetXaxis()->SetLabelSize(0.070);

  hFrame3->GetYaxis()->SetTitleSize(0.075*yFactor);
  hFrame3->GetYaxis()->SetTitleOffset(0.65/yFactor);
  hFrame3->GetYaxis()->SetLabelSize(0.0650*yFactor);
  hFrame3->GetYaxis()->SetLabelOffset(0.02/yFactor);
  hFrame3->GetYaxis()->SetTickLength(0.02/yFactor);

  hE2D[1]->GetZaxis()->SetTitleSize(0.06*yFactor);                        
  hE2D[1]->GetZaxis()->SetTitleOffset(0.45/yFactor);
  hE2D[1]->GetZaxis()->SetLabelSize(0.06*yFactor);  
  hE2D[1]->GetZaxis()->SetTickLength(0.02/yFactor);
  // hE2D[1]->GetZaxis()->SetNdivisions(505);
  
  hFrame3->Draw("col");

  exField->Draw();
  hE2D[1]->Draw("colz same");

  if(opt.Contains("1dline")) {
    lineYzero->Draw();
    lineYup->Draw();
  }

  if(zStartPlasma>xmin && zStartPlasma<xmax)
    lineStartPlasma->Draw();
  if(zStartNeutral>xmin && zStartNeutral<xmax)
    lineStartNeutral->Draw();
  if(zEndNeutral>xmin && zEndNeutral<xmax)
    lineEndNeutral->Draw();

  pad[2]->Update();
  palette = (TPaletteAxis*)hE2D[1]->GetListOfFunctions()->FindObject("palette");
  
  y1 = pad[2]->GetBottomMargin();
  y2 = 1 - pad[2]->GetTopMargin();
  x2 = 1 - pad[2]->GetRightMargin();
  palette->SetY2NDC(y2 - 0.01);
  palette->SetY1NDC(y1 + 0.01);
  palette->SetX1NDC(x2 + 0.005);
  palette->SetX2NDC(x2 + 0.03);
  palette->SetTitleSize(0.07*yFactor);
  palette->SetTitleOffset(0.65/yFactor);
  palette->SetLabelSize(0.065*yFactor);
  palette->SetBorderSize(2);
  palette->SetLineColor(1);

  // 1D electric field plots:
  if(hE1D[1]) {
    // yaxismin  =  pad[2]->GetUymin() + 0.16666*(pad[2]->GetUymax() - pad[2]->GetUymin());
    // yaxismax  =  pad[2]->GetUymax() - 0.16666*(pad[2]->GetUymax() - pad[2]->GetUymin());
    Float_t yaxismin  =  pad[2]->GetUymin();
    Float_t yaxismax  =  pad[2]->GetUymax();
    
    Float_t emin = hE1D[1]->GetMinimum();
    Float_t emax = hE1D[1]->GetMaximum();
    
    Float_t slope = yaxismin/emin;
    
    for(Int_t j=0;j<hE1D[1]->GetNbinsX();j++) {
      Float_t content = hE1D[1]->GetBinContent(j+1);
      hE1D[1]->SetBinContent(j+1,(content - emin) * slope + yaxismin);
    }    
    
    hE1D[1]->SetLineWidth(2);
    //hE1D[1]->SetLineStyle(2);
    // hE1D[1]->SetLineColor(kGray+2);
    hE1D[1]->SetLineColor(PlasmaGlob::elecLine);
    hE1D[1]->Draw("same C");
  }

  // Plot ionization probability:
  if(hIonProb1D) {
    Float_t ionmin = 0;
    Float_t ionmax = hIonProb1D->GetMaximum();
    
    Float_t slope = yaxismax/ionmax;
    
    for(Int_t j=0;j<hIonProb1D->GetNbinsX();j++) {
      Float_t content = hIonProb1D->GetBinContent(j+1);
      hIonProb1D->SetBinContent(j+1,content * slope);
    }    
    
    hIonProb1D->SetLineWidth(2);
    hIonProb1D->SetLineColor(kGray+2);
    hIonProb1D->Draw("same C");
  }

   
  pad[2]->RedrawAxis(); 

  C->cd();

  // Print to a file
  PlasmaGlob::imgconv(C,fOutName,opt);
  // ---------------------------------------------------------

}
Exemple #3
0
void PlotRakeInjection( const TString &sim, Int_t zoom=2, Int_t Nbins=2, const TString &options="") {
  
#ifdef __CINT__  
  gSystem->Load("libplasma.so");
#endif

  PlasmaGlob::Initialize();

  // Palettes!
  gROOT->Macro("PlasmaPalettes.C");

  // Init Units table
  PUnits::UnitsTable::Get();
  
  TString opt = options;
  
  // More makeup
  gStyle->SetPadGridY(0);
  if(opt.Contains("grid")) {
    gStyle->SetPadGridX(1);
    gStyle->SetPadGridY(1);
  }
  
  Bool_t CYL = kFALSE;
  if(sim.Contains("cyl")) { CYL = kTRUE; opt += "cyl"; } 
  
  Bool_t ThreeD = kFALSE;
  if(sim.Contains("3D")) ThreeD = kTRUE; 
  
  // Load PData
  PData *pData = PData::Get(sim.Data());

  // Create the canvas and the pads before the Frame loop
  // Resolution:
  Int_t sizex = 600;
  Int_t sizey = 640;
  if(opt.Contains("hres")) {
    Int_t sizex = 1200;
    Int_t sizey = 1280;    
  }
  
  TCanvas *C1 = new TCanvas("C1","Evolution of Injection",sizex,sizey);
  C1->cd();
  
  // Palettes
  TExec *exPlasma = new TExec("exPlasma","plasmaPalette->cd();");
  TExec *exElec   = new TExec("exElec","electronPalette->cd();");
  TExec *exHot    = new TExec("exHot","hotPalette->cd();");
  
  const Int_t NFrames = 4;
  // Int_t timestep[NFrames] = {18,22,26,34};
  // TString sLabels[NFrames] = {"(a)","(b)","(c)","(d)"};
  //  Int_t timestep[NFrames] = {34,26,22,18};
  Int_t timestep[NFrames] = {500,60,36,31};
  TString sLabels[NFrames] = {"(d)","(c)","(b)","(a)"};
  Float_t Time[NFrames] = {0.0};
  TH2F ***hDen2D = new TH2F**[NFrames];
  Float_t **zDenMax = new Float_t*[NFrames];
  Float_t *zDenMaxTotal = NULL;

  // Text objects
  TPaveText **textTime  = new TPaveText*[NFrames];
  TPaveText **textLabel = new TPaveText*[NFrames];

  TPad **pad = new TPad*[NFrames];
  TH2F *hFrame[NFrames];
 
  //  C1->Divide(1,NFrames);

  // Setup Pad layout:
  Double_t lMargin = 0.15;
  Double_t rMargin = 0.18;
  Double_t bMargin = 0.10;
  Double_t tMargin = 0.04;
  Double_t vSpacing = 0.00; 
  Double_t hStep = (1.-lMargin-rMargin);
  Double_t vStep = (1.- bMargin - tMargin - (NFrames-1) * vSpacing) / NFrames;
  
  Float_t vposd = 0.0;
  Float_t vposu = 0.0;
  Float_t vmard = 0.0;
  Float_t vmaru = 0.0;
  Float_t vfactor = 0.0;
  Float_t hposl = 0.0;
  Float_t hposr = 1.0;
  Float_t hmarl = lMargin;
  Float_t hmarr = rMargin;
  Float_t hfactor = 1.0;

  Double_t n0,kp,skindepth,E0;
  Int_t Nspecies;
  
  for(Int_t k=0;k<NFrames;k++) {
    
    pData->LoadFileNames(timestep[k]);
    if(!pData->IsInit()) continue;
    
    // Some plasma constants
    n0 = pData->GetPlasmaDensity();
    kp = pData->GetPlasmaK();
    skindepth = 1.;
    if(kp!=0.0) skindepth = 1/kp;
    E0 = pData->GetPlasmaE0();
    
    // Time in OU
    Time[k] = pData->GetRealTime();
    // z start of the plasma in normalized units.
    Float_t zStartPlasma = pData->GetPlasmaStart()*kp;
    // z start of the beam in normalized units.
    Float_t zStartBeam = pData->GetBeamStart()*kp;
    
    if(opt.Contains("center")) {
      Time[k] -= zStartPlasma;
      if(opt.Contains("comov"))      // Centers on the head of the beam.
	Time[k] += zStartBeam;
    }
    
    // Get charge density histos
    Nspecies = pData->NSpecies();
    hDen2D[k] = new TH2F*[Nspecies];
    zDenMax[k] = new Float_t[Nspecies];
    
    if(k==0) zDenMaxTotal = new Float_t[Nspecies];
   
    for(Int_t i=0;i<Nspecies;i++) {
      hDen2D[k][i] = NULL;
      if(k==0) zDenMaxTotal[i] = -999.;
      if(!pData->GetChargeFileName(i)) 
	continue;
      
      char hName[24];
      sprintf(hName,"hDen_%i_%i",k,i);
      hDen2D[k][i] = (TH2F*) gROOT->FindObject(hName);
      if(hDen2D[k][i]) delete hDen2D[k][i];
      
      if(!ThreeD)
	hDen2D[k][i] = pData->GetCharge(i,opt);
      else
	hDen2D[k][i] = pData->GetCharge2DSliceZY(i,-1,Nbins,opt+"avg");
      
      hDen2D[k][i]->SetName(hName);
      hDen2D[k][i]->GetXaxis()->CenterTitle();
      hDen2D[k][i]->GetYaxis()->CenterTitle();
      hDen2D[k][i]->GetZaxis()->CenterTitle();

      zDenMax[k][i] = hDen2D[k][i]->GetMaximum();
      if(zDenMax[k][i]>zDenMaxTotal[i])
	zDenMaxTotal[i] = zDenMax[k][i]; 
            
      // Changing to user units: 
      // --------------------------
      if(opt.Contains("units") && n0) {
	Int_t NbinsX = hDen2D[k][i]->GetNbinsX();
	Float_t xMin = skindepth * hDen2D[k][i]->GetXaxis()->GetXmin() / PUnits::um;
	Float_t xMax = skindepth * hDen2D[k][i]->GetXaxis()->GetXmax() / PUnits::um;
	Int_t NbinsY = hDen2D[k][i]->GetNbinsY();
	Float_t yMin = skindepth * hDen2D[k][i]->GetYaxis()->GetXmin() / PUnits::um;
	Float_t yMax = skindepth * hDen2D[k][i]->GetYaxis()->GetXmax() / PUnits::um;
	hDen2D[k][i]->SetBins(NbinsX,xMin,xMax,NbinsY,yMin,yMax);
	
	hDen2D[k][i]->GetXaxis()->SetTitle("#zeta [#mum]");
      } else {
	hDen2D[k][i]->GetXaxis()->SetTitle("k_{p} #zeta");
      }

      // Zoom
      Float_t range    = (hDen2D[k][i]->GetYaxis()->GetXmax() - hDen2D[k][i]->GetYaxis()->GetXmin())/zoom;
      Float_t midPoint = (hDen2D[k][i]->GetYaxis()->GetXmax() + hDen2D[k][i]->GetYaxis()->GetXmin())/2.;
      
      Float_t yMin,yMax;
      if(!CYL) {
	yMin = midPoint-range/2;
	yMax = midPoint+range/2;
      } else {
	yMin = 0.;
	yMax = range;
      }
      hDen2D[k][i]->GetYaxis()->SetRangeUser(yMin,yMax);
      

    }
  }

  Float_t density = 1;
  Float_t Base = density;

  Float_t *Max = new Float_t[Nspecies];
  Float_t *Min = new Float_t[Nspecies];
  
  for(Int_t k=0;k<NFrames;k++) {
    
    // Set Z ranges:
    for(Int_t i=0;i<Nspecies;i++) {
  
      Max[i] = zDenMaxTotal[i];
      Min[i] =  1.01E-1 * Base;
      if(i==2) {
	Min[i] = 1.01E-3 * Base;
	Max[i] = zDenMax[k][i];
      }
      
      hDen2D[k][i]->GetZaxis()->SetRangeUser(Min[i],Max[i]);
      
      
      if(i==0 && k==0) {
	// Dynamic plasma palette
	const Int_t plasmaDNRGBs = 3;
	const Int_t plasmaDNCont = 128;
	Double_t basePos = 0.5;
	if(Max[i]!=Min[i]) {
	  if(opt.Contains("logz")) {
	    Float_t a = 1.0/(TMath::Log10(Max[i])-TMath::Log10(Min[i]));
	    Float_t b = TMath::Log10(Min[i]);
	    basePos = a*(TMath::Log10(Base) - b);
	    //  cout << Form("Min = %f Base = %f Max = %f",a*(TMath::Log10(Min) - b),basePos,a*(TMath::Log10(Max) - b)) << endl;     
	  } else {
	    basePos = (1.0/(Max[i]-Min[i]))*(Base - Min[i]);
	  }
	}
	
	Double_t plasmaDStops[plasmaDNRGBs] = { 0.00, basePos, 1.00 };
	Double_t plasmaDRed[plasmaDNRGBs]   = { 0.99, 0.90, 0.00 };
	Double_t plasmaDGreen[plasmaDNRGBs] = { 0.99, 0.90, 0.00 };
	Double_t plasmaDBlue[plasmaDNRGBs]  = { 0.99, 0.90, 0.00 };
	
	PPalette * plasmaPalette = (PPalette*) gROOT->FindObject("plasma");
	plasmaPalette->CreateGradientColorTable(plasmaDNRGBs, plasmaDStops, 
						plasmaDRed, plasmaDGreen, plasmaDBlue, plasmaDNCont);
      }
    }
    
    // PLOTTING!
    if(k==0) {
      vposd = 0.0;
      vposu = bMargin + vStep;
      vfactor = vposu-vposd;  
      vmard = bMargin / vfactor;
      vmaru = 0.0;
    } else if(k == NFrames-1) {
      vposd = vposu + vSpacing;
      vposu = vposd + vStep + tMargin;
      vfactor = vposu-vposd;   
      vmard = 0.0;
      vmaru = tMargin / (vposu-vposd);
    } else {
      vposd = vposu + vSpacing;
      vposu = vposd + vStep; 
      vfactor = vposu-vposd;
      vmard = 0.0;
      vmaru = 0.0;
    } 
    hfactor = hposl-hposr;
  
    C1->cd();
    
    char name[16];
    sprintf(name,"pad_%i",k);
    pad[k] = new TPad(name,"",hposl,vposd,hposr,vposu);
    // // cout << Form("%f %f %f %f",hposl,vposd,hposr,vposu) << endl;
    // // cout << Form("%f %f %f %f",hmarl,vmard,hmarr,vmaru) << endl;
    pad[k]->SetLeftMargin(hmarl);
    pad[k]->SetRightMargin(hmarr);  
    pad[k]->SetBottomMargin(vmard);
    pad[k]->SetTopMargin(vmaru);
  
    pad[k]->SetFrameLineWidth(3);  
    if(opt.Contains("logz")) {
      pad[k]->SetLogz(1);
    } else {
      pad[k]->SetLogz(0);
    }
    pad[k]->Draw();
    pad[k]->cd();
    
    sprintf(name,"hFrame_%i",k);  
    hFrame[k] = (TH2F*) gROOT->FindObject(name);
    if(hFrame[k]) delete hFrame[k];
    hFrame[k] = (TH2F*) hDen2D[k][0]->Clone(name);
    hFrame[k]->Reset();
  
    hFrame[k]->GetXaxis()->CenterTitle();
    hFrame[k]->GetYaxis()->CenterTitle();
    hFrame[k]->GetZaxis()->CenterTitle();

    hFrame[k]->SetLabelFont(42,"xyz");
    hFrame[k]->SetTitleFont(42,"xyz");
  
    hFrame[k]->SetNdivisions(505,"xyz");
  
    hFrame[k]->SetTickLength(0.04,"xyz");
    hFrame[k]->SetTickLength(0.04*vfactor,"y");
  
    hFrame[k]->GetYaxis()->SetLabelSize(0.03/vfactor);
    hFrame[k]->GetYaxis()->SetLabelOffset(0.02);
  
    hFrame[k]->GetYaxis()->SetTitleSize(0.03/vfactor);
    hFrame[k]->GetYaxis()->SetTitleOffset(999.0*vfactor);
  
    if(k==0) {  
      hFrame[k]->GetXaxis()->SetLabelSize(0.10);
      hFrame[k]->GetXaxis()->SetLabelOffset(0.02);
      hFrame[k]->GetXaxis()->SetTitleSize(0.14);
      hFrame[k]->GetXaxis()->SetTitleOffset(1.0);
    } else {
      hFrame[k]->GetXaxis()->SetLabelSize(0.0);
      hFrame[k]->GetXaxis()->SetTitleSize(0.0);
    }
  
    hFrame[k]->Draw("axis");    
  


    exPlasma->Draw();

    // Sum of histograms!
    hDen2D[k][0]->Add(hDen2D[k][1]);
        
    //    hDen2D[k][0]->GetZaxis()->SetRangeUser(Min[1],Max[1]);
 
    hDen2D[k][0]->Draw("colz same");
    
    pad[k]->Update();
    TPaletteAxis *palette = (TPaletteAxis*)hDen2D[k][0]->GetListOfFunctions()->FindObject("palette");
    
    Float_t y1 = pad[k]->GetBottomMargin();
    Float_t y2 = 1 - pad[k]->GetTopMargin();
    Float_t x1 = pad[k]->GetLeftMargin();
    Float_t x2 = 1 - pad[k]->GetRightMargin();
    palette->SetY2NDC(y2 - 1*(y2-y1)/2.0 - 0.00);
    palette->SetY1NDC(y1 + 0*(y2-y1)/2.0 + 0.00);
    palette->SetX1NDC(x2 + 0.005);
    palette->SetX2NDC(x2 + 0.03);

    palette->SetLabelFont(42);

    palette->SetLabelSize(0.03/vfactor);
    palette->SetLabelOffset(-0.004);
    palette->SetTitleSize(0.03/vfactor);
    palette->SetTitleOffset(9999.0*vfactor);
  
    palette->SetBorderSize(2);
    palette->SetLineColor(1);
  
    exHot->Draw();
    hDen2D[k][2]->Draw("colz same");

    pad[k]->Update();
    palette = (TPaletteAxis*)hDen2D[k][2]->GetListOfFunctions()->FindObject("palette");
    
    palette->SetY2NDC(y2 - 0*(y2-y1)/2.0 - 0.00);
    palette->SetY1NDC(y1 + 1*(y2-y1)/2.0 + 0.00);
    palette->SetX1NDC(x2 + 0.005);
    palette->SetX2NDC(x2 + 0.03);

    palette->SetLabelFont(42);

    palette->SetLabelSize(0.03/vfactor);
    palette->SetLabelOffset(-0.004);
    palette->SetTitleSize(0.03/vfactor);
    palette->SetTitleOffset(9999.0*vfactor);

    palette->SetBorderSize(2);
    palette->SetLineColor(1);

    textTime[k] = new TPaveText(x2-0.17,y2-(0.05/vfactor),x2-0.02,y2-0.04,"NDC"); 
    PlasmaGlob::SetPaveTextStyle(textTime[k],32); 
    char ctext[128];
    if(opt.Contains("units")) 
      sprintf(ctext,"z = %5.0f #mum", skindepth * Time[k] / PUnits::um);
    else
      sprintf(ctext,"z = %5.1f #omega_{p}^{-1}",Time[k]);
    textTime[k]->AddText(ctext);
    textTime[k]->Draw();

    textLabel[k] = new TPaveText(x1+0.02,y1+(0.02/vfactor),x1+0.07,y1+(0.06/vfactor),"NDC"); 
    PlasmaGlob::SetPaveTextStyle(textLabel[k],32); 
    textLabel[k]->SetTextFont(42);
    textLabel[k]->AddText(sLabels[k]);
    textLabel[k]->Draw();
    

    pad[k]->RedrawAxis(); 

  }
    
  C1->cd();

  // PlasmaGlob::SetPaveTextStyle(textYaxis,11); 
  char ctext[128];
  if(opt.Contains("units")) 
    sprintf(ctext,"y [#mum]");
  else
    sprintf(ctext,"k_{p} y");
  TLatex *textYaxis = new TLatex(0.05,0.6,ctext);
  textYaxis->SetTextAlign(22);
  textYaxis->SetTextFont(42);
  textYaxis->SetTextAngle(90);
  textYaxis->Draw();
  
  sprintf(ctext,"n [n_{0}]");
  TLatex *textZaxis = new TLatex(0.95,0.6,ctext);
  textZaxis->SetTextAlign(22);
  textZaxis->SetTextFont(42);
  textZaxis->SetTextAngle(90);
  textZaxis->Draw();

  
  // Output file
  TString fOutName = Form("./%s/Plots/RakeInjection/RakeInjection",pData->GetPath().c_str());
  fOutName += Form("-%s",pData->GetName());
  
  // Print to a file
  PlasmaGlob::imgconv(C1,fOutName,opt);
  // ---------------------------------------------------------

}
Exemple #4
0
void PlotEvolutionsWIII(const TString &sim, UInt_t mask = 3, const TString &options="png") { 
  
#ifdef __CINT__  
  gSystem->Load("libptools.so");
#endif

  string imask = DecToBin(mask);
  cout << Form("\n Plotting Evolultion with mask: %s",imask.c_str()) << endl; 

  PGlobals::Initialize();
  
  // Palettes!
  gROOT->Macro("PPalettes.C");

  TString opt = options;
  
  // More makeup            
  Float_t margins[4] = {0.15,0.15,0.20,0.10};
  gStyle->SetPadLeftMargin(margins[0]);  // Margin left axis  
  gStyle->SetPadRightMargin(margins[2]);
  gStyle->SetPadTopMargin(margins[3]);  // Margin left axis  
  gStyle->SetPadBottomMargin(margins[1]);

  gStyle->SetPadTickX(0);
  gStyle->SetPadTickY(0);


  if(opt.Contains("grid")) {
    gStyle->SetPadGridX(1);
    gStyle->SetPadGridY(1);
  }

  // Load first simulation data (for instance)
  PData *pData = PData::Get(sim.Data());
  Double_t E0 = pData->GetPlasmaE0();
  
  Float_t maxEcross = -999.;
  Float_t minEcross = 999.;
  Float_t maxEextr  = -999.;
  Float_t minEextr  = 999.;
  Float_t maxEdephas  = -999.;
  Float_t minEdephas  = 999.;
 
  const Int_t Nfields = 2; // E_z, E_x 
  TH2F   *hEvsTime[Nfields];
  Int_t NCross[Nfields];
  TGraph **gEcross[Nfields]; 
  TGraph **gEextr[Nfields]; 
  TGraph **gEdephas[Nfields]; 
  TGraph *gTRatio;

  TH2F   *hFvsTime;
  TGraph **gFcross = NULL;
  TGraph **gFextr = NULL;

  TH2F   *hETvsTime;
  TGraph **gETcross = NULL;
  TGraph **gETextr = NULL;

  TH2F   *hVvsTime;
  TGraph **gVcross = NULL;
  TGraph **gVextr = NULL;

  const Int_t NAtoms = 3;
  char atNames[NAtoms][4] = {"H","He","He2"};
  TH2F   *hIonProbvsTime[NAtoms]; // For H, He and He+. 
  TGraph *gIonProb10[NAtoms];
  TGraph *gIonProb100[NAtoms];
  Float_t IonTh[NAtoms] = {33.8,92.75,234.96} ;  // GV/m
  // if(!opt.Contains("units")) 
  //   for(Int_t i=0;i<NAtoms;i++) IonTh[i]  /= ( E0 / (PUnits::GV/PUnits::m));

  char hName[24];
  char gName[24];
  
  TString filename;
  filename = Form("./%s/Plots/Evolutions/Evolutions-%s.root",sim.Data(),sim.Data());
  
  TFile  *ifile = (TFile*) gROOT->GetListOfFiles()->FindObject(filename.Data());
  if (!ifile) ifile = new TFile(filename,"READ");

  TH2F *hDen1DvsTime = NULL;
  sprintf(hName,"hDenvsTime_1"); 
  hDen1DvsTime = (TH2F*) ifile->Get(hName);

  TH2F *hRmsvsTime = NULL;
  sprintf(hName,"hRmsvsTime_1"); 
  hRmsvsTime = (TH2F*) ifile->Get(hName);
    
  for(Int_t i=0;i<Nfields;i++) {
    
    sprintf(hName,"hEvsTime_%i",i); 
    hEvsTime[i] = (TH2F*) ifile->Get(hName);
    if(!hEvsTime[i]) continue;
    
    cout << Form("ANALYZING FIELD %i ...",i) << endl;

    Int_t NTBins = hEvsTime[i]->GetNbinsX();
    for(Int_t it=NTBins;it>0;it--) {
      
      // 1D field at certain timestep "it".
      TH1F *hE1D = (TH1F*) hEvsTime[i]->ProjectionY("_py",it,it);
      
      Int_t MAXCROSS = 2;
      Float_t *Cross = new Float_t[MAXCROSS];
      Float_t *Extr = new Float_t[MAXCROSS];
      memset(Cross,0,sizeof(Float_t)*MAXCROSS);
      memset(Extr,0,sizeof(Float_t)*MAXCROSS);

      Int_t auxNcross = PGlobals::HCrossings(hE1D,Cross,Extr,MAXCROSS,0.,0.);
      // cout << Form("  -> Number of crossings for histogram \"%s\" : %i ",hE1D->GetName(),auxNcross) << endl;
      // for(Int_t ic=0;ic<auxNcross;ic++) {
      // 	cout << Form(" %2i:  cross = %6.4f  extreme = %6.4f", ic, Cross[ic], Extr[ic]) << endl; 
      // }
      
      if(it==NTBins) {
	NCross[i] = auxNcross;
	
	gEcross[i] = new TGraph*[NCross[i]];
	gEextr[i] = new TGraph*[NCross[i]];

	for(Int_t ic = 0;ic<NCross[i];ic++) {
	  gEcross[i][ic] = new TGraph(NTBins);
	  sprintf(gName,"gEcross_%i_%i",i,ic); 
	  gEcross[i][ic]->SetName(gName);
	  
	  gEextr[i][ic] = new TGraph(NTBins);
	  sprintf(gName,"gEextr_%i_%i",i,ic); 
	  gEextr[i][ic]->SetName(gName);
	}

      }

      Float_t time = hEvsTime[i]->GetXaxis()->GetBinCenter(it);
      // cout << Form("Time step %i (%.2f): %i crossings",it,time,NCross[i]) << endl;
     
      for(Int_t ic=0;ic<NCross[i];ic++) {
	// cout << Form("  - Adding %i crossing: cross = %6.4f extreme = %6.4f",ic,Cross[ic],Extr[ic]) << endl;
	
	gEcross[i][ic]->SetPoint(it-1,time,Cross[ic]);
	gEextr[i][ic]->SetPoint(it-1,time,Extr[ic]);
      }
      
    }

    
    // Calculate the max and min of every crossing.
    // Also calculates dephasing:
    gEdephas[i] = new TGraph*[NCross[i]];
    for(Int_t ic = 0;ic<NCross[i];ic++) {

      Int_t Npoints = gEcross[i][ic]->GetN();
      Double_t *yEcross = gEcross[i][ic]->GetY();
      Double_t *yEextr  = gEextr[i][ic]->GetY();
      Double_t *xEextr  = gEextr[i][ic]->GetX();
      
      Double_t *yEdephas = new Double_t[Npoints];
      for(Int_t j=0;j<Npoints;j++) {
	yEdephas[j] = yEcross[j] - yEcross[0];
      }
      gEdephas[i][ic] = new TGraph(Npoints,xEextr,yEdephas);
      sprintf(gName,"gEdephas_%i_%i",i,ic); 
      gEdephas[i][ic]->SetName(gName);
      
      for(Int_t j=0;j<Npoints;j++) {
	if(yEcross[j]>maxEcross)
	  maxEcross = yEcross[j];
	if(yEcross[j]<minEcross)
	  minEcross = yEcross[j];
	
	if(yEextr[j]>maxEextr)
	  maxEextr = yEextr[j];
	if(yEextr[j]<minEextr)
	  minEextr = yEextr[j];
      
	// Only takes into account the minimums of the accelerating field:
	if(ic%2 || i!=0) continue;
	if(yEdephas[j]>maxEdephas)
	  maxEdephas = yEdephas[j];
	if(yEdephas[j]<minEdephas)
	  minEdephas = yEdephas[j];
	
      }
    
    }
  }

  // Transformer ratio vs time:
  // Take the ratio of the minimum over the maximum of the first oscillation of the Ez.
  {
    Int_t Npoints = gEextr[0][1]->GetN();
    Double_t *TR = new Double_t[Npoints];
    Double_t *yExtrPrev = gEextr[0][0]->GetY();
    Double_t *yExtr = gEextr[0][1]->GetY();
    Double_t *xExtr = gEextr[0][1]->GetX();
    for(Int_t j=0;j<Npoints;j++) {
      TR[j] = TMath::Abs(yExtr[j]/yExtrPrev[j]);	
    }
    
    gTRatio = new TGraph(Npoints,xExtr,TR);
    sprintf(gName,"gTRatio"); 
    gTRatio->SetName(gName);
  }
  

  sprintf(hName,"hVvsTime"); 
  hVvsTime = (TH2F*) ifile->Get(hName);
  Int_t NVCross = 0;
  cout << Form("ANALYZING POTENTIAL") << endl;

  Int_t NTBins = hVvsTime->GetNbinsX();
  for(Int_t it=NTBins;it>0;it--) {
      
    // 1D field at certain timestep "it".
    TH1F *hV1D = (TH1F*) hVvsTime->ProjectionY("_py",it,it);
      
    Int_t MAXCROSS = 2;
    Float_t *Cross = new Float_t[MAXCROSS];
    Float_t *Extr = new Float_t[MAXCROSS];
    memset(Cross,0,sizeof(Float_t)*MAXCROSS);
    memset(Extr,0,sizeof(Float_t)*MAXCROSS);
    
    Int_t auxNcross = PGlobals::HCrossings(hV1D,Cross,Extr,MAXCROSS,0.,20.);
    // cout << Form("  -> Number of crossings for histogram \"%s\" : %i ",hV1D->GetName(),auxNcross) << endl;
    // for(Int_t ic=0;ic<auxNcross;ic++) {
    // 	cout << Form(" %2i:  cross = %6.4f  extreme = %6.4f", ic, Cross[ic], Extr[ic]) << endl; 
    // }
    
    if(it==NTBins) {
      NVCross = auxNcross;
	
      gVcross = new TGraph*[NVCross];
      gVextr = new TGraph*[NVCross];

      for(Int_t ic = 0;ic<NVCross;ic++) {
	gVcross[ic] = new TGraph(NTBins);
	sprintf(gName,"gVcross_%i",ic); 
	gVcross[ic]->SetName(gName);
	  
	gVextr[ic] = new TGraph(NTBins);
	sprintf(gName,"gVextr_%i",ic); 
	gVextr[ic]->SetName(gName);
      }

    }

    Float_t time = hVvsTime->GetXaxis()->GetBinCenter(it);
    // cout << Form("Time step %i (%.2f): %i crossings",it,time,NVCross) << endl;
     
    for(Int_t ic=0;ic<NVCross;ic++) {
      // cout << Form("  - Adding %i crossing: cross = %6.4f extreme = %6.4f",ic,Cross[ic],Extr[ic]) << endl;
	
      gVcross[ic]->SetPoint(it-1,time,Cross[ic]);
      gVextr[ic]->SetPoint(it-1,time,Extr[ic]);
    }
      
  }


  sprintf(hName,"hETotalvsTime"); 
  hETvsTime = (TH2F*) ifile->Get(hName);
  Int_t NETCross = 0;
  cout << Form("ANALYZING TOTAL ELECTRIC FIELD") << endl;

  NTBins = hETvsTime->GetNbinsX();
  for(Int_t it=NTBins;it>0;it--) {
      
    // 1D field at certain timestep "it".
    TH1F *hET1D = (TH1F*) hETvsTime->ProjectionY("_py",it,it);
      
    Int_t MAXCROSS = 2;
    Float_t *Cross = new Float_t[MAXCROSS];
    Float_t *Extr = new Float_t[MAXCROSS];
    memset(Cross,0,sizeof(Float_t)*MAXCROSS);
    memset(Extr,0,sizeof(Float_t)*MAXCROSS);

    Float_t baseline = 0.5;
    if(opt.Contains("units"))
      baseline *= E0 / (PUnits::GV/PUnits::m);
    
    Int_t auxNcross = PGlobals::HCrossings(hET1D,Cross,Extr,MAXCROSS,baseline,-999,-999,"cum");
    // cout << Form("  -> Number of crossings for histogram \"%s\" : %i ",hET1D->GetName(),auxNcross) << endl;
    // for(Int_t ic=0;ic<auxNcross;ic++) {
    // 	cout << Form(" %2i:  cross = %6.4f  extreme = %6.4f", ic, Cross[ic], Extr[ic]) << endl; 
    // }
    
    if(it==NTBins) {
      NETCross = auxNcross;
	
      gETcross = new TGraph*[NETCross];
      gETextr = new TGraph*[NETCross];

      for(Int_t ic = 0;ic<NETCross;ic++) {
	gETcross[ic] = new TGraph(NTBins);
	sprintf(gName,"gETcross_%i",ic); 
	gETcross[ic]->SetName(gName);
	  
	gETextr[ic] = new TGraph(NTBins);
	sprintf(gName,"gETextr_%i",ic); 
	gETextr[ic]->SetName(gName);
      }

    }

    Float_t time = hETvsTime->GetXaxis()->GetBinCenter(it);
    // if(it==1)
    //   cout << Form("Time step %i (%.2f): %i crossings",it,time,NETCross) << endl;
     
    for(Int_t ic=0;ic<NETCross;ic++) {
      //if(it==1)
      //	cout << Form("  - Adding %i crossing: cross = %6.4f extreme = %6.4f",ic,Cross[ic],Extr[ic]) << endl;
	
      gETcross[ic]->SetPoint(it-1,time,Cross[ic]);
      gETextr[ic]->SetPoint(it-1,time,Extr[ic]);
      

    }
    
  }

  sprintf(hName,"hFocusvsTime"); 
  hFvsTime = (TH2F*) ifile->Get(hName);
  Int_t NFCross = 0;
  cout << Form("ANALYZING FOCUSING") << endl;

  NTBins = hFvsTime->GetNbinsX();
  for(Int_t it=NTBins;it>0;it--) {
      
    // 1D field at certain timestep "it".
    TH1F *hF1D = (TH1F*) hFvsTime->ProjectionY("_py",it,it);
      
    Int_t MAXCROSS = 2;
    Float_t *Cross = new Float_t[MAXCROSS];
    Float_t *Extr = new Float_t[MAXCROSS];
    memset(Cross,0,sizeof(Float_t)*MAXCROSS);
    memset(Extr,0,sizeof(Float_t)*MAXCROSS);

    Int_t auxNcross = PGlobals::HCrossings(hF1D,Cross,Extr,MAXCROSS,0.,0.);
    // cout << Form("  -> Number of crossings for histogram \"%s\" : %i ",hF1D->GetName(),auxNcross) << endl;
    // for(Int_t ic=0;ic<auxNcross;ic++) {
    // 	cout << Form(" %2i:  cross = %6.4f  extreme = %6.4f", ic, Cross[ic], Extr[ic]) << endl; 
    // }
    
    if(it==NTBins) {
      NFCross = auxNcross;
	
      gFcross = new TGraph*[NFCross];
      gFextr = new TGraph*[NFCross];

      for(Int_t ic = 0;ic<NFCross;ic++) {
	gFcross[ic] = new TGraph(NTBins);
	sprintf(gName,"gFcross_%i",ic); 
	gFcross[ic]->SetName(gName);
	  
	gFextr[ic] = new TGraph(NTBins);
	sprintf(gName,"gFextr_%i",ic); 
	gFextr[ic]->SetName(gName);
      }

    }

    Float_t time = hFvsTime->GetXaxis()->GetBinCenter(it);
    // cout << Form("Time step %i (%.2f): %i crossings",it,time,NFCross) << endl;
     
    for(Int_t ic=0;ic<NFCross;ic++) {
      // cout << Form("  - Adding %i crossing: cross = %6.4f extreme = %6.4f",ic,Cross[ic],Extr[ic]) << endl;
	
      gFcross[ic]->SetPoint(it-1,time,Cross[ic]);
      gFextr[ic]->SetPoint(it-1,time,Extr[ic]);
      

    }
    
  }
  

  for(Int_t i=0;i<NAtoms;i++) {
    sprintf(hName,"hIonProbvsTime_%s",atNames[i]); 
    hIonProbvsTime[i] = (TH2F*) ifile->Get(hName);
    if(!hIonProbvsTime[i]) continue;
    
    cout << Form("ANALYZING Ionization probability %i ...",i) << endl;

    Int_t NTBins = hIonProbvsTime[i]->GetNbinsX();
    for(Int_t it=NTBins;it>0;it--) {
      
      // 1D field at certain timestep "it".
      TH1F *hIonProb1D = (TH1F*) hIonProbvsTime[i]->ProjectionY("_py",it,it);
      
      Int_t MAXCROSS = 2;
      Float_t *Cross = new Float_t[MAXCROSS];
      Float_t *Extr = new Float_t[MAXCROSS];
      memset(Cross,0,sizeof(Float_t)*MAXCROSS);
      memset(Extr,0,sizeof(Float_t)*MAXCROSS);
      
      Int_t auxNcross = PGlobals::HCrossings(hIonProb1D,Cross,Extr,MAXCROSS,10.0);

      if(it==NTBins) {
	gIonProb10[i] = new TGraph(NTBins);
	sprintf(gName,"gIonProb10_%i",i); 
	gIonProb10[i]->SetName(gName);
      }

      Float_t time = hIonProbvsTime[i]->GetXaxis()->GetBinCenter(it);
      gIonProb10[i]->SetPoint(it-1,time,Cross[0]);
     
      memset(Cross,0,sizeof(Float_t)*MAXCROSS);
      memset(Extr,0,sizeof(Float_t)*MAXCROSS);
      auxNcross = PGlobals::HCrossings(hIonProb1D,Cross,Extr,MAXCROSS,99.0);
      
      if(it==NTBins) {
	gIonProb100[i] = new TGraph(NTBins);
	sprintf(gName,"gIonProb100_%i",i); 
	gIonProb100[i]->SetName(gName);
      }
      
      gIonProb100[i]->SetPoint(it-1,time,Cross[0]);
    }
  }

  // Set the color of the different evolutions according to a palette
  // UInt_t np = 50;
  // PPalette * colorPalette = (PPalette*) gROOT->FindObject("colorPalette");
  // if(!colorPalette) {
  //   const UInt_t Number = 3;
  //   Double_t Red[Number] = { 1.00, 0.00, 0.00};
  //   Double_t Green[Number]  = { 0.00, 1.00, 0.00};
  //   Double_t Blue[Number]   = { 1.00, 0.00, 1.00};
  //   Double_t Length[Number] = { 0.00, 0.50, 1.00 };
  //   colorPalette = new PPalette("colorPalette");
  //   colorPalette->CreateGradientColorTable(Number,Length,Red,Green,Blue,np);
  // }
  
  // for(Int_t i=0;i<Nfields;i++) { 
  //   for(Int_t ic=0;ic<Ncross;ic++) {
  //     Float_t step = (np/Nosc);
  //     Int_t icolor = TMath::Nint( ((ic+1)/2) * step - 1 );
  //     gEextr[i][ic]->SetLineColor(colorPalette->GetColor(icolor));
  //     gEextr[i][ic]->SetLineWidth(2);
  //     gEdephas[i][ic]->SetLineColor(colorPalette->GetColor(icolor));
  //     gEdephas[i][ic]->SetLineWidth(2);
  //   }
  // }
  // --------------------------------------------------------------------------

  // Manual coloring:
  const Int_t NCOLORS = 5;
  //  Int_t colors[NCOLORS] = {kMagenta+2,kRed,kBlue,kYellow+2,kCyan+2};
  Int_t colors[NCOLORS] = {kGray+3,kGray+2,kGray+1,kGray};
  for(Int_t i=0;i<Nfields;i++) { 
    for(Int_t ic=0;ic<NCross[i];ic++) {

      if( !gEcross[i][ic] || !gEextr[i][ic] ) continue;
            
      Int_t index = ic/2;
      if(index>=NCOLORS) index = NCOLORS-1;
      gEcross[i][ic]->SetLineColor(colors[index]);
      gEextr[i][ic]->SetLineColor(colors[index]);
      gEextr[i][ic]->SetLineWidth(1);
      gEdephas[i][ic]->SetLineColor(colors[index]);
      gEdephas[i][ic]->SetLineWidth(1);

      // cout << "EEEOOO" << endl;
      // if(ic%2) { 
      // 	gEcross[i][ic]->SetLineStyle(2);
      // 	gEextr[i][ic]->SetLineStyle(2);
      // 	gEdephas[i][ic]->SetLineStyle(2);
      // } else {
      // 	gEcross[i][ic]->SetLineStyle(1);
      // 	gEextr[i][ic]->SetLineStyle(1);
      // 	gEdephas[i][ic]->SetLineStyle(1);
      // }
      
      
    }
  }


  for(Int_t ic = 0;ic<NFCross;ic++) {
    // Graph's attributes
    Int_t index = ic/2;
    if(index>=NCOLORS) index = NCOLORS-1;
    gFcross[ic]->SetLineColor(colors[index]);
    if(ic%2-1) { 
      gFcross[ic]->SetLineStyle(2);
      gFextr[ic]->SetLineStyle(2);
      
    } else {
      gFcross[ic]->SetLineStyle(1);
      gFextr[ic]->SetLineStyle(1);
    }
  }

  for(Int_t ic = 0;ic<NVCross;ic++) {
    // Graph's attributes
    Int_t index = ic/2;
    if(index>=NCOLORS) index = NCOLORS-1;
    gVcross[ic]->SetLineColor(colors[index]);
    gVcross[ic]->SetLineStyle(3);
    gVextr[ic]->SetLineStyle(1);
  }
  
  for(Int_t ic = 0;ic<NETCross;ic++) {
    // Graph's attributes
    Int_t index = ic/2;
    if(index>=NCOLORS) index = NCOLORS-1;
    gETcross[ic]->SetLineColor(colors[index]);
    if(ic%2-1) { 
      gETcross[ic]->SetLineStyle(2);
      gETextr[ic]->SetLineStyle(2);
      
    } else {
      gETcross[ic]->SetLineStyle(1);
      gETextr[ic]->SetLineStyle(1);
    }
  }
  
  for(Int_t i=0;i<NAtoms;i++) {
    gIonProb10[i]->SetLineStyle(2);
    gIonProb10[i]->SetLineColor(kGray+2);

    gIonProb100[i]->SetLineStyle(1);
    gIonProb100[i]->SetLineColor(kGray+2);
  }
  

  // Plotting
  // ------------------------------------------------------------
  
  // Canvas setup
  UInt_t NPad = BitCounter(mask);
  if(NPad==0) NPad = 1;
  Float_t ypadsize = 250;
  Float_t ymarginsize = 200;
  if(NPad==1) ypadsize = 300;
  Float_t ysize = ypadsize * NPad + ymarginsize; 
  Float_t boom = 1.2;
  if(opt.Contains("boom"))
    ysize *= boom;
  TCanvas *C = new TCanvas("C","Snapshot",1050,ysize);
  C->SetFillStyle(4000);

  UInt_t lineColor = kOrange+10;
  //UInt_t lineColor =  TColor::GetColor(196,30,78);
  
  // Setup Pad layout:
  TPad **pad = new TPad*[NPad];
  TH2F **hFrame = new TH2F*[NPad];
  Float_t bMargin = 0.12 * (950/ysize);
  Float_t tMargin = 0.04 * (950/ysize);
  Float_t lMargin = 0.14;
  Float_t rMargin = 0.18;
  Float_t mMargin = 0.015 * (950/ysize);
  Float_t pfactor = 1.0;
  if(opt.Contains("nomar"))
    bMargin = tMargin = lMargin = rMargin = mMargin = 0.0;
  if(NPad==1)
    PGlobals::CanvasPartition(C,NPad,lMargin,rMargin,bMargin,tMargin,mMargin);
  else
    PGlobals::CanvasAsymPartition(C,NPad,lMargin,rMargin,bMargin,tMargin,pfactor,mMargin);
 
  // Define the frames for plotting
  Int_t fonttype = 43;
  Int_t fontsize = 32;
  Int_t tfontsize = 38;
  Int_t txsize = tfontsize+6;
  Int_t lxsize = fontsize;
  Int_t tysize = tfontsize;
  Int_t lysize = fontsize-2;
  Int_t tzsize = tfontsize-4;
  Int_t lzsize = fontsize-2;
  Float_t txoffset = (250/ypadsize) * 2.4 / (950/ysize);
  Float_t lxoffset = 0.015;
  Float_t tyoffset = 1.2 / (950/ysize);
  Float_t lyoffset = 0.01;
  Float_t tzoffset = 1.4 / (950/ysize);
  Float_t lzoffset = 0.01;
  Float_t tylength = 0.015;
  Float_t txlength = 0.04;
  for(Int_t i=NPad-1;i>=0;i--) {
    char name[16];
    sprintf(name,"pad_%i",i);
    pad[i] = (TPad*) gROOT->FindObject(name);
    pad[i]->SetFrameLineWidth(2);  
    pad[i]->SetTickx(1);
    pad[i]->SetTicky(1);
    if(opt.Contains("trans"))
      pad[i]->SetFillStyle(4000);
    pad[i]->SetFrameFillStyle(4000);

    sprintf(name,"hFrame_%i",i);
    hFrame[i] = (TH2F*) gROOT->FindObject(name);
    if(hFrame[i]) delete hFrame[i];
    hFrame[i] = (TH2F*) hEvsTime[0]->Clone(name);
    hFrame[i]->Reset();
    
    Float_t xFactor = pad[NPad-1]->GetAbsWNDC()/pad[i]->GetAbsWNDC();
    Float_t yFactor = pad[NPad-1]->GetAbsHNDC()/pad[i]->GetAbsHNDC();

    // Format for y axis
    hFrame[i]->GetYaxis()->SetLabelFont(fonttype);
    hFrame[i]->GetYaxis()->SetLabelSize(lysize);
    hFrame[i]->GetYaxis()->SetLabelOffset(lyoffset);
    hFrame[i]->GetYaxis()->SetTitleFont(fonttype);
    hFrame[i]->GetYaxis()->SetTitleSize(tysize);
    hFrame[i]->GetYaxis()->SetTitleOffset(tyoffset);

    hFrame[i]->GetYaxis()->SetTickLength(xFactor*tylength/yFactor);

    // Format for x axis
    hFrame[i]->GetXaxis()->SetLabelFont(fonttype);
    hFrame[i]->GetXaxis()->SetLabelSize(lxsize);
    hFrame[i]->GetXaxis()->SetLabelOffset(lxoffset);
    hFrame[i]->GetXaxis()->SetTitleFont(fonttype);
    hFrame[i]->GetXaxis()->SetTitleSize(txsize);
    hFrame[i]->GetXaxis()->SetTitleOffset(txoffset);
    
    hFrame[i]->GetXaxis()->SetTickLength(yFactor*txlength/xFactor);      

    if(i>0) { // skip x axis labels except for the lowest one
      hFrame[i]->GetXaxis()->SetLabelSize(0.0);
      hFrame[i]->GetXaxis()->SetTitleSize(0.0);
    }

    if(opt.Contains("nomar")) {
      hFrame[i]->GetYaxis()->SetTickLength(0.0);
      hFrame[i]->GetXaxis()->SetTickLength(0.0);      
    }

    // Labels for the frames

  }

  // Access to color Palettes 
  TExec *exPlasma = new TExec("exPlasma","plasmaPalette->cd();");
  TExec *exElec   = new TExec("exElec","electron0Palette->cd();");
  TExec *exHot    = new TExec("exHot","hotPalette->cd();");
  TExec *exField  = new TExec("exField","rbowwhitePalette->cd();");
  TExec *exFieldT = new TExec("exFieldT","red0Palette->cd();");
  TExec *exIonP   = new TExec("exIonP","redelectron0Palette->cd();");
  TExec *exPot    = new TExec("exPot","rbowinvPalette->cd();");

  

  // Actual Plotting!
  // ------------------------------------------------------------

  C->cd(0);

  Float_t x1,x2,y1,y2;
  Float_t gap = 0.01;
  TPaletteAxis *palette = NULL;
  UInt_t ip = NPad-1;

  if(mask & 0x1) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hDen1DvsTime->GetZaxis()->SetNdivisions(503);
    hDen1DvsTime->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hDen1DvsTime->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exElec->Draw();
    hDen1DvsTime->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hDen1DvsTime->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }
   
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }

  if(mask & 0x2) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hRmsvsTime->GetZaxis()->SetNdivisions(503);
    hRmsvsTime->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hRmsvsTime->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exElec->Draw();
    hRmsvsTime->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hRmsvsTime->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }
   
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }

  if(mask & 0x4) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hEvsTime[0]->GetZaxis()->SetNdivisions(503);
    hEvsTime[0]->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hEvsTime[0]->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exField->Draw();
    hEvsTime[0]->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hEvsTime[0]->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }

    if(!opt.Contains("nocross")) {
    
      for(Int_t ic=0;ic<NVCross;ic++) {
	if( gVcross[ic] )
	  gVcross[ic]->Draw("L");
      }

      if(gEcross[1][0]) {
	gEcross[1][0]->SetLineStyle(4);
	gEcross[1][0]->Draw("L");
      }
    }

    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }

  if(mask & 0x8) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hEvsTime[1]->GetZaxis()->SetNdivisions(503);
    hEvsTime[1]->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hEvsTime[1]->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exField->Draw();
    hEvsTime[1]->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hEvsTime[1]->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }
   
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }

  if(mask & 0x10) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hETvsTime->GetZaxis()->SetNdivisions(503);
    hETvsTime->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hETvsTime->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exFieldT->Draw();
    hETvsTime->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hETvsTime->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }

    if(!opt.Contains("nocross")) {
    
      for(Int_t ic=0;ic<NVCross;ic++) {
	if( gVcross[ic] )
	  gVcross[ic]->Draw("L");
      }

      if(gEcross[1][0]) {
	gEcross[1][0]->SetLineStyle(4);
	gEcross[1][0]->Draw("L");
      }
    }
     
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }

  if(mask & 0x20) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    //  hVvsTime->GetZaxis()->SetNdivisions(503);
    hVvsTime->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hVvsTime->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    
    exPot->Draw();
    hVvsTime->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hVvsTime->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }
   
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }
  

  if(mask & 0x40) {

    pad[ip]->Draw();
    pad[ip]->cd(); 
    if(opt.Contains("logz")) {
      pad[ip]->SetLogz(1);
    } else {
      pad[ip]->SetLogz(0);
    }

    hFrame[ip]->Draw("col");

    hIonProbvsTime[1]->GetZaxis()->SetNdivisions(503);
    hIonProbvsTime[1]->GetZaxis()->SetTitleFont(fonttype);
    Float_t xFactor = pad[0]->GetAbsWNDC()/pad[ip]->GetAbsWNDC();
    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[ip]->GetAbsHNDC();
    hIonProbvsTime[1]->GetZaxis()->SetTickLength(xFactor*tylength/yFactor);
    hIonProbvsTime[1]->GetZaxis()->SetRangeUser(0.,0.12);
    
    // exFieldT->Draw();
    // exPlasma->Draw();
    exIonP->Draw();
    hIonProbvsTime[1]->Draw("colz same");

    pad[ip]->Update();
  
    y1 = pad[ip]->GetBottomMargin();
    y2 = 1 - pad[ip]->GetTopMargin();
    x1 = pad[ip]->GetLeftMargin();
    x2 = 1 - pad[ip]->GetRightMargin();
  
    palette = (TPaletteAxis*) hIonProbvsTime[1]->GetListOfFunctions()->FindObject("palette");  
    if(palette) {
      palette->SetY2NDC(y2 - gap);
      palette->SetY1NDC(y1 + gap);
      palette->SetX1NDC(x2 + 0.005);
      palette->SetX2NDC(x2 + 0.03);
      palette->SetTitleOffset(tzoffset);
      palette->SetTitleSize(tzsize);
      palette->SetLabelFont(fonttype);
      palette->SetLabelSize(lzsize);
      palette->SetLabelOffset(lyoffset);
      palette->SetBorderSize(2);
      palette->SetLineColor(1);
    }

    if(!opt.Contains("nocross")) {
      
      for(Int_t ic=0;ic<NVCross;ic++) {
	if( gVcross[ic] )
	  gVcross[ic]->Draw("L");
      }
      
      if(gEcross[1][0]) {
	gEcross[1][0]->SetLineStyle(4);
	gEcross[1][0]->Draw("L");
      }
    }
     
    pad[ip]->RedrawAxis(); 

    ip--;
    C->cd(0);
    
  }
  
  // Print to a file
  // Output file
  TString fOutName = Form("./%s/Plots/Evolutions/Evolutions%s-WII-%s",sim.Data(),imask.c_str(),sim.Data());
  PGlobals::imgconv(C,fOutName,opt);
  // ---------------------------------------------------------

  
  ifile->Close();
  cout << endl;
}
Exemple #5
0
void PlotPotential2D( const TString &sim, Int_t time, Int_t zoom=2, Int_t Nbins=2, const TString &options="") {

#ifdef __CINT__
    gSystem->Load("libplasma.so");
#endif

    PlasmaGlob::Initialize();

    // Palettes!
    gROOT->Macro("PlasmaPalettes.C");

    // Init Units table
    PUnits::UnitsTable::Get();

    // Load PData
    PData *pData = PData::Get(sim.Data());
    pData->LoadFileNames(time);
    if(!pData->IsInit()) return;

    TString opt = options;

    // More makeup
    gStyle->SetPadGridY(0);
    if(opt.Contains("gridx")) {
        gStyle->SetPadGridX(1);
    }
    if(opt.Contains("gridy")) {
        gStyle->SetPadGridY(1);
    }


    // Some plasma constants
    Double_t n0 = pData->GetPlasmaDensity();
    Double_t omegap = pData->GetPlasmaFrequency();
    Double_t timedepth = 1.;
    if(omegap!=0.0) timedepth = 1/omegap;
    Double_t kp = pData->GetPlasmaK();
    Double_t skindepth = 1.;
    if(kp!=0.0) skindepth = 1/kp;
    Double_t E0 = pData->GetPlasmaE0();

    // Some beam properties:
    Double_t Ebeam = pData->GetBeamEnergy();
    Double_t gamma = pData->GetBeamGamma();
    Double_t vbeam = pData->GetBeamVelocity();

    cout << Form(" - Bunch gamma      = %8.4f", gamma ) << endl;
    cout << Form(" - Bunch velocity   = %8.4f c", vbeam ) << endl;

    // Other parameters
    Float_t trapPotential = 1.0 - (1.0/gamma);
    cout << Form(" - Trap. potential  = %8.4f mc2/e",trapPotential) << endl;
    cout << endl;

    // Time in OU
    Float_t Time = pData->GetRealTime();
    // z start of the plasma in normalized units.
    Float_t zStartPlasma = pData->GetPlasmaStart()*kp;
    // z start of the beam in normalized units.
    Float_t zStartBeam = pData->GetBeamStart()*kp;
    // z start of the neutral in normalized units.
    Float_t zStartNeutral = pData->GetNeutralStart()*kp;
    // z end of the neutral in normalized units.
    Float_t zEndNeutral = pData->GetNeutralEnd()*kp;

    if(opt.Contains("center")) {
        Time -= zStartPlasma;
        if(opt.Contains("comov"))      // Centers on the head of the beam.
            Time += zStartBeam;
    }
    Float_t shiftz = pData->Shift(opt);
    //  cout << "Shift = " << shiftz << endl;


    // Calculate the "axis range" in number of bins. If Nbins==0 a RMS width is taken.
    Double_t rms0 = pData->GetBeamRmsY() * kp;
    if(pData->IsCyl())  rms0  = pData->GetBeamRmsR() * kp;

    Int_t FirstyBin = 0;
    Int_t LastyBin = 0;
    if(Nbins==0) {
        if(rms0>0.0)
            Nbins =  TMath::Nint(rms0 / pData->GetDX(1));
        else
            Nbins = 1;
    }

    // Slice width limits.
    if(!pData->IsCyl()) {
        FirstyBin = pData->GetNX(1)/2 + 1 - Nbins;
        LastyBin =  pData->GetNX(1)/2 + Nbins;
    } else {
        FirstyBin = 1;
        LastyBin  = Nbins;
    }
    // ----------------------------------------------------------------------------------


    // Get charge density histos
    Int_t Nspecies = pData->NSpecies();
    TH2F **hDen2D = new TH2F*[Nspecies];
    // Get charge density on-axis
    TH1F **hDen1D = new TH1F*[Nspecies];
    // And electric current (integrated)
    TH1F **hCur1D = new TH1F*[Nspecies];
    for(Int_t i=0; i<Nspecies; i++) {

        hDen2D[i] = NULL;

        if(!pData->GetChargeFileName(i))
            continue;

        cout << Form(" Getting charge density of specie: ") << i << endl;


        char hName[24];
        sprintf(hName,"hDen2D_%i",i);
        hDen2D[i] = (TH2F*) gROOT->FindObject(hName);
        if(hDen2D[i]) delete hDen2D[i];

        if(!pData->Is3D())
            hDen2D[i] = pData->GetCharge(i,opt);
        else
            hDen2D[i] = pData->GetCharge2DSliceZY(i,-1,Nbins,opt+"avg");

        hDen2D[i]->SetName(hName);
        hDen2D[i]->GetXaxis()->CenterTitle();
        hDen2D[i]->GetYaxis()->CenterTitle();
        hDen2D[i]->GetZaxis()->CenterTitle();

        if(opt.Contains("comov"))
            hDen2D[i]->GetXaxis()->SetTitle("k_{p} #zeta");
        else
            hDen2D[i]->GetXaxis()->SetTitle("k_{p} z");

        if(pData->IsCyl())
            hDen2D[i]->GetYaxis()->SetTitle("k_{p} r");
        else
            hDen2D[i]->GetYaxis()->SetTitle("k_{p} y");

        hDen2D[i]->GetZaxis()->SetTitle("n [n_{0}]");


        hDen1D[i] = NULL;
        hCur1D[i] = NULL;

        if(!pData->GetEfieldFileName(i))
            continue;

        sprintf(hName,"hDen1D_%i",i);
        hDen1D[i] = (TH1F*) gROOT->FindObject(hName);
        if(hDen1D[i]) delete hDen1D[i];

        // 1D histograms
        if(pData->Is3D()) {
            hDen1D[i] = pData->GetH1SliceZ3D(pData->GetChargeFileName(i)->c_str(),"charge",-1,Nbins,-1,Nbins,opt+"avg");
        } else if(pData->IsCyl()) { // Cylindrical: The first bin with r>0 is actually the number 1 (not the 0).
            hDen1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",1,Nbins,opt+"avg");
        } else { // 2D cartesian
            hDen1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",-1,Nbins,opt+"avg");
        }
        hDen1D[i]->SetName(hName);

        // if(hDen1D[i]) delete hDen1D[i];
        // hDen1D[i] = (TH1F*) hE2D[i]->ProjectionX(hName,FirstyBin,LastyBin);
        // hDen1D[i]->Scale(1.0/(LastyBin-FirstyBin+1));

        if(opt.Contains("comov"))
            hDen1D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
        else
            hDen1D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");

        if(i==0)
            hDen1D[i]->GetYaxis()->SetTitle("n/n_{0}");
        else if(i==1)
            hDen1D[i]->GetYaxis()->SetTitle("n_{b}/n_{0}");
        else
            hDen1D[i]->GetYaxis()->SetTitle("n_{i}/n_{0}");

        // Get the current:
        if(i==0) continue;

        sprintf(hName,"hCur1D_%i",i);
        hCur1D[i] = (TH1F*) gROOT->FindObject(hName);
        if(hCur1D[i]) delete hCur1D[i];

        if(opt.Contains("curr")) {
            // To get the current is needed to read in a wider transverse range which includes all the charge.
            Int_t NbinsT = 100;
            if(pData->Is3D()) {
                hCur1D[i] = pData->GetH1SliceZ3D(pData->GetChargeFileName(i)->c_str(),"charge",-1,NbinsT,-1,NbinsT,opt+"int");
            } else if(pData->IsCyl()) { // Cylindrical: The first bin with r>0 is actually the number 1 (not the 0).
                hCur1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",1,NbinsT,opt+"int");
            } else { // 2D cartesian
                hCur1D[i] = pData->GetH1SliceZ(pData->GetChargeFileName(i)->c_str(),"charge",-1,NbinsT,opt+"int");
            }
            hCur1D[i]->SetName(hName);

            if(opt.Contains("comov")) {
                hCur1D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
                hCur1D[i]->GetYaxis()->SetTitle("dn/d#zeta [(n_{0}/k_{p}^{3}) (#omega_{p}/c)]");
            } else {
                hCur1D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");
                hCur1D[i]->GetYaxis()->SetTitle("dn/dz [(n_{0}/k_{p}^{3}) (#omega_{p}/c)]");
            }

            Int_t NB = hCur1D[i]->GetNbinsX();
            Float_t dx = (hCur1D[i]->GetBinLowEdge(1)-hCur1D[i]->GetBinLowEdge(NB+1))/NB;

            // hCur1D[i]->Scale(dx);
            Float_t Charge = hCur1D[i]->Integral() * dx;

            cout << Form(" Integrated charge of specie %3i = %8.4f n0 * kp^-3",i,Charge) << endl;
        }
    }


    // Get electric fields 2D
    const Int_t Nfields = 3;
    TH2F **hE2D = new TH2F*[Nfields];
    TH1F **hE1D = new TH1F*[Nfields];
    TH2F *hV2D = NULL;
    TH1F *hV1D = NULL;
    for(Int_t i=0; i<Nfields; i++) {
        hE2D[i] = NULL;
        hE1D[i] = NULL;

        if(!pData->GetEfieldFileName(i))
            continue;

        cout << Form(" Getting electric field number ") << i+1 << endl;

        char hName[24];
        sprintf(hName,"hE2D_%i",i);
        hE2D[i] = (TH2F*) gROOT->FindObject(hName);
        if(hE2D[i]) delete hE2D[i];

        if(!pData->Is3D())
            hE2D[i] = pData->GetEField(i,opt);
        else
            hE2D[i] = pData->GetEField2DSliceZY(i,-1,Nbins,opt+"avg");

        hE2D[i]->SetName(hName);
        hE2D[i]->GetXaxis()->CenterTitle();
        hE2D[i]->GetYaxis()->CenterTitle();
        hE2D[i]->GetZaxis()->CenterTitle();
        if(opt.Contains("comov"))
            hE2D[i]->GetXaxis()->SetTitle("k_{p} #zeta");
        else
            hE2D[i]->GetXaxis()->SetTitle("k_{p} z");

        if(pData->IsCyl())
            hE2D[i]->GetYaxis()->SetTitle("k_{p} r");
        else
            hE2D[i]->GetYaxis()->SetTitle("k_{p} y");

        if(i==0)
            hE2D[i]->GetZaxis()->SetTitle("E_{z}/E_{0}");
        else if(i==1)
            hE2D[i]->GetZaxis()->SetTitle("E_{y}/E_{0}");
        else if(i==2)
            hE2D[i]->GetZaxis()->SetTitle("E_{x}/E_{0}");

        sprintf(hName,"hE1D_%i",i);
        hE1D[i] = (TH1F*) gROOT->FindObject(hName);
        if(hE1D[i]) delete hE1D[i];

        // 1D histograms
        char nam[3];
        sprintf(nam,"e%i",i+1);
        if(pData->Is3D()) {

            if(i==0)
                hE1D[i] = pData->GetH1SliceZ3D(pData->GetEfieldFileName(i)->c_str(),nam,-1,Nbins,-1,Nbins,opt+"avg");
            else
                hE1D[i] = pData->GetH1SliceZ3D(pData->GetEfieldFileName(i)->c_str(),nam,-Nbins,Nbins,-Nbins,Nbins,opt+"avg");

        } else if(pData->IsCyl()) { // Cylindrical: The first bin with r>0 is actually the number 1 (not the 0).

            hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,1,Nbins,opt+"avg");

        } else { // 2D cartesian

            if(i==0)
                hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,-1,Nbins,opt+"avg");
            else
                hE1D[i] = pData->GetH1SliceZ(pData->GetEfieldFileName(i)->c_str(),nam,-Nbins,Nbins,opt+"avg");
        }

        hE1D[i]->SetName(hName);
        if(opt.Contains("comov"))
            hE1D[i]->GetXaxis()->SetTitle("#zeta [c/#omega_{p}]");
        else
            hE1D[i]->GetXaxis()->SetTitle("z [c/#omega_{p}]");

        if(i==0)
            hE1D[i]->GetYaxis()->SetTitle("E_{z} [E_{0}]");
        else if(i==1)
            hE1D[i]->GetYaxis()->SetTitle("E_{y} [E_{0}]");
        else if(i==2)
            hE1D[i]->GetYaxis()->SetTitle("E_{x} [E_{0}]");

        // Alternative
        // if(hE1D[i]) delete hE1D[i];
        // hE1D[i] = (TH1F*) hE2D[i]->ProjectionX(hName,FirstyBin,LastyBin);
        // hE1D[i]->Scale(1.0/(LastyBin-FirstyBin+1));

        if(i==0) {
            Int_t   NbinsX = hE2D[i]->GetNbinsX();
            Int_t   NbinsY = hE2D[i]->GetNbinsY();

            Float_t dx = pData->GetDX(0);

            sprintf(hName,"hV2D");
            hV2D = (TH2F*) hE2D[i]->Clone(hName);
            hV2D->Reset();

            sprintf(hName,"hV1D");
            hV1D = (TH1F*) hE1D[i]->Clone(hName);
            hV1D->Reset();

            for(Int_t j=NbinsY; j>0; j--) {
                Double_t integral = 0.0;
                for(Int_t k=NbinsX; k>0; k--) {
                    integral += hE2D[i]->GetBinContent(k,j) * dx;
                    hV2D->SetBinContent(k,j,integral);
                }
            }

            Double_t integral = 0.0;
            for(Int_t k=NbinsX; k>0; k--) {
                integral += hE1D[i]->GetBinContent(k) * dx;
                hV1D->SetBinContent(k,integral);
            }

        }

    }

    // Now, combine the electric field components into the total |E|
    // and calculate ionization probability for He:
    // Outter Helium electron
    Double_t Eion0 = 24.59 * PUnits::eV;
    Double_t Z     = 1;

    TH2F *hETotal2D = (TH2F*) hE2D[0]->Clone("hETotal2D");
    hETotal2D->Reset();
    TH2F *hIonProb2D = (TH2F*) hE2D[0]->Clone("hIonProb2D");
    hIonProb2D->Reset();
    TH1F *hETotal1D = (TH1F*) hE1D[0]->Clone("hETotal1D");
    hETotal1D->Reset();
    TH1F *hIonProb1D = (TH1F*) hE1D[0]->Clone("hIonProb1D");
    hIonProb1D->Reset();
    {
        Int_t NbinsX = hE2D[0]->GetNbinsX();
        Int_t NbinsY = hE2D[0]->GetNbinsY();
        for(Int_t j=0; j<NbinsX; j++) {
            for(Int_t k=0; k<NbinsY; k++) {
                Double_t E1 = hE2D[0]->GetBinContent(j,k);
                Double_t E2 = hE2D[1]->GetBinContent(j,k);
                Double_t E3 = hE2D[2]->GetBinContent(j,k);
                Double_t E  = TMath::Sqrt(E1*E1+E2*E2+E3*E3);

                hETotal2D->SetBinContent(j,k,E);

                E *= E0;

                // Double_t IonProb = (PFunc::ADK(E,Eion0,Z,l,m)/PUnits::atomictime)*PUnits::femtosecond;
                Double_t IonProb = PFunc::ADK_ENG(E,Eion0,Z) * PUnits::femtosecond;
                // if(IonProb>1) IonProb = 1.0;
                // cout << "Ion prob = " << IonProb << endl;
                hIonProb2D->SetBinContent(j,k,IonProb);
            }
            Double_t E1 = hE1D[0]->GetBinContent(j);
            Double_t E2 = hE1D[1]->GetBinContent(j);
            Double_t E3 = hE1D[2]->GetBinContent(j);
            Double_t E  = TMath::Sqrt(E1*E1+E2*E2+E3*E3);

            hETotal1D->SetBinContent(j,E);

            E *= E0;

            // Double_t IonProb = (PFunc::ADK(E,Eion0,Z,l,m)/PUnits::atomictime)*PUnits::femtosecond;
            Double_t IonProb = PFunc::ADK_ENG(E,Eion0,Z) * PUnits::femtosecond;
            // cout << "Ion prob = " << IonProb << endl;

            hIonProb1D->SetBinContent(j,IonProb);


        }
    }
    hETotal2D->GetZaxis()->SetTitle("E [E_{0}]");
    hIonProb2D->GetZaxis()->SetTitle("W_{ADK} [fs^{-1}]");
    hETotal1D->GetYaxis()->SetTitle("E [E_{0}]");
    hIonProb1D->GetYaxis()->SetTitle("W_{ADK} [fs^{-1}]");



    // Tunning the Histograms
    // ---------------------

    // Chaning to user units:
    // --------------------------

    if(opt.Contains("units") && n0) {

        for(Int_t i=0; i<Nspecies; i++) {

            if(!hDen2D[i]) continue;

            Int_t NbinsX = hDen2D[i]->GetNbinsX();
            Float_t xMin = skindepth * hDen2D[i]->GetXaxis()->GetXmin() / PUnits::um;
            Float_t xMax = skindepth * hDen2D[i]->GetXaxis()->GetXmax() / PUnits::um;
            Int_t NbinsY = hDen2D[i]->GetNbinsY();
            Float_t ymin = skindepth * hDen2D[i]->GetYaxis()->GetXmin() / PUnits::um;
            Float_t ymax = skindepth * hDen2D[i]->GetYaxis()->GetXmax() / PUnits::um;
            hDen2D[i]->SetBins(NbinsX,xMin,xMax,NbinsY,ymin,ymax);
            // for(Int_t j=0;j<hDen2D[i]->GetNbinsX();j++) {
            // 	for(Int_t k=0;k<hDen2D[i]->GetNbinsY();k++) {
            // 	  hDen2D[i]->SetBinContent(j,k, hDen2D[i]->GetBinContent(j,k) * n0 / (1e17/PUnits::cm3) );
            // 	}
            // }

            if(pData->IsCyl())
                hDen2D[i]->GetYaxis()->SetTitle("r [#mum]");
            else
                hDen2D[i]->GetYaxis()->SetTitle("y [#mum]");

            if(opt.Contains("comov"))
                hDen2D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
            else
                hDen2D[i]->GetXaxis()->SetTitle("z [#mum]");

            // if(i==0)
            // 	hDen2D[i]->GetZaxis()->SetTitle("n_{e} [10^{17}/cm^{3}]");
            // else if(i==1)
            // 	hDen2D[i]->GetZaxis()->SetTitle("n_{b} [10^{17}/cm^{3}]");
            // else
            // 	hDen2D[i]->GetZaxis()->SetTitle("n_{i} [10^{17}/cm^{3}]");

            hDen1D[i]->SetBins(NbinsX,xMin,xMax);
            // for(Int_t j=0;j<hDen1D[i]->GetNbinsX();j++) {
            // 	hDen1D[i]->SetBinContent(j, hDen1D[i]->GetBinContent(j) * n0 / (1e17/PUnits::cm3) );
            // }


            if(opt.Contains("comov"))
                hDen1D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
            else
                hDen1D[i]->GetXaxis()->SetTitle("z [#mum]");

            if(hCur1D[i]) {

                hCur1D[i]->SetBins(NbinsX,xMin,xMax);
                Double_t binSize = (xMax - xMin)/NbinsX;  // bin size in um.

                Double_t dV = skindepth * skindepth * skindepth;
                Double_t lightspeed =  PConst::c_light / (PUnits::um/PUnits::femtosecond);

                hCur1D[i]->Scale(TMath::Abs(n0 * dV * (PConst::ElectronCharge/PUnits::picocoulomb) * (kp * PConst::c_light * PUnits::femtosecond)));

                hCur1D[i]->GetYaxis()->SetTitle("I[kA]");
                hCur1D[i]->GetYaxis()->SetTitle("");
                if(opt.Contains("comov"))
                    hCur1D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
                else
                    hCur1D[i]->GetXaxis()->SetTitle("z [#mum]");


                Float_t Charge = hCur1D[i]->Integral() * (binSize / lightspeed);
                cout << Form(" Integrated charge of specie %3i = %8f pC",i,Charge) << endl;
            }
        }


        for(Int_t i=0; i<Nfields; i++) {
            Int_t NbinsX = hE2D[i]->GetNbinsX();
            Float_t xMin = skindepth * hE2D[i]->GetXaxis()->GetXmin() / PUnits::um;
            Float_t xMax = skindepth * hE2D[i]->GetXaxis()->GetXmax() / PUnits::um;
            Int_t NbinsY = hE2D[i]->GetNbinsY();
            Float_t ymin = skindepth * hE2D[i]->GetYaxis()->GetXmin() / PUnits::um;
            Float_t ymax = skindepth * hE2D[i]->GetYaxis()->GetXmax() / PUnits::um;
            hE2D[i]->SetBins(NbinsX,xMin,xMax,NbinsY,ymin,ymax);
            hE1D[i]->SetBins(NbinsX,xMin,xMax);

            for(Int_t j=0; j<hE2D[i]->GetNbinsX(); j++) {
                for(Int_t k=0; k<hE2D[i]->GetNbinsY(); k++) {
                    hE2D[i]->SetBinContent(j,k, hE2D[i]->GetBinContent(j,k) * ( E0 / (PUnits::GV/PUnits::m) ) );
                }
                hE1D[i]->SetBinContent(j, hE1D[i]->GetBinContent(j) * ( E0 / (PUnits::GV/PUnits::m) ) );
            }

            if(pData->IsCyl())
                hE2D[i]->GetYaxis()->SetTitle("r [#mum]");
            else
                hE2D[i]->GetYaxis()->SetTitle("y [#mum]");

            if(opt.Contains("comov"))
                hE2D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
            else
                hE2D[i]->GetXaxis()->SetTitle("z [#mum]");

            if(i==0)
                hE2D[i]->GetZaxis()->SetTitle("E_{z} [GV/m]");
            else if(i==1)
                hE2D[i]->GetZaxis()->SetTitle("E_{y} [GV/m]");
            else if(i==2)
                hE2D[i]->GetZaxis()->SetTitle("E_{x} [GV/m]");


            if(opt.Contains("comov"))
                hE1D[i]->GetXaxis()->SetTitle("#zeta [mm]");
            else
                hE1D[i]->GetXaxis()->SetTitle("z [mm]");

            if(i==0)
                hE1D[i]->GetYaxis()->SetTitle("E_{z} [GV/m]");
            else if(i==1)
                hE1D[i]->GetYaxis()->SetTitle("E_{y} [GV/m]");
            else if(i==2)
                hE1D[i]->GetYaxis()->SetTitle("E_{x} [GV/m]");


            if(i==0) {
                hV2D->SetBins(NbinsX,xMin,xMax,NbinsY,ymin,ymax);
                hETotal2D->SetBins(NbinsX,xMin,xMax,NbinsY,ymin,ymax);
                hIonProb2D->SetBins(NbinsX,xMin,xMax,NbinsY,ymin,ymax);
                hV1D->SetBins(NbinsX,xMin,xMax);
                hETotal1D->SetBins(NbinsX,xMin,xMax);
                hIonProb1D->SetBins(NbinsX,xMin,xMax);
                for(Int_t j=0; j<NbinsX; j++) {
                    for(Int_t k=0; k<NbinsY; k++) {
                        hV2D->SetBinContent(j,k, hV2D->GetBinContent(j,k) * E0 * skindepth / (PUnits::MV));
                        hETotal2D->SetBinContent(j,k, hETotal2D->GetBinContent(j,k) * ( E0 / (PUnits::GV/PUnits::m) ) );
                    }
                    hV1D->SetBinContent(j, hV1D->GetBinContent(j) * ( E0 * skindepth / (PUnits::MV) ) );
                    hETotal1D->SetBinContent(j, hETotal1D->GetBinContent(j) * ( E0 / (PUnits::GV/PUnits::m) ) );
                }

                if(pData->IsCyl()) {
                    hV2D->GetYaxis()->SetTitle("r [#mum]");
                    hETotal2D->GetYaxis()->SetTitle("r [#mum]");
                } else {
                    hV2D->GetYaxis()->SetTitle("y [#mum]");
                    hETotal2D->GetYaxis()->SetTitle("y [#mum]");

                }

                if(opt.Contains("comov")) {
                    hV2D->GetXaxis()->SetTitle("#zeta [#mum]");
                    hV1D->GetXaxis()->SetTitle("#zeta [#mum]");
                    hETotal2D->GetXaxis()->SetTitle("#zeta [#mum]");
                    hETotal1D->GetXaxis()->SetTitle("#zeta [#mum]");
                } else {
                    hV2D->GetXaxis()->SetTitle("z [#mum]");
                    hV2D->GetXaxis()->SetTitle("z [#mum]");
                    hETotal2D->GetXaxis()->SetTitle("z [#mum]");
                    hETotal1D->GetXaxis()->SetTitle("z [#mum]");
                }

                hV2D->GetZaxis()->SetTitle("#Psi-#Psi_{t} [MV]");
                hV1D->GetYaxis()->SetTitle("#Psi-#Psi_{t} [MV]");
                hETotal2D->GetZaxis()->SetTitle("E [GV/m]");
                hETotal1D->GetYaxis()->SetTitle("E [GV/m]");
            }
        }
    }


    // --------------------------------------------------- Vertical Zoom ------------

    Float_t yRange    = (hDen2D[0]->GetYaxis()->GetXmax() - hDen2D[0]->GetYaxis()->GetXmin())/zoom;
    Float_t midPoint = (hDen2D[0]->GetYaxis()->GetXmax() + hDen2D[0]->GetYaxis()->GetXmin())/2.;
    Float_t yMin = midPoint-yRange/2;
    Float_t yMax = midPoint+yRange/2;
    if(pData->IsCyl()) {
        yMin = pData->GetXMin(1);
        yMax = yRange;
    }

    for(Int_t i=0; i<Nspecies; i++) {
        if(!hDen2D[i]) continue;
        hDen2D[i]->GetYaxis()->SetRangeUser(yMin,yMax);
    }

    for(Int_t i=0; i<Nfields; i++) {
        if(!hE2D[i]) continue;
        hE2D[i]->GetYaxis()->SetRangeUser(yMin,yMax);
    }

    hETotal2D->GetYaxis()->SetRangeUser(yMin,yMax);

    Float_t xMin = hDen2D[0]->GetXaxis()->GetXmin();
    Float_t xMax = hDen2D[0]->GetXaxis()->GetXmax();
    Float_t xRange = xMax - xMin;

    // ------------- z Zoom --------------------------------- Plasma palette -----------
    // Set the range of the plasma charge density histogram for maximum constrast
    // using a dynamic palette wich adjust the nominal value to a certain color.


    Float_t density = 1;
    Float_t Base  = density;

    Float_t *Max = new Float_t[Nspecies];
    Float_t *Min = new Float_t[Nspecies];

    for(Int_t i=0; i<Nspecies; i++) {
        if(!hDen2D[i]) continue;

        Max[i] = hDen2D[i]->GetMaximum();
        Min[i] = 1.01E-1 * Base;
        if(i==1) Min[i] = 1.01E-1 * Base;
        if(i==2) Min[i] = 1.01E-4 * Base;
        hDen2D[i]->GetZaxis()->SetRangeUser(Min[i],Max[i]);
    }

    // Dynamic plasma palette
    const Int_t plasmaDNRGBs = 3;
    const Int_t plasmaDNCont = 64;
    Float_t basePos = 0.5;
    if(Max[0]!=Min[0]) {
        if(opt.Contains("logz")) {
            Float_t a = 1.0/(TMath::Log10(Max[0])-TMath::Log10(Min[0]));
            Float_t b = TMath::Log10(Min[0]);
            basePos = a*(TMath::Log10(Base) - b);

        } else {
            basePos = (1.0/(Max[0]-Min[0]))*(Base - Min[0]);
        }
    }

    Double_t plasmaDStops[plasmaDNRGBs] = { 0.00, basePos, 1.00 };
    Double_t plasmaDRed[plasmaDNRGBs]   = { 0.99, 0.90, 0.00 };
    Double_t plasmaDGreen[plasmaDNRGBs] = { 0.99, 0.90, 0.00 };
    Double_t plasmaDBlue[plasmaDNRGBs]  = { 0.99, 0.90, 0.00 };

    PPalette * plasmaPalette = (PPalette*) gROOT->FindObject("plasma");
    plasmaPalette->CreateGradientColorTable(plasmaDNRGBs, plasmaDStops,
                                            plasmaDRed, plasmaDGreen, plasmaDBlue, plasmaDNCont);
    // Change the range of z axis for the fields to be symmetric.
    Float_t *Emax = new Float_t[Nfields];
    Float_t *Emin = new Float_t[Nfields];
    for(Int_t i=0; i<Nfields; i++) {
        Emax[i] = hE2D[i]->GetMaximum();
        Emin[i] = hE2D[i]->GetMinimum();
        if(Emax[i] > TMath::Abs(Emin[i]))
            Emin[i] = -Emax[i];
        else
            Emax[i] = -Emin[i];
        hE2D[i]->GetZaxis()->SetRangeUser(Emin[i],Emax[i]);
    }


    // Potential
    if(opt.Contains("units")) {
        trapPotential *=  ( E0 * skindepth / (PUnits::MV) );
    }

    Float_t Vmin = hV1D->GetMinimum();
    {   // Shift potential
        Int_t   NbinsX = hV2D->GetNbinsX();
        Int_t   NbinsY = hV2D->GetNbinsY();
        for(Int_t j=0; j<NbinsX; j++) {
            for(Int_t k=0; k<NbinsY; k++) {
                hV2D->SetBinContent(j,k, hV2D->GetBinContent(j,k) - Vmin -trapPotential);
            }
            hV1D->SetBinContent(j, hV1D->GetBinContent(j) - Vmin -trapPotential);
        }
    }

    Vmin = hV1D->GetMinimum();
    Float_t Vmax = hV1D->GetMaximum();

    // Dynamic potential palette
    const Int_t potPNRGBs = 5;
    const Int_t potPNCont = 64;
    Float_t zeroPos = -Vmin/(Vmax-Vmin);

    Double_t potPStops[potPNRGBs] = { 0.00, zeroPos-3.0/potPNCont,zeroPos, zeroPos+3.0/potPNCont, 1.00 };
    Double_t potPRed[potPNRGBs]   = { 0.518, 0.965, 0.90, 0.498, 0.106 };
    Double_t potPGreen[potPNRGBs] = { 0.078, 0.925, 0.90, 0.718, 0.078 };
    Double_t potPBlue[potPNRGBs]  = { 0.106, 0.353, 0.90, 0.780, 0.518 };

    PPalette * potentialPalette = (PPalette*) gROOT->FindObject("rbow2inv");
    potentialPalette->CreateGradientColorTable(potPNRGBs, potPStops,
            potPRed, potPGreen, potPBlue, potPNCont);

    // Extract contours
    TCanvas* c = new TCanvas("c","Contour List",0,0,600,600);
    c->cd();

    // Potential
    TH2F *hV2Dc = (TH2F*) hV2D->Clone("hV2Dc");
    const Int_t Ncontours = 25;
    Double_t contours[Ncontours];
    for(Int_t i=0; i<Ncontours; i++) {
        contours[i] = i*(trapPotential/5.0) - trapPotential;
    }
    hV2Dc->SetContour(Ncontours, contours);
    hV2Dc->Draw("cont list");

    c->Update();
    TObjArray *contsV2D = (TObjArray*) gROOT->GetListOfSpecials()->FindObject("contours");
    TClonesArray graphsV2D("TGraph",Ncontours);
    {
        Int_t ncontours = contsV2D->GetSize();
        TList* clist = NULL;
        Int_t nGraphs = 0;
        TGraph *gr = NULL;
        for(Int_t i = 0; i < ncontours; i++) {
            if(i==0) continue;

            clist = (TList*) contsV2D->At(i);

            for(Int_t j = 0 ; j < clist->GetSize(); j++) {
                gr = (TGraph*) clist->At(j);
                if(!gr) continue;

                gr->SetLineWidth(1);
                gr->SetLineColor(kGray+1);

                if( !((i)%5) ) {
                    gr->SetLineWidth(2);
                    gr->SetLineColor(kGray+2);
                }
                new(graphsV2D[nGraphs]) TGraph(*gr) ;
                nGraphs++;
            }
        }
    }

    // Ion probability
    hIonProb2D->GetZaxis()->SetRangeUser(0.00501,80);

    TH2F *hIonProb2Dc = (TH2F*) hIonProb2D->Clone("hIonProb2Dc");
    const Int_t NcontI = 4;
    Double_t contI[NcontI] = {0.01,0.1,1.0,10.0};
    hIonProb2Dc->SetContour(NcontI, contI);
    hIonProb2Dc->Draw("cont list");

    c->Update();
    TObjArray *contsI2D = (TObjArray*) gROOT->GetListOfSpecials()->FindObject("contours");
    TClonesArray graphsI2D("TGraph",NcontI);
    {
        Int_t ncontours = contsI2D->GetSize();
        TList* clist = NULL;
        Int_t nGraphs = 0;
        TGraph *gr = NULL;
        for(Int_t i = 0; i < ncontours; i++) {
            clist = (TList*) contsI2D->At(i);

            for(Int_t j = 0 ; j < clist->GetSize(); j++) {
                gr = (TGraph*) clist->At(j);
                if(!gr) continue;

                if( !(i%2) ) {
                    gr->SetLineWidth(1);
                    gr->SetLineStyle(2);
                    gr->SetLineColor(kOrange-3);
                } else {
                    gr->SetLineWidth(1);
                    gr->SetLineStyle(1);
                    gr->SetLineColor(kOrange-3);
                }

                new(graphsI2D[nGraphs]) TGraph(*gr) ;
                nGraphs++;
            }
        }
    }




    // "Axis range" in Osiris units:
    Double_t ylow  = hDen2D[0]->GetYaxis()->GetBinLowEdge(FirstyBin);
    Double_t yup = hDen2D[0]->GetYaxis()->GetBinUpEdge(LastyBin);
    Double_t xmin = hDen2D[0]->GetXaxis()->GetXmin();
    Double_t xmax = hDen2D[0]->GetXaxis()->GetXmax();

    TLine *lineYzero = new TLine(xmin,0.0,xmax,0.0);
    lineYzero->SetLineColor(kGray+2);
    lineYzero->SetLineStyle(2);

    TLine *lineYup = new TLine(xmin,yup,xmax,yup);
    lineYup->SetLineColor(kGray+1);
    lineYup->SetLineStyle(2);

    TLine *lineYdown = new TLine(xmin,ylow,xmax,ylow);
    lineYdown->SetLineColor(kGray+1);
    lineYdown->SetLineStyle(2);

    zStartPlasma -= shiftz;
    zStartNeutral -= shiftz;
    zEndNeutral -= shiftz;

    if(opt.Contains("units")) {
        zStartPlasma *= skindepth / PUnits::um;
        zStartNeutral *= skindepth / PUnits::um;
        zEndNeutral *= skindepth / PUnits::um;
    }

    //  cout << "Start plasma = " << zStartPlasma << endl;
    TLine *lineStartPlasma = new TLine(zStartPlasma,yMin,zStartPlasma,yMax);
    lineStartPlasma->SetLineColor(kGray+2);
    lineStartPlasma->SetLineStyle(2);
    lineStartPlasma->SetLineWidth(3);

    //  cout << "Start plasma = " << zStartNeutral << endl;
    TLine *lineStartNeutral = new TLine(zStartNeutral,yMin,zStartNeutral,yMax);
    lineStartNeutral->SetLineColor(kGray+1);
    lineStartNeutral->SetLineStyle(2);
    lineStartNeutral->SetLineWidth(3);

    //  cout << "End plasma = " << zEndNeutral << endl;
    TLine *lineEndNeutral = new TLine(zEndNeutral,yMin,zEndNeutral,yMax);
    lineEndNeutral->SetLineColor(kGray+1);
    lineEndNeutral->SetLineStyle(2);
    lineEndNeutral->SetLineWidth(3);


    // Plotting
    // -----------------------------------------------

    // Canvas setup
    TCanvas *C = new TCanvas("C","2D Charge density and Electric field",750,666);

    // Palettes setup
    TExec *exPlasma = new TExec("exPlasma","plasmaPalette->cd();");
    TExec *exElec   = new TExec("exElec","redelectronPalette->cd();");
    TExec *exHot    = new TExec("exHot","hotPalette->cd();");
    TExec *exField  = new TExec("exField","rbow2Palette->cd();");
    TExec *exFieldT = new TExec("exFieldT","redPalette->cd();");
    TExec *exIonP   = new TExec("exIonP","redPalette->cd();");
    TExec *exPot    = new TExec("exPot","rbow2invPalette->cd();");

    // Actual Plotting!
    // ------------------------------------------------------------

    // Output file
    TString fOutName = Form("./%s/Plots/Potential2D/Potential2D",pData->GetPath().c_str());
    fOutName += Form("-%s_%i",pData->GetName(),time);

    // Setup Pad layout:
    Float_t lMargin = 0.15;
    Float_t rMargin = 0.18;
    Float_t bMargin = 0.15;
    Float_t tMargin = 0.04;
    Float_t factor = 1.0;
    PlasmaGlob::CanvasAsymPartition(C,2,lMargin,rMargin,bMargin,tMargin,factor);

    TPad *pad[2];
    TString sLabels[] = {"(a)","(b)"};
    // Text objects
    TPaveText **textLabel = new TPaveText*[2];

    C->cd(0);
    char pname[16];
    sprintf(pname,"pad_%i",1);
    pad[0] = (TPad*) gROOT->FindObject(pname);
    pad[0]->Draw();
    pad[0]->cd(); // <---------------------------------------------- Top Plot ---------
    if(opt.Contains("logz")) {
        pad[0]->SetLogz(1);
    } else {
        pad[0]->SetLogz(0);
    }
    pad[0]->SetFrameLineWidth(3);
    pad[0]->SetTickx(1);

    // Re-range:
    for(Int_t i=0; i<Nspecies; i++) {
        if(!hDen2D[i]) continue;
        hDen2D[i]->GetYaxis()->SetRangeUser(yMin -(factor-1)*yRange, yMax);
    }


    TH2F *hFrame = (TH2F*) gROOT->FindObject("hFrame1");
    if(hFrame) delete hFrame;
    hFrame = (TH2F*) hDen2D[0]->Clone("hFrame1");
    hFrame->Reset();

    hFrame->SetLabelFont(42,"xyz");
    hFrame->SetTitleFont(42,"xyz");

    hFrame->GetYaxis()->SetNdivisions(505);
    hFrame->GetYaxis()->SetLabelSize(0.085);
    hFrame->GetYaxis()->SetTitleSize(0.09);
    hFrame->GetYaxis()->SetTitleOffset(0.7);
    hFrame->GetYaxis()->SetTickLength(0.02);

    hFrame->GetXaxis()->SetLabelOffset(999.);
    hFrame->GetXaxis()->SetTitleOffset(999.);
    hFrame->GetXaxis()->SetTickLength(0.04);

    // Frame asymmetry:
    hFrame->Draw("col");

    // hDen2D[0]->GetZaxis()->SetNdivisions(505);

    // Injected electrons if any
    if(Nspecies>=3) {
        if(hDen2D[2]) {
            exHot->Draw();
            hDen2D[2]->Draw("colz same");
        }
    }

    // Plasma
    hDen2D[0]->GetZaxis()->SetTitleFont(42);
    exPlasma->Draw();
    hDen2D[0]->Draw("colz same");

    // Beam driver.
    if(hDen2D[1]) {
        //    hDen2D[1]->GetZaxis()->SetNdivisions(505);
        exElec->Draw();
        hDen2D[1]->Draw("colz same");
    }

    {
        TGraph *gr = (TGraph*) graphsV2D.At(4);
        gr->Draw("C");
    }

    {
        TGraph *gr = (TGraph*) graphsI2D.At(1);
        gr->Draw("C");
    }


    if(opt.Contains("1dline")) {
        lineYzero->Draw();
        lineYdown->Draw();
        lineYup->Draw();
    }

    if(opt.Contains("sline")) {
        if(zStartPlasma>xmin && zStartPlasma<xmax)
            lineStartPlasma->Draw();
        if(zStartNeutral>xmin && zStartNeutral<xmax)
            lineStartNeutral->Draw();
        if(zEndNeutral>xmin && zEndNeutral<xmax)
            lineEndNeutral->Draw();
    }

    // lineYdown->Draw();
    // lineYup->Draw();

    // Palettes re-arrangement
    pad[0]->Update();
    Float_t y1 = pad[0]->GetBottomMargin();
    Float_t y2 = 1 - pad[0]->GetTopMargin();
    Float_t x1 = pad[0]->GetLeftMargin();
    Float_t x2 = 1 - pad[0]->GetRightMargin();

    TPaletteAxis *palette = NULL;
    if(Nspecies>=3) {
        if(hDen2D[2]) {
            palette = (TPaletteAxis*)hDen2D[2]->GetListOfFunctions()->FindObject("palette");
        }
    }
    if(palette) {
        palette->SetY2NDC(y2 - 0.00);
        palette->SetY1NDC(0.66*(y1+y2) + 0.00);
        palette->SetX1NDC(x2 + 0.005);
        palette->SetX2NDC(x2 + 0.03);
        //  palette->SetTitleFont(42);
        //  palette->SetTitleOffset(0.85);
        palette->SetTitleOffset(999.9);
        palette->SetTitleSize(0.075);
        palette->SetLabelFont(42);
        palette->SetLabelSize(0.075);
        palette->SetLabelOffset(0.001);
        palette->SetBorderSize(2);
        palette->SetLineColor(1);
    }

    palette = (TPaletteAxis*)hDen2D[0]->GetListOfFunctions()->FindObject("palette");
    if(palette) {
        palette->SetY2NDC(0.66*(y1+y2) - 0.00);
        palette->SetY1NDC(0.33*(y1+y2) + 0.00);
        palette->SetX1NDC(x2 + 0.005);
        palette->SetX2NDC(x2 + 0.03);
        // palette->SetTitleFont(42);
        palette->SetTitleOffset(0.80);
        palette->SetTitleSize(0.075);
        palette->SetLabelFont(42);
        palette->SetLabelSize(0.075);
        palette->SetLabelOffset(0.001);
        palette->SetBorderSize(2);
        palette->SetLineColor(1);
    }

    palette = (TPaletteAxis*)hDen2D[1]->GetListOfFunctions()->FindObject("palette");
    if(palette) {
        palette->SetY2NDC(0.33*(y1+y2) - 0.00);
        palette->SetY1NDC(y1 + 0.00);
        palette->SetX1NDC(x2 + 0.005);
        palette->SetX2NDC(x2 + 0.03);
        //palette->SetTitleFont(42);
        //palette->SetTitleOffset(0.85);
        palette->SetTitleOffset(999.9);
        palette->SetTitleSize(0.075);
        palette->SetLabelFont(42);
        palette->SetLabelSize(0.075);
        palette->SetLabelOffset(0.001);
        palette->SetBorderSize(2);
        palette->SetLineColor(1);
    }


    // 1D charge density plots:
    Float_t yaxismin  =  pad[0]->GetUymin();
    Float_t yaxismax  =  pad[0]->GetUymin() + 0.33*(pad[0]->GetUymax() - pad[0]->GetUymin()) - 0.00;
    Float_t denmin = Min[1];
    Float_t denmax = Max[1];
    if(opt.Contains("logz")) {
        denmin = TMath::Log10(denmin);
        denmax = TMath::Log10(denmax);
    }

    Float_t curmin = 0.0;
    Float_t curmax = 0.0;
    if(opt.Contains("curr")) {
        curmin = 0.0;
        curmax = hCur1D[1]->GetMaximum();

        cout << Form(" Maximum driver  current = %6.2f kA ", curmax) << endl ;
        if(Nspecies>=3)
            if(hCur1D[2])
                cout << Form(" Maximum witness current = %6.2f kA ", hCur1D[2]->GetMaximum()) << endl ;

        // Round for better plotting
        curmax = 0.1*TMath::Nint(curmax*10);
    }

    for(Int_t i=0; i<Nspecies; i++) {
        if(!hDen1D[i]) continue;

        Float_t slope = (yaxismax - yaxismin)/(denmax - denmin);

        for(Int_t j=0; j<hDen1D[i]->GetNbinsX(); j++) {
            Float_t content = hDen1D[i]->GetBinContent(j+1);
            if(opt.Contains("logz")) content = TMath::Log10(content);

            if(content<denmin)
                hDen1D[i]->SetBinContent(j+1,yaxismin);
            else
                hDen1D[i]->SetBinContent(j+1,(content - denmin) * slope + yaxismin);


        }

        if(hCur1D[i]) {
            slope = (yaxismax - yaxismin)/(curmax - curmin);

            for(Int_t j=0; j<hCur1D[i]->GetNbinsX(); j++) {
                Float_t content = hCur1D[i]->GetBinContent(j+1);

                if(content<curmin)
                    hCur1D[i]->SetBinContent(j+1,yaxismin);
                else
                    hCur1D[i]->SetBinContent(j+1,(content - curmin) * slope + yaxismin);
            }

        }

    }

    // Plasma on-axis density:
    // hDen1D[0]->SetLineWidth(2);
    // hDen1D[0]->SetLineColor(kGray+1);
    // // // PlasmaGlob::SetH1Style(hDen1D[0],1);
    // hDen1D[0]->Draw("same C");


    if(opt.Contains("curr")) {
        hCur1D[1]->SetLineWidth(2);
        hCur1D[1]->SetLineColor(PlasmaGlob::elecLine);
        hCur1D[1]->Draw("same C");
    } else {
        hDen1D[1]->SetLineWidth(2);
        hDen1D[1]->SetLineColor(PlasmaGlob::elecLine);
        //    hDen1D[1]->Draw("same C");
    }

    if(Nspecies>=3) {
        if(hDen1D[2]) {
            if(opt.Contains("curr")) {
                hCur1D[2]->SetLineWidth(2);
                hCur1D[2]->SetLineColor(kOrange+8);
                hCur1D[2]->Draw("same C");
            } else {
                hDen1D[2]->SetLineWidth(2);
                hDen1D[2]->SetLineColor(kOrange+8);
                //   hDen1D[2]->Draw("same C");
            }
        }
    }

    // Current axis
    TGaxis *axis = NULL;
    if(opt.Contains("curr")) {
        axis = new TGaxis(xMax-xRange/6.0,yMin - (factor-1)*yRange,
                          xMax-xRange/6.0,yaxismax,
                          0.001,curmax,503,"+LS");

        axis->SetLineWidth(1);
        axis->SetLineColor(kGray+3);//PlasmaGlob::elecLine);
        axis->SetLabelColor(kGray+3);//PlasmaGlob::elecLine);
        axis->SetLabelSize(0.06);
        axis->SetLabelOffset(0.01);
        axis->SetLabelFont(42);
        axis->SetTitleColor(kGray+3);//PlasmaGlob::elecLine);
        axis->SetTitleSize(0.06);
        axis->SetTitleOffset(0.6);
        axis->SetTitleFont(42);
        axis->SetTickSize(0.03);
        axis->SetTitle("I [kA]");
        axis->CenterTitle();
        axis->SetNdivisions(505);

        axis->Draw();
    }


    TPaveText *textTime = new TPaveText(xMax - 0.3*xRange, yMax-0.15*yRange, xMax-0.1, yMax-0.05*yRange);
    //x2-0.17,y2-0.12,x2-0.02,y2-0.02,"NDC");
    PlasmaGlob::SetPaveTextStyle(textTime,32);
    char ctext[128];
    if(opt.Contains("units") && n0)
        sprintf(ctext,"z = %5.1f #mum", Time * skindepth / PUnits::um);
    else
        sprintf(ctext,"t = %5.1f #omega_{p}^{-1}",Time);
    textTime->SetTextFont(42);
    textTime->AddText(ctext);

    textTime->Draw();
    // textDen->Draw();
    // if(opt.Contains("units"))
    //   textWav->Draw();

    textLabel[0] = new TPaveText(xMin + 0.02*xRange, yMax-0.2*yRange, xMin+0.30*xRange, yMax-0.05*yRange);
    PlasmaGlob::SetPaveTextStyle(textLabel[0],12);
    textLabel[0]->SetTextFont(42);
    textLabel[0]->AddText(sLabels[0]);
    textLabel[0]->Draw();


    pad[0]->RedrawAxis();

    C->cd(0);
    sprintf(pname,"pad_%i",0);
    pad[1] = (TPad*) gROOT->FindObject(pname);
    pad[1]->Draw();
    pad[1]->cd(); // <--------------------------------------------------------- Bottom Plot
    pad[1]->SetFrameLineWidth(3);
    pad[1]->SetTickx(1);

    hFrame = (TH2F*) gROOT->FindObject("hFrame2");
    if(hFrame) delete hFrame;
    hFrame = (TH2F*) hDen2D[0]->Clone("hFrame2");
    hFrame->Reset();

    Float_t yFactor = pad[0]->GetAbsHNDC()/pad[1]->GetAbsHNDC();


    hFrame->GetYaxis()->SetLabelSize(yFactor*0.085);
    hFrame->GetYaxis()->SetTitleSize(yFactor*0.09);
    hFrame->GetYaxis()->SetTitleOffset(0.7/yFactor);
    hFrame->GetYaxis()->SetTickLength(0.02/yFactor);

    hFrame->GetXaxis()->SetTitleSize(0.10);
    hFrame->GetXaxis()->SetLabelSize(0.08);
    hFrame->GetXaxis()->SetLabelOffset(0.02);
    hFrame->GetXaxis()->SetTitleOffset(1.0);
    hFrame->GetXaxis()->SetTickLength(0.04*yFactor);

    hFrame->SetLabelFont(42,"xyz");
    hFrame->SetTitleFont(42,"xyz");

    hFrame->Draw("col");

    //  hE2D[0]->GetZaxis()->SetNdivisions(505);
    hV2D->GetZaxis()->SetTitleFont(42);
    hV2D->GetZaxis()->SetTickLength(0.02/yFactor);


    exPot->Draw();

    hV2D->Draw("col z same");

    for(Int_t i=0; i<graphsV2D.GetEntriesFast(); i++) {
        TGraph *gr = (TGraph*) graphsV2D.At(i);
        if(!gr) continue;
        gr->Draw("C");
    }

    for(Int_t i=0; i<graphsI2D.GetEntriesFast(); i++) {
        //if(i!=2) continue;
        TGraph *gr = (TGraph*) graphsI2D.At(i);
        if(!gr) continue;
        gr->Draw("C");
    }


    if(opt.Contains("1dline")) {
        lineYzero->Draw();
        lineYdown->Draw();
        lineYup->Draw();
    }

    if(opt.Contains("sline")) {
        if(zStartPlasma>xmin && zStartPlasma<xmax)
            lineStartPlasma->Draw();
        if(zStartNeutral>xmin && zStartNeutral<xmax)
            lineStartNeutral->Draw();
        if(zEndNeutral>xmin && zEndNeutral<xmax)
            lineEndNeutral->Draw();
    }


    pad[1]->Update();

    y1 = pad[1]->GetBottomMargin();
    y2 = 1 - pad[1]->GetTopMargin();
    x1 = pad[1]->GetLeftMargin();
    x2 = 1 - pad[1]->GetRightMargin();

    palette = (TPaletteAxis*)hV2D->GetListOfFunctions()->FindObject("palette");
    if(palette) {
        palette->SetY2NDC(y2 - 0.00);
        palette->SetY1NDC(y1 + 0.00);
        palette->SetX1NDC(x2 + 0.005);
        palette->SetX2NDC(x2 + 0.03);
        // palette->SetTitleFont(42);
        palette->SetTitleSize(yFactor*0.075);
        palette->SetTitleOffset(0.80/yFactor);
        palette->SetLabelSize(yFactor*0.075);
        palette->SetLabelFont(42);
        palette->SetLabelOffset(0.01/yFactor);
        palette->SetBorderSize(2);
        palette->SetLineColor(1);
    }



    pad[1]->RedrawAxis();

    textLabel[1] = new TPaveText(xMin + 0.02*xRange, yMax-0.2*yRange, xMin+0.30*xRange, yMax-0.05*yRange);
    PlasmaGlob::SetPaveTextStyle(textLabel[1],12);
    textLabel[1]->SetTextFont(42);
    textLabel[1]->AddText(sLabels[1]);
    textLabel[1]->Draw();

    C->cd();

    // Print to a file
    PlasmaGlob::imgconv(C,fOutName,opt);
    // ---------------------------------------------------------

    PlasmaGlob::DestroyCanvases();
}
Exemple #6
0
void PlotField2D( const TString &sim, Int_t time, Int_t index = 0, Float_t zoom=2, Int_t Nbins=2, const TString &options="") {
    
#ifdef __CINT__  
  gSystem->Load("libptools.so");
#endif

  // Load PData
  PData *pData = PData::Get(sim.Data());
  pData->LoadFileNames(time);
  if(!pData->IsInit()) return;

  // Refresh and Style
  PGlobals::Initialize();

  // Coloured palettes
  gROOT->Macro("PPalettes.C");

  TString opt = options;
 
  // More makeup
  if(opt.Contains("grid")) {
    gStyle->SetPadGridX(1);
    gStyle->SetPadGridY(1);
  }
 
  gStyle->SetTitleFont(42);
  gStyle->SetStatFont(42);
  gStyle->SetTextFont(42);
  gStyle->SetTitleFont(42,"xyz");
  gStyle->SetLabelFont(42,"xyz");

  // Some plasma constants
  Double_t n0 = pData->GetPlasmaDensity();
  Double_t kp = pData->GetPlasmaK();
  Double_t skindepth = 1.;
  if(kp!=0.0) skindepth = 1/kp;
  Double_t E0 = pData->GetPlasmaE0();

  // Some beam properties:
  Float_t Ebeam = pData->GetBeamEnergy() * PUnits::MeV;
  Float_t gamma = Ebeam / PConst::ElectronMassE;
  Float_t vbeam = TMath::Sqrt(1 - 1/(gamma*gamma));
  // cout << Form(" - Bunch gamma      = %8.4f", gamma ) << endl;
  // cout << Form(" - Bunch velocity   = %8.4f c", vbeam ) << endl;
  Float_t nb = pData->GetBeamDensity();
  
  // Time in OU
  Float_t Time = pData->GetRealTime();
  // z start of the plasma in normalized units.
  Float_t zStartPlasma = pData->GetPlasmaStart()*kp;
  // z start of the beam in normalized units.
  Float_t zStartBeam = pData->GetBeamStart()*kp;
  // z start of the neutral in normalized units.
  Float_t zStartNeutral = pData->GetNeutralStart()*kp;
  // z end of the neutral in normalized units.
  Float_t zEndNeutral = pData->GetNeutralEnd()*kp;
  
  if(opt.Contains("center")) {
    Time -= zStartPlasma;
    if(opt.Contains("comov"))      // Centers on the head of the beam.
      Time += zStartBeam;
  }
  Float_t shiftz = pData->Shift(opt);
  //  cout << "Shift = " << shiftz << endl;
  
  // Calculate the "axis range" in number of bins. If Nbins==0 a RMS width is taken.
  Double_t rms0 = pData->GetBeamRmsY() * kp;
  if(pData->IsCyl())  rms0  = pData->GetBeamRmsR() * kp;
  
  Int_t FirstyBin = 0;
  Int_t LastyBin = 0;
  if(Nbins==0) { 
    Nbins =  TMath::Nint(rms0 / pData->GetDX(1));
  }
  
  // Slice width limits.
  if(!pData->IsCyl()) {
    FirstyBin = pData->GetNX(1)/2 + 1 - Nbins;
    LastyBin =  pData->GetNX(1)/2 + Nbins;
  } else {
    FirstyBin = 1; 
    LastyBin  = Nbins;
  }
  // -------------------------------------------------------------------------------

  // Get field histos
  const Int_t Nfields = 3;
  TH2F **hE2D = new TH2F*[Nfields];
  for(Int_t i=0;i<Nfields;i++) {
    hE2D[i] = NULL;

    if(i!=index) continue;

    if(!pData->GetEfieldFileName(i))
      continue;
    
    char hName[24];
    sprintf(hName,"hE2D_%i",i);
    hE2D[i] = (TH2F*) gROOT->FindObject(hName);
    if(hE2D[i]) delete hE2D[i];
    
    if(!pData->Is3D())
      hE2D[i] = pData->GetEField(i,opt);
    else
      hE2D[i] = pData->GetEField2DSliceZY(i,-1,Nbins,opt+"avg");
    
    hE2D[i]->SetName(hName);   
    hE2D[i]->GetXaxis()->CenterTitle();
    hE2D[i]->GetYaxis()->CenterTitle();
    hE2D[i]->GetZaxis()->CenterTitle();
    if(opt.Contains("comov"))
      hE2D[i]->GetXaxis()->SetTitle("k_{p} #zeta");
    else
      hE2D[i]->GetXaxis()->SetTitle("k_{p} z");
    
    if(pData->IsCyl()) 
      hE2D[i]->GetYaxis()->SetTitle("k_{p} r");
    else
      hE2D[i]->GetYaxis()->SetTitle("k_{p} x");
    
    if(i==0)
      hE2D[i]->GetZaxis()->SetTitle("E_{z} / E_{0}");
    else if(i==1)
      hE2D[i]->GetZaxis()->SetTitle("E_{y} / E_{0}");
    else if(i==2)
      hE2D[i]->GetZaxis()->SetTitle("E_{x} / E_{0}");
  }


  // Tunning the Histograms
  // ---------------------

  // Chaning to user units: 
  // --------------------------
  
  if(opt.Contains("units") && n0) {
    
    for(Int_t i=0;i<Nfields;i++) {
      
      if(i!=index) continue;

      Int_t NbinsX = hE2D[i]->GetNbinsX();
      Float_t xMin = skindepth * hE2D[i]->GetXaxis()->GetXmin() / PUnits::um;
      Float_t xMax = skindepth * hE2D[i]->GetXaxis()->GetXmax() / PUnits::um;
      Int_t NbinsY = hE2D[i]->GetNbinsY();
      Float_t yMin = skindepth * hE2D[i]->GetYaxis()->GetXmin() / PUnits::um;
      Float_t yMax = skindepth * hE2D[i]->GetYaxis()->GetXmax() / PUnits::um;
      hE2D[i]->SetBins(NbinsX,xMin,xMax,NbinsY,yMin,yMax);
      for(Int_t j=0;j<hE2D[i]->GetNbinsX();j++) {
	for(Int_t k=0;k<hE2D[i]->GetNbinsY();k++) {
	  hE2D[i]->SetBinContent(j,k, hE2D[i]->GetBinContent(j,k) * ( E0 / (PUnits::GV/PUnits::m) ) );
	}
      }

      if(pData->IsCyl())
	hE2D[i]->GetYaxis()->SetTitle("r [#mum]");      
      else
	hE2D[i]->GetYaxis()->SetTitle("x [#mum]");      

      if(opt.Contains("comov"))
	hE2D[i]->GetXaxis()->SetTitle("#zeta [#mum]");
      else
	hE2D[i]->GetXaxis()->SetTitle("z [#mum]");
      

      if(i==0)
	hE2D[i]->GetZaxis()->SetTitle("E_{z} [GV/m]");
      else if(i==1)
	hE2D[i]->GetZaxis()->SetTitle("E_{y} [GV/m]");
      else if(i==2)
	hE2D[i]->GetZaxis()->SetTitle("E_{x} [GV/m]");
    }

  }

  // --------------------------------------------------- Vertical Zoom ------------
  
  Float_t yRange    = (hE2D[index]->GetYaxis()->GetXmax() - hE2D[index]->GetYaxis()->GetXmin())/zoom;
  Float_t midPoint = (hE2D[index]->GetYaxis()->GetXmax() + hE2D[index]->GetYaxis()->GetXmin())/2.;
  Float_t yMin = midPoint-yRange/2;
  Float_t yMax = midPoint+yRange/2;
  if(pData->IsCyl()) {
    yMin = hE2D[index]->GetYaxis()->GetXmin();
    yMax = yRange;
  }
  
  hE2D[index]->GetYaxis()->SetRangeUser(yMin,yMax);

  Float_t xMin = hE2D[index]->GetXaxis()->GetXmin();
  Float_t xMax = hE2D[index]->GetXaxis()->GetXmax();
  Float_t xRange = xMax - xMin;
 

  // Change the range of z axis for the fields to be symmetric.
  Float_t Emax = hE2D[index]->GetMaximum();
  Float_t Emin = hE2D[index]->GetMinimum();
  if(Emax > TMath::Abs(Emin))
    Emin = -Emax;
  else
    Emax = -Emin;
  hE2D[index]->GetZaxis()->SetRangeUser(Emin,Emax); 

  
  PPalette * plasmaPalette = (PPalette*) gROOT->FindObject("rbowwhite");
  plasmaPalette->cd();

  // Plotting
  // -----------------------------------------------

  // Canvas setup
  TCanvas *C = new TCanvas("C","2D electric",800,500);
  
  // Actual Plotting!
  // ------------------------------------------------------------

  // Output file
  TString fname;
  if(index<3) fname = Form("E%i",index+1);
  TString fOutName = Form("./%s/Plots/Field2D/Field2D-%s",sim.Data(),fname.Data());
  fOutName += Form("-%s_%i",sim.Data(),time);
  
  C->cd();

  // Setup Pad layout:
  Float_t lMargin = 0.15;
  Float_t rMargin = 0.18;
  Float_t bMargin = 0.20;
  Float_t tMargin = 0.06;
  gPad->SetLeftMargin(lMargin);
  gPad->SetRightMargin(rMargin);
  gPad->SetBottomMargin(bMargin);
  gPad->SetTopMargin(tMargin);
  
  if(opt.Contains("logz")) {
    gPad->SetLogz(1);
  } else {
    gPad->SetLogz(0);
  }
  gPad->SetFrameLineWidth(2);  

  // Define the frames for plotting
  Int_t fonttype = 43;
  Int_t fontsize = 28;
  Int_t tfontsize = 30;
  Float_t txoffset = 1.3;
  Float_t lxoffset = 0.02;
  Float_t tyoffset = 1.0;
  Float_t lyoffset = 0.01;
  Float_t tylength = 0.02;
  Float_t txlength = 0.04;

  TH2F *hFrame = (TH2F*) gROOT->FindObject("hFrame");
  if(hFrame) delete hFrame;
  hFrame = (TH2F*) hE2D[index]->Clone("hFrame");
  hFrame->Reset();

  // Format for y axis
  hFrame->GetYaxis()->SetTitleFont(fonttype);
  hFrame->GetYaxis()->SetTitleSize(tfontsize);
  hFrame->GetYaxis()->SetTitleOffset(tyoffset);
  hFrame->GetYaxis()->SetLabelFont(fonttype);
  hFrame->GetYaxis()->SetLabelSize(fontsize);
  hFrame->GetYaxis()->SetLabelOffset(lyoffset);

  hFrame->GetYaxis()->SetTickLength(tylength);

  // Format for x axis
  hFrame->GetXaxis()->SetTitleFont(fonttype);
  hFrame->GetXaxis()->SetTitleSize(tfontsize+2);
  hFrame->GetXaxis()->SetTitleOffset(txoffset);
  hFrame->GetXaxis()->SetLabelFont(fonttype);
  hFrame->GetXaxis()->SetLabelSize(fontsize+2);
  hFrame->GetXaxis()->SetLabelOffset(lxoffset);
    
  hFrame->GetXaxis()->SetTickLength(txlength);      

  hFrame->Draw("col");            
 
  //  hE2D[index]->GetZaxis()->SetNdivisions(505);  
  hE2D[index]->GetZaxis()->SetTitleFont(fonttype);
  hE2D[index]->Draw("colz same");
    
  // Re-touchs
  gPad->Update();

  Float_t y1 = gPad->GetBottomMargin();
  Float_t y2 = 1 - gPad->GetTopMargin();
  Float_t x1 = gPad->GetLeftMargin();
  Float_t x2 = 1 - gPad->GetRightMargin();
  Float_t gap = 0.005;  

  TPaletteAxis *palette = (TPaletteAxis*)hE2D[index]->GetListOfFunctions()->FindObject("palette");
  if(palette) {
    palette->SetY2NDC(y2 - gap);
    palette->SetY1NDC(y1 + gap);
    palette->SetX1NDC(x2 + 0.005);
    palette->SetX2NDC(x2 + 0.03);
    palette->SetTitleOffset(tyoffset);
    palette->SetTitleSize(tfontsize);
    palette->SetLabelFont(fonttype);
    palette->SetLabelSize(fontsize);
    if(opt.Contains("logz")) 
      palette->SetLabelOffset(0);
    else
      palette->SetLabelOffset(lyoffset);
    palette->SetBorderSize(2);
    palette->SetLineColor(1);
  }
 
  
  // Text objects
  TPaveText *textTime = new TPaveText(xMax - 0.30*xRange, yMax-0.10*yRange, xMax-0.02*xRange, yMax-0.02*yRange);
  PGlobals::SetPaveTextStyle(textTime,32); 
  char ctext[128];
  if(opt.Contains("units") && n0) 
    sprintf(ctext,"z = %5.1f #mum", Time * skindepth / PUnits::um);
  else
    sprintf(ctext,"#omega_{p} t = %5.1f",Time);
  textTime->AddText(ctext);
  
  TPaveText *textDen = new TPaveText(xMin + 0.02*xRange, yMax-0.10*yRange, xMin + 0.40*xRange, yMax-0.02*yRange);
  PGlobals::SetPaveTextStyle(textDen,12); 
  textDen->SetTextColor(kOrange+10);
  if(opt.Contains("units") && n0) {
    sprintf(ctext,"n_{0} = %5.2f x 10^{17} / cm^{3}", 1e-17 * n0 * PUnits::cm3);
    textDen->AddText(ctext);
    textDen->Draw();
  }
  
  textTime->Draw();
  
  gPad->RedrawAxis(); 

  C->cd();

  // Print to a file
  PGlobals::imgconv(C,fOutName,opt);
  // ---------------------------------------------------------

}