/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes
getLEDGain::LEDGainAlg Class Reference
Inheritance diagram for getLEDGain::LEDGainAlg:
Inheritance graph
[legend]
Collaboration diagram for getLEDGain::LEDGainAlg:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def histname
def fullpath
def goodfit
def initialize
def execute
def finalize
def DoAllFits
def DBUpload

Public Attributes

 det
 runno
 workdir
 cableSvc
 statsSvc
 calibSvc
 Nevts
 Note: adc histograms are created in "execute".
 badfits
 chan
 is2inch
 good
 gain
 gainunc
 sigma
 fitqual
 dbsave
 filesave
 firstctx
 channels
 svcModefirst
 avg
 ngoodpmts
 wrt
 outfile

Detailed Description

Definition at line 27 of file getLEDGain.py.


Constructor & Destructor Documentation

def getLEDGain::LEDGainAlg::__init__ (   self,
  name 
)

Definition at line 29 of file getLEDGain.py.

00030                            :
00031         DybPythonAlg.__init__(self,name)
00032         self.det = None
00033         self.runno = None
00034         self.workdir = None
00035         return


Member Function Documentation

def getLEDGain::LEDGainAlg::histname (   self,
  chanId 
)

Definition at line 36 of file getLEDGain.py.

00037                              :
00038         name = "h_b"+str(chanId.board())+"_c"+str(chanId.connector());
00039         return name

def getLEDGain::LEDGainAlg::fullpath (   self,
  chanId 
)

Definition at line 40 of file getLEDGain.py.

00041                              :
00042         name = self.histname(chanId);
00043         return "/file0/myhists/channels/"+name;

def getLEDGain::LEDGainAlg::goodfit (   self,
  func 
)

Definition at line 44 of file getLEDGain.py.

00045                           :
00046         adcMean=func.GetParameter(3);
00047         adcMeanUncert =func.GetParError(3);
00048         adcSigma = func.GetParameter(4);
00049         adcSigmaUncert = func.GetParError(4);
00050         Chi2 = func.GetChisquare();
00051         ndf = func.GetNDF();
00052         fitquality=15;
00053         if ndf!=0:
00054             fitquality = Chi2/ndf;
00055         if adcMean<6 or adcMean>45 or adcMeanUncert>1 or adcSigma>18 or adcSigmaUncert>1 or fitquality>6:
00056             return False
00057         else:
00058             return True

def getLEDGain::LEDGainAlg::initialize (   self)

Definition at line 59 of file getLEDGain.py.

00060                         :
00061         status = DybPythonAlg.initialize(self)
00062         if status.isFailure(): return status
00063         self.info("initializing")
00064 
00065         # Cable Service
00066         self.cableSvc = self.svc('ICableSvc','CableSvc')
00067         if self.cableSvc == None:
00068             self.error("Failed to get CableSvc")
00069             return FAILURE
00070 
00071         #  Statistics Service
00072         self.statsSvc = self.svc('IStatisticsSvc','StatisticsSvc')
00073         if self.statsSvc == None:
00074             self.error("Failed to initialize statistics service.")
00075             return FAILURE
00076 
00077         # Get CalibData Service
00078         self.calibSvc = self.svc('IPmtCalibSvc','DybPmtCalibSvc')
00079 
00080         # histograms --------------------------------------------------
00081         self.stats["/file0/myhists/hocc"] = TH1F("hocc","",100,0,1);
00082         self.stats["/file0/myhists/hocc"].GetXaxis().SetTitle("occupancy");
00083         self.stats["/file0/myhists/hocc"].GetYaxis().SetTitle("# of entries");
00084         self.stats["/file0/myhists/h2occ"] = TH2F("h2occ","",21,-0.5,20.5,16,0.5,16.5);
00085         self.stats["/file0/myhists/h2occ"].GetXaxis().SetTitle("board");
00086         self.stats["/file0/myhists/h2occ"].GetYaxis().SetTitle("connector");
00087         ##Note: adc histograms are created in "execute" 
00088         # --------------------------------------------------------------
00089 
00090         # global variables
00091         self.Nevts=0;
00092         self.badfits=0;
00093         self.chan = [];
00094         self.is2inch = [];
00095         self.good = [];
00096         self.gain = [];
00097         self.gainunc = [];
00098         self.sigma = [];
00099         self.fitqual = [];
00100 
00101         os.environ['DBCONF'] = "tmp_offline_db"
00102 
00103         # db and/or file save
00104         self.dbsave=True;
00105         self.filesave=True;
00106                
00107         return SUCCESS

