cms-gem-daq-project / gem-plotting-tools

Repository for GEM commissioning plotting tools
GNU General Public License v3.0
1 stars 26 forks source link

add DAC scan analysis #158

Closed AndrewLevin closed 5 years ago

AndrewLevin commented 5 years ago

This script analyzes DAC scans. It reads an input file containing DAC and ADC values, performs calibration, performs a fit, and reports the DAC values which should produce the nominal voltage or current as specified in the VFAT3 manual. 3 by 8 plots of the calibrated data and the fit are also produced.

Description

Pseudo-code, from @bdorney, copied here for convenience from https://github.com/cms-gem-daq-project/gem-plotting-tools/issues/155:

if __name__ == '__main__':
   setup an argparse.ArgumentParser instance
   get arguments from a user

   # Determine which links present suggest using root_numpy, something like:
   import root_numpy as rp
   import numpy as np
   bNames = ['link']
   ohArray = rp.tree2array(tree=calTree, branches=bNames)
   ohArray = np.unique(ohArray) # now ohArray will have one entry for each unmasked optohybrid

   # Determine which DAC was scanned and against which ADC
   nameX = ""
   nameY = ""
   for event in calTree:
      nameX = event.nameX
      nameY = event.nameY
      break # all entries will be the same

   # Get ADC0 or ADC1 calibration
   # nameY will either be ADC0 or ADC1, you'll need to load calibration for each link
   # use parseCalFile(...) from gem-plotting-tools/utils/anautilities.py
   # Note this calibration is unique to each (vfat,ohN) pairing
   # In future this is where a DB query based on vfatID would take place 

   # Make output plots, suggest TGraphErrors objects
   # I would suggest you use a nesteddict object (gempython/utils/nesteddict.py) to index these by [oh][vfat]
   for link in ohArray:
      for vfat in range(0,24):
           dict_dacScanResults[link][vfat] = r.TGraphErrors(...) # declare TGraphErrors
           You can get number of points in TGraphErrors by looking at maxVfat3DACSize of amc_user_functions_xhal.py (from cmsgemos PR#206)

   # Loop over events in the tree and fill plots
   for event in calTree:
       assign (dacValX, dacValY, dacValY_Err) to the tgraph for a given (vfat,oh) pair
       Here you need to use the ADCX (for X = 0,1) calibration to convert dacValY & dacValY_Err to physical units
       You also need to identify if nameX corresponds to a bias voltage DAC or a bias current DAC, see VFAT3 manual Table 25: GBL CFG CTR 4 : Monitoring settings
       Note nameX is the register that was scanned during the DAC scan, but we want plots that are Y=nameX (reg scanned) and X=nameY (ADC value), see below 

   # Fit the TGraphErrors Objects
   for oh in ohArray
      for vfat in range(0,24):
          dict_dacScanResults[oh][vfat].Fit(...) # linear fitting may not be suitable, polynomial may be required, investigation needed.  Possible try several fits and choose one with best norm chi2

    # Determine DAC values to achieve recommended bias voltage and current settings
    See Table 9, Nominal Current column, and the VFAT3a specific note under table 9
    See Table 10, Nominal Voltage column
    Here for each (vfat,oh) you should use the fit function to do a look-up on the nominal value to get the DAC value that will achieve this, e.g.
    dacVal = myTF1.Eval(nominal voltage)
    This then should be printed to terminal as a table, an output file (in either tab-delimited or TTree format) and then as a TGraph object with DAC value vs. VFAT position

    # Make plots
    Make our standard 3x8 grid plot of the DAC scan results, see make3x8Canvas(...), one such grid plot should be made for each entry in ohArray
    Store plots in an output TFile (use TDirectories to organize logically)

    Exit

Help output:

[amlevin@gem904qc8daq gem-plotting-tools]$ ./anaDACScan.py -h
usage: anaDACScan.py [-h] [--calFileList calFileList] [-o outfilename]
                     [--assignXErrors]
                     infilename

Arguments to supply to anaDACScan.py

positional arguments:
  infilename            Filename from which input information is read

optional arguments:
  -h, --help            show this help message and exit
  --calFileList calFileList
                        File specifying which calFile to use for each OH.
                        Format: 0 /path/to/my/cal/file.txt<newline> 1
                        /path/to/my/cal/file.txt<newline>...
  -o outfilename, --outfilename outfilename
                        Filename to which output information is written
  --assignXErrors       dacValY_Err values are always used, but dacValX_Err
                        values are used if and only if this flag is set

calFileList example:

1 /home/amlevin/gem-plotting-tools/calibration_OH1.txt

Example execution:

[amlevin@gem904qc8daq gem-plotting-tools]$ ./anaDACScan.py dacScanV3.root  --calFileList calibration.txt 
Analyzing: 'dacScanV3.root'
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
Warning: the nominal DAC value is that was found from the fit is less than 0
| OH | vfatN | dacVal |
| :-: | :---: | :----: |
| 1 | 0  | 120 | 
| 1 | 1  | 120 | 
| 1 | 2  | 120 | 
| 1 | 3  | 120 | 
| 1 | 4  | -87 | 
| 1 | 5  | -82 | 
| 1 | 6  | -98 | 
| 1 | 7  | -75 | 
| 1 | 8  | 120 | 
| 1 | 9  | 120 | 
| 1 | 10  | 120 | 
| 1 | 11  | 120 | 
| 1 | 12  | -72 | 
| 1 | 13  | -100 | 
| 1 | 14  | -76 | 
| 1 | 15  | 120 | 
| 1 | 16  | 120 | 
| 1 | 17  | 120 | 
| 1 | 18  | 120 | 
| 1 | 19  | 120 | 
| 1 | 20  | -85 | 
| 1 | 21  | -97 | 
| 1 | 22  | -111 | 
| 1 | 23  | -104 | 
Info in <TCanvas::Print>: png file Summary.png has been created

Types of changes

Motivation and Context

This script was requested in https://github.com/cms-gem-daq-project/gem-plotting-tools/issues/155

How Has This Been Tested?

I have tested that the script runs on an input file provided by @bdorney: dacScanV3.root

Screenshots (if appropriate):

Output root file structure: connection to localhost 10152018-08 16 10

DACvsADC: dacvsadc

RawADCvsDAC: rawadcvsdac

NominalDacValVsVFATX: nominaldacvalvsvfatx

Summary plot: summary

Checklist:

bdorney commented 5 years ago

Is this a work in progress or is it ready for review?

AndrewLevin commented 5 years ago

@bdorney, it is a work in progress

now, the only thing missing is the use of the correct nominal value for all of the dacSelect options

the tables in the vfat3 manual do not actually have rows for each of the dacSelect values that you wrote in https://github.com/cms-gem-daq-project/gem-plotting-tools/issues/155

to help me with this, can you tell me the nominal value for CFG_BIAS_SH_I_BFCAS?

bdorney commented 5 years ago

Commits here don’t seem to reflect your comment. Perhaps some commits where not pushed to your remote? Also rebase on top of develop branch, and when ready remove WIP. I’ll review after WIP is removed.

You’ll need to contact VFAT3 team for question regarding BFCAS.

AndrewLevin commented 5 years ago

@bdorney, I rebased it (although given that I only added a new file, I am not sure why that would matter)

Commits here don’t seem to reflect your comment. Perhaps some commits where not pushed to your remote?

Can you be more specific? All commits were pushed to the remote.

You’ll need to contact VFAT3 team for question regarding BFCAS.

I just sent an e-mail to Paul and Henri with you in cc.

bdorney commented 5 years ago

When you clean this up please attach screenshots to the PR so we can see what plots have been produced.

AndrewLevin commented 5 years ago

@bdorney, two questions:

1) In https://github.com/cms-gem-daq-project/vfatqc-python-scripts/pull/203, CFG_THR_ARM_DAC and CFG_THR_ZCC_DAC are repeated:

