How might i draw a histogram consisting of different color dots

Hi All,

How might i draw a histogram consisting of different color dots ?

the code I work on is below;

 
 		   if (db==0){
 
                     HistGain[hv]->Fill((qie+db*24),GAIN);
                     HistGain[hv]->GetXaxis()->SetTitle("Channel");
                     HistGain[hv]->GetYaxis()->SetTitle("Gain");
                     HistGain[hv]->SetMarkerStyle(8);
                     HistGain[hv]->SetMarkerColor(kRed);
                     }

                     else{
                      HistGain[hv]->Fill((qie+db*24),GAIN);
                      HistGain[hv]->GetXaxis()->SetTitle("Channel");
                      HistGain[hv]->GetYaxis()->SetTitle("Gain");
                      HistGain[hv]->SetMarkerStyle(8);
                      HistGain[hv]->SetMarkerColor(kBlue);
                      }

What i want to do here is that the first 24 dots are red and the last 24 dots are blue. I did whatever i change on the code, ROOT still perceived the last one ,being SetMarkerColor(kBlue)…

What am i supposed to do for drawing histogram with different color dots ?

Cheers,
Thanks for your help,
Ersel

You probably need two histograms (one for “db==0” and another one for “db!=0”).

Hi Pepe,

No, I want to make one histogram consisting of 48 bins.

for db==0 , the first 24 bins will be filled with SetMarkerColor(kRed)
for db==1, the last 24 bins will be filled with SetMarkerColor(kBlue)

Cheers,
Ersel

THistPainter

I already got there SetMarketColor(),
Must be there anything else ?

Cheers,
Ersel

Ok I try to merge two in one histogram ? Why don’t I merge them :mrgreen: ?

           	     HistGain_0[hv]->Fill((qie+0*24),GAIN);
                     HistGain_0[hv]->GetXaxis()->SetTitle("Channel");
                     HistGain_0[hv]->GetYaxis()->SetTitle("Gain");
                     HistGain_0[hv]->SetMarkerStyle(8);
                     HistGain_0[hv]->SetMarkerColor(kBlue);

                     HistGain_1[hv]->Fill((qie+1*24),GAIN);
                     HistGain_1[hv]->GetXaxis()->SetTitle("Channel");
                     HistGain_1[hv]->GetYaxis()->SetTitle("Gain");
                     HistGain_1[hv]->SetMarkerStyle(8);
                     HistGain_1[hv]->SetMarkerColor(kRed);
                     
                     HistGain_0[hv]->Draw();
                     HistGain_1[hv]->Draw("same");

Use a “THStack” and then draw it using the “NOSTACK” option.

No, It didn’t work. I also try to run THStack.C ,it didn’t run as well, that is, it did not draw a histogram on a canvas.

THStack hs("hs","test stacked histograms");

[code]
HistGain_0[hv]->Fill((qie+0*24),GAIN);
HistGain_0[hv]->GetXaxis()->SetTitle(“Channel”);
HistGain_0[hv]->GetYaxis()->SetTitle(“Gain”);
HistGain_0[hv]->SetMarkerStyle(8);
HistGain_0[hv]->SetMarkerColor(kBlue);
hs.Add(HistGain_0[hv]);

                 HistGain_1[hv]->Fill((qie+1*24),GAIN);
                 HistGain_1[hv]->GetXaxis()->SetTitle("Channel");
                 HistGain_1[hv]->GetYaxis()->SetTitle("Gain");
                 HistGain_1[hv]->SetMarkerStyle(8);
                 HistGain_1[hv]->SetMarkerColor(kRed);
                 hs.Add(HistGain_1[hv]);         
                 
                 hs.Draw();                                                                                                                                                               
                 hs.Draw("nostack");                                                                                                                          [/code]

hs.Draw(“NOSTACK P”);

Yeah, I did it, but nothing changed

{ TH1F *h0 = new TH1F("h0", "h0", 50, -5, 5); h0->SetMarkerStyle(kFullTriangleUp); h0->SetMarkerColor(kBlue); h0->FillRandom("gaus"); TH1F *h1 = new TH1F("h1", "h1", 100, -4, 16); h1->SetMarkerStyle(kFullTriangleDown); h1->SetMarkerColor(kRed); h1->FillRandom("landau"); THStack *hs = new THStack("hs", "My Fantastic Results;Huh ?;WTF ?"); hs->Add(h0); hs->Add(h1); hs->Draw("NOSTACK P"); }

Ok done, thanks Pepe :mrgreen: But There is a problem in my script…

What is wrong with my script ?

