cms-cat / cmsstyle

Holding repo for CMS plotting style ROOT macros
https://cmsstyle.readthedocs.io/
MIT License
8 stars 10 forks source link

Place CMS label dynamically #8

Closed clelange closed 1 year ago

clelange commented 1 year ago

@IzaakWN said he had a solution for this that would be great to try.

IzaakWN commented 1 year ago

Hi @clelange, @anmalara, This is my modified version of CMSStyle.py with a solution for the dynamic placement and scaling of the usual CMS labels. I think you more or less need these for placing the CMS labels outside the frame:

  H = pad.GetWh()*pad.GetHNDC()
  W = pad.GetWw()*pad.GetWNDC()
  l = pad.GetLeftMargin()
  t = pad.GetTopMargin()
  r = pad.GetRightMargin()
  b = pad.GetBottomMargin()
  e = 0.025
  scale = float(H)/W if W>H else 1 # float(W)/H

  [...]

  if iPosX==0:
    relPosX_ = relPosX_*(42*t*scale)*(cmsTextSize/0.84)
    posX = l + relPosX_ #*(1-l-r)
    posY = 1 - t + lumiTextOffset*t

Source: https://github.com/cms-tau-pog/TauFW/blob/8984be701ef6a5e4d126a16c390b4efb4afe101a/Plotter/python/plot/CMSStyle.py#L186-L193

Examples of output that shows the dynamic placement works: https://ineuteli.web.cern.ch/ineuteli/forCAT/CMSStyle/

It required some finetuning, and reverse engineering of ROOT's canvas coordinate system... You can test it yourself with these standalone scripts, which should reproduce the plots on my website above:

wget https://raw.githubusercontent.com/cms-tau-pog/TauFW/master/Plotter/python/plot/CMSStyle.py
wget https://raw.githubusercontent.com/cms-tau-pog/TauFW/master/Plotter/test/testCMSStyle.py
python3 testCMSStyle.py -v2
IzaakWN commented 1 year ago

As mentioned in https://github.com/cms-cat/cms-root-style/issues/10, a possible alternative solution is getting the width of "CMS" with one of these methods: TLaTeX::GetBoundingBox, TLaTeX::GetXsize, but I never tried it...

andrzejnovak commented 1 year ago

Examples of output that shows the dynamic placement works: https://ineuteli.web.cern.ch/ineuteli/forCAT/CMSStyle/

I like these a lot. They already very closely match the style in mplhep except having ticks on all sides https://cms-analysis.docs.cern.ch/guidelines/plotting/general/#cms-label-position

If we could sync on this, that would be great!

IzaakWN commented 1 year ago

Yes, actually the ticks are missing on all sides are missing because of a bug that I fixed (pad.SetTicks(1,1)). This was indeed default in the original TDR style. Lemme update the images.

IzaakWN commented 1 year ago

As mentioned in #10, a possible alternative solution is getting the width of "CMS" with one of these methods: TLaTeX::GetBoundingBox, TLaTeX::GetXsize, but I never tried it...

Btw, I could not get these methods to work... They all return 0 size... Not sure if I am missing something...

import ctypes
w, h = ctypes.c_uint(), ctypes.c_uint()

latex = TLatex()
...
latex.DrawLatex(...,"CMS")
latex.GetBoundingBox(w,h)
print(">>> latex.GetXsize()=%r, latex.GetBoundingBox()=%r"%(latex.GetXsize(),(w.value,h.value)))
ttedeschi commented 1 year ago

Hi @clelange, @anmalara, This is my modified version of CMSStyle.py with a solution for the dynamic placement and scaling of the usual CMS labels. I think you more or less need these for placing the CMS labels outside the frame:

  H = pad.GetWh()*pad.GetHNDC()
  W = pad.GetWw()*pad.GetWNDC()
  l = pad.GetLeftMargin()
  t = pad.GetTopMargin()
  r = pad.GetRightMargin()
  b = pad.GetBottomMargin()
  e = 0.025
  scale = float(H)/W if W>H else 1 # float(W)/H

  [...]

  if iPosX==0:
    relPosX_ = relPosX_*(42*t*scale)*(cmsTextSize/0.84)
    posX = l + relPosX_ #*(1-l-r)
    posY = 1 - t + lumiTextOffset*t

Source: https://github.com/cms-tau-pog/TauFW/blob/8984be701ef6a5e4d126a16c390b4efb4afe101a/Plotter/python/plot/CMSStyle.py#L186-L193

Examples of output that shows the dynamic placement works: https://ineuteli.web.cern.ch/ineuteli/forCAT/CMSStyle/

It required some finetuning, and reverse engineering of ROOT's canvas coordinate system... You can test it yourself with these standalone scripts, which should reproduce the plots on my website above:

wget https://raw.githubusercontent.com/cms-tau-pog/TauFW/master/Plotter/python/plot/CMSStyle.py
wget https://raw.githubusercontent.com/cms-tau-pog/TauFW/master/Plotter/test/testCMSStyle.py
python3 testCMSStyle.py -v2

@IzaakWN do you think you can create a Pull Request modifying the example/TDR script including your snippet?

ttedeschi commented 1 year ago

Sorry @IzaakWN, any news on this?

IzaakWN commented 1 year ago

Hi @ttedeschi, yes, I just discussed it with @anmalara, and he agreed to try it out.

anmalara commented 1 year ago

Thanks, @IzaakWN for the tip. I've implemented it in https://github.com/cms-cat/cms-root-style/pull/15