14 CFG_THR_ARM_DAC 15 CFG_THR_ZCC_DAC 35 CFG_THR_ARM_DAC 36 CFG_THR_ZCC_DAC

Why?

2) The calibrated ADC values are in what units (e.g. uA or nA)? I guess this depends on the calFile, which I don't have an real example of.

bdorney commented 5 years ago

For question 1 this is illustrated in the VFAT3 manual, table 25.

For question 2 the calibration of the ADC is always in volts. In the case of voltage the calibration equation is used, in the case of current you need to convert to volts, e.g. it's the current across a 20kOhm resistor. If a monitoring current is being measured that is not the IREF than the current being monitored is the current of interest plus the IREF. As described in table 25 of the VFAT3 manual.

While you don't need a calibration file for this exercise (e.g. set m=1 and b=0) here are two examples for VFAT3B:

% more vfat3CalInfo/vfat3b/vfat3_0061/calibration_data.dat

Adjusting the global reference current.
- Iref adjusted.
Register value: 28 .

Internal ADCs calibrated. Values:
ADC0: 2.035943 + -325.388101
ADC1: 2.301137 + -450.231461

CAL_DAC conversion completed.
CAL_DAC to fC: 0.215792 + -2.028758

Scan duration: 0.017219 min

And for VFAT3A:

% more vfat3CalInfo/vfat3a/hbrdnp011/calPars.reg
adc0M/D:adc0B/D:adc1M/D:adc1B/D:cal_dac_fcM/D:cal_dac_fcB/D:Iref/I
2.059961    -318.730307 0.823305    334.545018  0.275427    -2.187759   27

Note this is unique to each VFAT and there 5000+ so I don't have each value nor do I have a "characteristic" or "typical" value.

bdorney commented 5 years ago

VFAT3 manual

AndrewLevin commented 5 years ago

@bdorney, in the example root file that you provided, the dacValYs are between 100.0 and 500.0, so calibrating them using a slope of 1 and an intercept of 0, and then dividing by 20000 gives values between 0.05 and 0.025. The nominal current for CFG_THR_ARM_DAC from the VFAT3 manual is 3.2 uA. Are we sure about the value of the resistance?

bdorney commented 5 years ago