def getLEDGain::LEDGainAlg::execute (   self)

Definition at line 108 of file getLEDGain.py.

00109                      :
00110         #self.info("executing")
00111         evt = self.evtSvc()
00112 
00113         # Access the Readout Header.  This is a container for the readout data
00114         readoutHdr = evt["/Event/Readout/ReadoutHeader"]
00115         if readoutHdr == None:
00116             self.error("Failed to get current readout header")
00117             return FAILURE
00118         
00119         # Access the daqCrate.  This is the data from one trigger.
00120         daqCrate = readoutHdr.daqCrate()
00121         if daqCrate == None:
00122             self.error("Failed to get daqCrate from header")
00123             return FAILURE
00124 
00125         # Avoid irrelevant triggers (from other detectors or non-Calib)
00126         triggerType = daqCrate.triggerType();
00127         if triggerType & Trigger.kCalib == Trigger.kNone:
00128             return SUCCESS;
00129         detId = Detector(readoutHdr.context().GetSite(),readoutHdr.context().GetDetId());
00130         if detId.isAD()!=1:
00131             return SUCCESS;
00132         if readoutHdr.context().GetDetId()!=self.det:
00133             return SUCCESS;
00134         self.Nevts+=1
00135 
00136         # SvcMode (used to query cable map, so use task=0)
00137         svcMode = ServiceMode( readoutHdr.context(), 0 )
00138 
00139         # --> prepare histograms for each channel in cable map and record context of first event
00140         if self.Nevts==1:
00141             self.firstctx = Context(readoutHdr.context())
00142             self.channels=self.cableSvc.feeChannelIds(svcMode);
00143             for chanId in self.channels:
00144                 namel = self.histname(chanId);
00145                 fullname = self.fullpath(chanId);
00146                 self.stats[fullname] = TH1F(namel,"",400,0,400);
00147         
00148         
00149         # Loop over channels <-----
00150         if None != daqCrate.asPmtCrate():
00151             for channel in daqCrate.pmtChannelReadouts():
00152                 channelId = channel.channelId()
00153                 
00154                 # Get the PMT ID attached to this electronics channel
00155                 pmtId = self.cableSvc.adPmtSensor(channelId, svcMode)
00156                 if pmtId.bogus()==False and pmtId.is8inch()==True:
00157                     self.stats["/file0/myhists/h2occ"].Fill(channelId.board(),channelId.connector());
00158                     # -->Fill adc channel
00159                     if channelId in self.channels:
00160                       namel = self.histname(channelId);
00161                       fullname = self.fullpath(channelId);
00162                       for adcIndex in range(channel.hitCount()):
00163                           adc=channel.adc(adcIndex)
00164                           ped=channel.preAdcAvg(adcIndex)
00165                           if channel.isHighGainAdc(adcIndex):
00166                               self.stats[fullname].Fill(adc-ped);
00167                         
00168         return SUCCESS
        
def getLEDGain::LEDGainAlg::finalize (   self)

Definition at line 169 of file getLEDGain.py.

00170                       :
00171         self.info("finalizing")
00172 
00173         # calculate occupancy and fill 1d histogram
00174         self.stats["/file0/myhists/h2occ"].Scale(1./self.Nevts);
00175         for ibinx in range(1,self.stats["/file0/myhists/h2occ"].GetXaxis().GetNbins()+1):
00176             for ibiny in range(1,self.stats["/file0/myhists/h2occ"].GetYaxis().GetNbins()+1):
00177                 if self.stats["/file0/myhists/h2occ"].GetBinContent(ibinx,ibiny) != 0:
00178                     self.stats["/file0/myhists/hocc"].Fill(self.stats["/file0/myhists/h2occ"].GetBinContent(ibinx,ibiny));
00179 
00180         # if occupancy is not right then do not even bother
00181         print "Mean occupancy: ",self.stats["/file0/myhists/hocc"].GetMean()
00182         if self.stats["/file0/myhists/hocc"].GetMean() > 0.22 or self.stats["/file0/myhists/hocc"].GetMean() < 0.02:
00183             return SUCCESS 
00184 
00185         # fitting
00186         self.DoAllFits();
00187 
00188         # uploading to DB
00189         print "The number of bad fits is ",self.badfits
00190         if self.badfits<10: #there are 6 2inch PMTs which will always be bad fits
00191             self.DBUpload();
00192 
00193         # finalize--
00194         status = DybPythonAlg.finalize(self)
00195         return status