I use THStack for colouring two histograms in one histogram.

While THStack draws a histogram by doing a superimposed one histogram for hv==0 && db==0 and hv==1 && db==0 and they are blue colours, outfile.root perceives just hv==0 && db==1 and hv==1 && db==1 and they are red colours. So, there is no hv==0 && db==0 and hv==1 && db==0, being in THStack histogram, in outfile.root !

Problem is that THStack and *_outfile.root didn’t perceive all of data(48bins) in one histogram.Why ?

I want to be coloured different colours with respect to the first 24 data and the last 24 ones !
Could you,please, help me for this problem, I appreciate you .

PS:
My script runs as root -b -q ‘Analysis.C(3104)’
map.txt and an example of rootfile is in the attachment!
My script is :

#include <iostream>
#include <fstream>

using namespace std;

char rootfile[200];
int qie,depth,ieta;
int runno;

void Analysis(runno){

  sprintf(rootfile,"Run_%d.root",runno);
  TFile *f = new TFile(rootfile);

  stringstream outfile;
  outfile<<runno<<"_outfile.root";
  TFile *f1 = new TFile(outfile.str().c_str(),"RECREATE");
  outfile.str("");


  int HV[] = {650,700};

  //  TH2F *HistGain[2];                                                                                                                                                           
  TH2F *HistGain_0[2];
  TH2F *HistGain_1[2];

  THStack *hs = new THStack ("hs","db0 and db1 in one histogram");
  stringstream voltage;
  for(const int hv=0;hv<2;hv++) {
      voltage<<HV[hv]<<"GainVsChannel";
      HistGain_0[hv] = new TH2F(voltage.str().c_str(),voltage.str().c_str(),55,0,50,1000,0,2700000);
      HistGain_1[hv] = new TH2F(voltage.str().c_str(),voltage.str().c_str(),55,0,50,1000,0,2700000);
      //    HistGain[hv]= new TH2F(voltage.str().c_str(),voltage.str().c_str(),48,0,50,1000,0,2700000);                                                                            
      voltage.str("");
  }


  ifstream input("map.txt");

  if (input.fail()){
    cout<<"map.txt is not available"<<endl;
  }
  int count = 0;
  while( input>>qie>>ieta>>depth ){
          for(int hv=0;hv<2;hv++){
  for(int db=0;db<2;db++) {

          //        count++;                                                                                                                                                       
          // cout<<count;                                                                                                                                                          

          TString histnameLED,histnamePED;
          stringstream ledname,pedname;
      histnameLED.Form("/Analyzer/QIEsSumLED%d_QiECh_%d_DBOX_%d_eta_%d_D_%d",HV[hv],qie,db,ieta,depth);
      histnamePED.Form("/Analyzer/QIEsSumPED%d_QiECh_%d_DBOX_%d_eta_%d_D_%d",HV[hv],qie,db,ieta,depth);
      pedname<<histnamePED;
      ledname<<histnameLED;

      TH1F *HistLED = (TH1F*)f->Get(ledname.str().c_str());
      TH1F *HistPED = (TH1F*)f->Get(pedname.str().c_str());
      ledname.str("");
      pedname.str("");
      //      cout<<ledname.str().c_str()<<endl;                                                                                                                                   

           if(HistLED && HistPED) {

             LEDMEAN = HistLED->GetMean();
             PEDMEAN = HistPED->GetMean();
             LEDRMS = HistLED->GetRMS();
             PEDRMS = HistPED->GetRMS();


           double TRUEMEAN = LEDMEAN - PEDMEAN;
           double TRUERMS = sqrt(pow(LEDRMS,2)+pow(PEDRMS,2));
           double NPE = 1.15*pow(TRUEMEAN,2)/pow(TRUERMS,2);
           double GAIN = TRUEMEAN*2.6*10000/NPE/1.6;

           cout<<GAIN<<endl;

                   if (GAIN>0) {

                     if (db==0) {

                     HistGain_0[hv]->Fill(qie+(db*24),GAIN);
                     HistGain_0[hv]->GetXaxis()->SetTitle("Channel");
                     HistGain_0[hv]->GetYaxis()->SetTitle("Gain");
                     HistGain_0[hv]->SetMarkerStyle(8);
                     HistGain_0[hv]->SetMarkerColor(kBlue);
                     hs->Add(HistGain_0[hv]);  }

                     if (db==1) {

                     HistGain_1[hv]->Fill(qie+(db*24),GAIN);
                     HistGain_1[hv]->GetXaxis()->SetTitle("Channel");
                     HistGain_1[hv]->GetYaxis()->SetTitle("Gain");
                     HistGain_1[hv]->SetMarkerStyle(8);
                     HistGain_1[hv]->SetMarkerColor(kRed);       
                     hs->Add(HistGain_1[hv]);   }


                   }
           }
        }
          }
          hs->Draw("NOSTACK P");

  }


  f1->Write();
  input.close();
  f1->Close();
  f->Close();
}