@bdorney, in the example root file that you provided, the dacValYs are between 100.0 and 500.0, so calibrating them using a slope of 1 and an intercept of 0, and then dividing by 20000 gives values between 0.05 and 0.025. The nominal current for CFG_THR_ARM_DAC from the VFAT3 manual is 3.2 uA. Are we sure about the value of the resistance?

If you're using a dummy value of slope=1 and intercept of 0 then you're not in volts, but DAC units.

bdorney commented 5 years ago

Additionally I'd wager from Fig 29 in VFAT3 manual that the ADC calibration comes out in mV.

AndrewLevin commented 5 years ago

ok,

Additionally I'd wager from Fig 29 in VFAT3 manual that the ADC calibration comes out in mV.

I have taken this into account now

AndrewLevin commented 5 years ago

@bdorney, for the two registers

CFG_THR_ARM_DAC CFG_THR_ZCC_DAC

isn't it ambiguous if they correspond to a voltage or a current? Possibly the dacSelect number needs to be stored also?

bdorney commented 5 years ago

@bdorney, for the two registers CFG_THR_ARM_DAC CFG_THR_ZCC_DAC isn't it ambiguous if they correspond to a voltage or a current? Possibly the dacSelect number needs to be stored also?

Yup that's a good point. For now just consider the voltage case. Maybe this week if you have free time in the camp you can open a GitHub issue for dacScanV3.py to store the dacSelect in the output TTree (and even make the PR!).

bdorney commented 5 years ago

Once this comment is resolved I'll force merge this PR and build the rpm's for the camp. I understood Henri is coming tomorrow so we should be in time. Otherwise try to reach him by email before 11h00.

bdorney commented 5 years ago

Thanks for working through the weekend on this!

bdorney commented 5 years ago

Given Henri's recent response we expect mV or uA please ensure all output plots are in these units.

AndrewLevin commented 5 years ago

@bdorney, Henri only confirmed the units of the voltage calibration constants, should I also ask him about the units of the current calibration constants?

Also, I think I may be doing things in the wrong order:

https://github.com/cms-gem-daq-project/gem-plotting-tools/blob/0abcc751eb7ed5adeaee10157ae46a607cb7393f/anaDACScan.py#L219-L223

The correct order for a current register is:

1) calibrate 2) subtract the nominal current 3) use Ohm's law to convert to volts,

right?

bdorney commented 5 years ago

@bdorney, Henri only confirmed the units of the voltage calibration constants, should I also ask him about the units of the current calibration constants?

There are no current calibration coefficients, and ADC always reads voltage. A bias current is converted to a voltage by the use of a resistor (e.g. the 20kOhm resistor).

After everything is said and done bias voltage DAC scans should have their physical unit by mV. Bias current DAC scans should have their physical unit by uA.

The correct order for a current register is: calibrate subtract the nominal current use Ohm's law to convert to volts, right?

No, the ADC is always in voltage units.

  1. Convert ADC counts to milli volts,
  2. Convert milli volts to micro amps,
  3. Subtract IREF from those bias currents that are not IREF.
AndrewLevin commented 5 years ago

@bdorney, ok, now all of the plots are in mV and uA

AndrewLevin commented 5 years ago

The errors on the dacValY variable, which is plotted on the x-axis in the DAC vs ADC plots, have been removed now. ROOT's fitting method does not converge when these errors are used, regardless of what they are set to. I tried a variety of tweaks to the fitting method and the initial values of the polynomial coefficients, but nothing helps at all. In any case, we have such a large number of points and these errors are small enough that their effect would be negligible, at least in the cases I have considered. The fits with the fifth degree polynomial are extremely good when using 0 for the error on the dacValY variable (see the screenshots).

bdorney commented 5 years ago

Please add units to access labels where appropriate (e.g. mV or #muA).

Additionally there's no labels on the axes of NominalDacValVsVFATX. Please add them, with units. Additionally restrict this plot's y-axis range to something physical (e.g. negative numbers should not be displayed).

Drawing option APE1 is strongly preferred. Lines should not be drawn between points, only fit curves should be draw.

For DACvsADC increase the point size (or decrease the TF1 width) so we can see how the points and the fit are matching. Perhaps use a dotted line style for the fit.

Generally the over-arching question when developing a tool for general use is: "can someone with little experience understand what is being plotted?"

bdorney commented 5 years ago

We can also resolve the issue about not knowing if CFG_THR_ARM_DAC is a bias current or voltage by co-resolving: https://github.com/cms-gem-daq-project/vfatqc-python-scripts/issues/206

lmoureaux commented 5 years ago

You need to add doc/man/anaDACScan.rst and the corresponding line to doc/man-index.rst in order for your documentation to be built correctly

AndrewLevin commented 5 years ago

@lmoureaux, the two files are added now

lmoureaux commented 5 years ago

Issues noticed when installing in a virtualenv:

AndrewLevin commented 5 years ago

@lmoureaux, fixed

AndrewLevin commented 5 years ago

@bdorney, the scaling factors in Table 12 and Table 13 of the VFAT3 manual are now applied

AndrewLevin commented 5 years ago

@bdorney, I am now masking vfats that have a constant dacValY that is equal to 0