def getLEDGain::LEDGainAlg::DoAllFits (   self)

Definition at line 196 of file getLEDGain.py.

00197                        :
00198         try:
00199             from ROOT import NIMmodelFit
00200         except ImportError:
00201             from ROOT import gROOT
00202             gROOT.ProcessLine('.L $LEDGAINROOT/share/NIMmodel.C')
00203             from ROOT import NIMmodelFit
00204 
00205         for chanId in self.channels:
00206             print "Fitting ",chanId.asString()
00207 
00208             #first fit 
00209             hcurr=self.stats[self.fullpath(chanId)]
00210             nimmodelfunc = NIMmodelFit('fit',5,100)           
00211             AreaGuess = hcurr.GetEntries();
00212             Q1MeanGuess = hcurr.GetMean();
00213             Q1SigmaGuess = hcurr.GetRMS();
00214             nimmodelfunc.SetParameters(AreaGuess, 90,1.9,Q1MeanGuess, Q1SigmaGuess, 0.01, 0.03,0.05);
00215             nimmodelfunc.FixParameter(1,0);
00216             nimmodelfunc.FixParameter(2,0);
00217             nimmodelfunc.FixParameter(5,0);
00218             nimmodelfunc.FixParameter(6,0);
00219             
00220             hcurr.Fit(nimmodelfunc,"RQ");
00221 
00222             #check fit results; if bad fit try to fit again in different range 
00223             if self.goodfit(nimmodelfunc)==False:    
00224                 print "Bad fit for ",chanId.asString()," --> ",chanId.board(),",",chanId.connector()
00225                 nimmodelfunc = NIMmodelFit('fit2',10,100)
00226                 nimmodelfunc.SetParameters(AreaGuess, 90,1.9,Q1MeanGuess, Q1SigmaGuess, 0.01, 0.03,0.05);
00227                 nimmodelfunc.FixParameter(1,0);
00228                 nimmodelfunc.FixParameter(2,0);
00229                 nimmodelfunc.FixParameter(5,0);
00230                 nimmodelfunc.FixParameter(6,0);
00231                 hcurr.Fit(nimmodelfunc,"RQ");
00232 
00233             self.svcModefirst = ServiceMode(self.firstctx,0);
00234             pmtId = self.cableSvc.adPmtSensor(chanId, self.svcModefirst)
00235             if pmtId.is2inch():
00236                 self.is2inch.append(1)
00237             else:
00238                 self.is2inch.append(0)
00239             self.chan.append(chanId.fullPackedData());
00240             self.good.append(self.goodfit(nimmodelfunc));        
00241             self.gain.append(nimmodelfunc.GetParameter(3));
00242             self.gainunc.append(nimmodelfunc.GetParError(3));
00243             self.sigma.append(math.fabs(nimmodelfunc.GetParameter(4)));
00244             chi2ndf=100;
00245             if nimmodelfunc.GetNDF()!=0:
00246                 chi2ndf=nimmodelfunc.GetChisquare()/nimmodelfunc.GetNDF();
00247             self.fitqual.append(chi2ndf);
00248 
00249             #count bad fits
00250             if self.goodfit(nimmodelfunc)==False:
00251                 self.badfits+=1
00252                              
00253         self.info("done fitting all channels!")
00254                 
00255         return SUCCESS
00256 

def getLEDGain::LEDGainAlg::DBUpload (   self)

Definition at line 257 of file getLEDGain.py.