Thanks for your help,
Cheers,
Ersel
map.txt (241 Bytes)
Run_3104.root (709 KB)

Ok, i don’t want to use THStack because my histograms are 2D and so THStack makes them 3D,

Do you know another method how to be coloured a histogram with two different colours, or how to superimposed two histograms in one histogram ?

h1->Draw();
h2->Draw("same");

ok
but i want to fill them with different colour, not drawing them.

Thanks,
Cheers,
Ersel

See the “Example of BOX plots with option SAME” in the THistPainter class description.
You can easily modify this example to draw scatter plots. Replace all “SetFillColor” with “SetMarkerColor” and remove the “box” option from all “Draw” calls (or replace all “box” with “scat”).

Thanks Pepe
But, Yep, I have already done these example, which are “superimposed” ones.
So, Problem here is that it considers just “the Mean X of “the first” histogram”, it doesn’t consider the others

I look for a method which creates a histogram with correct statistics and with different colours !

Thanks again,
Cheers,
Ersel

{ gStyle->SetOptStat("eMRiSKuo"); // "nemr" is the default TCanvas *c1 = new TCanvas("c1", "c1", 600, 400); TH2F *hb_all = new TH2F("hb_all", "Example of plots with option SAME ", 40, -3, 3, 40, -3, 3); TH2F *hb1 = new TH2F("hb1", "hb1", 40, -3, 3, 40, -3, 3); TH2F *hb2 = new TH2F("hb2", "hb2", 40, -3, 3, 40, -3, 3); TH2F *hb3 = new TH2F("hb3", "hb3", 40, -3, 3, 40, -3, 3); TH2F *hb4 = new TH2F("hb4", "hb4", 40, -3, 3, 40, -3, 3); gRandom->SetSeed(0); for (Int_t i = 0; i < 1000; i++) { double x, y; gRandom->Rannor(x, y); hb_all->Fill(x, y); if (x>0 && y>0) hb1->Fill(x, y); if (x<0 && y<0) hb2->Fill(x, y); if (x>0 && y<0) hb3->Fill(x, y); if (x<0 && y>0) hb4->Fill(x, y); } hb1->SetFillColor(1); hb1->SetMarkerColor(1); hb2->SetFillColor(2); hb2->SetMarkerColor(2); hb3->SetFillColor(3); hb3->SetMarkerColor(3); hb4->SetFillColor(4); hb4->SetMarkerColor(4); hb_all->Draw("AXIS"); // draw axes and statistics box only TString options = "box same"; // "box same" or "scat same" hb1->Draw(options); hb2->Draw(options); hb3->Draw(options); hb4->Draw(options); // hb_all->Draw("AXIS same"); // redraw axes and statistics box return c1; }

Ok done, Thanks Pepe,

One last question is how might i write histograms to a rootfile? TH1::Draw() just shows histograms, doesn’t write a rootfile,

Might i use TH1::Fill() to write histograms to a rootfile ?

Cheers,
Ersel

TFile *fout = TFile::Open("hb.root", "RECREATE"); // fout->cd(); hb_all->Write(); hb1->Write(); hb2->Write(); hb3->Write(); hb4->Write(); // c1->Write(); delete fout;

I have already tried your script, Write() option. But it didn’t fill them in a rootfile ? All of them are empty for filling except for drawing

 
  TFile *f3 = new TFile("out.root","RECREATE");

  TH2F *h3 = new TH2F("h3","db0 red and db1 blue",48,0,50,1000,0,2700000);
  TH2F *h4 = new TH2F("h4","db0 red",48,0,50,1000,0,2700000);
  TH2F *h5= new TH2F("h5","db1 blue",48,0,50,1000,0,2700000);
 
  TFile *f2 = new TFile("2879_Ch_vs_Gain.root","UPDATE");

   h4 = (TH2F*)f2->Get("650_0GainVsChannel");
   h5 = (TH2F*)f2->Get("650_1GainVsChannel");

   h3->Draw("");
   h4->Draw("same");
   h5->Draw("same");

   h3->Write();
   h4->Write();
   h5->Write();

   h3->Print();
   h4->Print();
   h5->Print();

   f3->Write();

Cheers,
Ersel