00258                       :
00259         
00260         from DybDbi import GCalibPmtFineGain
00261         from DybDbi import TimeStamp, ContextRange
00262         from DybDbi import Site, DetectorId, SimFlag
00263 
00264         #calculate average for 2inch PMTs
00265         self.avg=0;
00266         self.ngoodpmts=0;
00267         for ichan in range(0,len(self.chan)):
00268             if(self.good[ichan]):
00269                 self.ngoodpmts+=1
00270                 self.avg+=self.gain[ichan]
00271         if self.ngoodpmts!=0:
00272             self.avg=self.avg/self.ngoodpmts;
00273         else:
00274             self.avg=19
00275         print "--> Good PMTs: ",self.ngoodpmts
00276         print "--> Average gain: ",self.avg
00277 
00278         #open writer
00279         if self.dbsave:
00280             self.wrt = GCalibPmtFineGain.Wrt()
00281             print self.firstctx.AsString()
00282             tstart = self.firstctx.GetTimeStamp();
00283             tend=TimeStamp.GetEOT();
00284             contran=ContextRange(self.firstctx.GetSite(),SimFlag.kData,tstart,tend);
00285             self.wrt.ctx(
00286                 contextrange=contran,
00287                 versiondate=TimeStamp(0, 0),
00288                 subsite=self.firstctx.GetDetId(),
00289                 task=0
00290                 )
00291         
00292         #  write to file
00293         if self.filesave:
00294             filename=self.workdir+"/ledGain_"+str(self.firstctx.GetSite())+"_"+str(self.firstctx.GetDetId())+"_"+str(self.runno)+"_"+str(self.firstctx.GetTimeStamp().GetSec())+"_"+str(self.firstctx.GetTimeStamp().GetNanoSec())+".txt";
00295             self.outfile=open(filename,'w')
00296 
00297         for ichan in range(0,len(self.chan)):
00298             chanIdtofill = FeeChannelId(self.chan[ichan])
00299             gaintofill=self.gain[ichan]
00300             gainunctofill=self.gainunc[ichan]
00301             sigmatofill=self.sigma[ichan]
00302             fitqualtofill=self.fitqual[ichan]
00303             #2inch PMTs get average of good pmts (with -1 in fitqual)
00304             if self.is2inch[ichan]==1:
00305                 gaintofill=self.avg
00306                 gainunctofill=1;
00307                 sigmatofill=6.3;
00308                 fitqualtofill=-1;
00309             #bad fits get whatever is in database (with -1 in fitqual)
00310             if self.good[ichan]==False:
00311                 fineGainCalib = self.calibSvc.fineGainCalibData(chanIdtofill, self.svcModefirst);
00312                 if fineGainCalib:
00313                     gaintofill=fineGainCalib.m_speHigh;
00314                     gainunctofill=fineGainCalib.m_speHighError;
00315                     sigmatofill=fineGainCalib.m_sigmaSpeHigh;
00316                     fitqualtofill=-1;
00317                 else: #if nothing in database use defaults
00318                     gaintofill=19;
00319                     gainunctofill=1;
00320                     sigmatofill=6.3;
00321                     fitqualtofill=-1;
00322 
00323             if self.filesave:
00324                 towrite=str(chanIdtofill.fullPackedData())+"\t"+str(gaintofill)+"\t"+str(gainunctofill)+"\t"+str(sigmatofill)+"\t"+str(fitqualtofill)+"\n";
00325                 self.outfile.write(towrite);
00326 
00327             if self.dbsave:
00328                 entry=GCalibPmtFineGain.Create(ChannelId=chanIdtofill,
00329                                                SpeHigh=gaintofill,
00330                                                SpeHighError=gainunctofill,
00331                                                SigmaSpeHigh=sigmatofill,
00332                                                SpeHighFitQual=fitqualtofill)
00333                 self.wrt.Write(entry)           
00334             
00335         if self.dbsave:
00336             seqno=self.wrt.Close();
00337             self.info("done uploading to database; seqno=%d"%(seqno))
00338         return SUCCESS 
00339     


Member Data Documentation

Definition at line 29 of file getLEDGain.py.

Definition at line 29 of file getLEDGain.py.

Definition at line 29 of file getLEDGain.py.

Definition at line 59 of file getLEDGain.py.

Definition at line 59 of file getLEDGain.py.

Definition at line 59 of file getLEDGain.py.

Note: adc histograms are created in "execute".

--------------------------------------------------------------

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 61 of file getLEDGain.py.

Definition at line 108 of file getLEDGain.py.

Definition at line 108 of file getLEDGain.py.

Definition at line 196 of file getLEDGain.py.

Definition at line 257 of file getLEDGain.py.

Definition at line 257 of file getLEDGain.py.

Definition at line 257 of file getLEDGain.py.

Definition at line 257 of file getLEDGain.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Fri May 16 2014 09:55:31 for LEDGain by doxygen 1.7.4