Ignore:
Timestamp:
09/24/09 15:45:24 (15 years ago)
Author:
daqct3
Message:
New Functions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trigger/CommunicateWithVME.py

    r77 r115  
    1515import _VME
    1616import time
     17import datetime
     18
     19def GetStatus():
     20    print "Trigger thresholds: ", GlobalVariables.TriggerThresholds
     21    print "Hex Pattern: ", GlobalVariables.HexPattern
     22    print "Majority Level: ",GlobalVariables.MajorityLevel
     23
     24
     25
     26
     27
    1728def GetRate(module,channel):
    1829    _VME.V560_Clear_Scales( module)
     
    2334    return (endCounter-beginCounter)/2000.
    2435
     36
     37
    2538def GetRates(module):
    2639    _VME.V560_Clear_Scales( module)
     
    4356        #GlobalVariables.ServerMessage = str(beginCounter[i])
    4457    #GlobalVariables.ServerMessage = "Evaluating rates..."
    45     time.sleep(2)
     58    time.sleep(1)
    4659    for i in range(0,16):
    4760        endCounter[i] = _VME. V560_Read_Counter( module,  i)
    4861        #GlobalVariables.ServerMessage = str(endCounter[i])
    49         rates[i+1] =(endCounter[i]-beginCounter[i])/2000.
     62        rates[i+1] =(endCounter[i]-beginCounter[i])/1000.
    5063    return rates
     64
    5165
    5266class GimmeRates(threading.Thread):
     
    6478            self.rates=newrates
    6579
     80
     81
     82       
     83class WriteOutput(threading.Thread):      #thread that writes the rates and threshold to a file.
     84    rates=["rates",
     85        0,0,0,0,
     86           0,0,0,0,
     87           0,0,0,0,
     88           0,0,0,0]
     89    interval = 2
     90    filename = "test.txt"
     91    #gimmeRates =GimmeRates()
     92    gimmeRates=GimmeRates()
     93    #File=open(filename,'a')
     94    def __init__(self, filename="test.txt", interval = 10):
     95        threading.Thread.__init__(self)
     96        #self.OpenNewFile(filename)
     97
     98
     99
     100    def writeToFile(self):
     101        File = open(self.filename,'a')
     102        #print "writing", (GlobalVariables.Rates)
     103        #year = datetime.date.today().year
     104        #month = datetime.date.today().month
     105        #day = datetime.date.today().day
     106        year = time.localtime(time.time())[0]
     107        month =time.localtime(time.time())[1]
     108        day = time.localtime(time.time())[2]
     109        hour = time.localtime(time.time())[3]
     110        minute = time.localtime(time.time())[4]
     111        seconds = time.localtime(time.time())[5]
     112        ms = time.localtime(time.time())[6]
     113        #date=str(year)
     114        #if (hour>=12):
     115        #    day = day+1
     116
     117        t = datetime.datetime.now()
     118        EpochSeconds=int(time.mktime(t.timetuple()))
     119
     120        outstring = "Trigger " + " ThreshRate "+str(year) + " " + str(month) +" " + str(day) +" "+str(hour)+" " + str(minute) + " " + str(seconds)+ " "+str(ms)+" "+str(EpochSeconds)+" Thresh: "
     121        File.write(outstring)
     122        #print outstring
     123        File.write(str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', ''))
     124        File.write(" rate: ")
     125        GlobalVariables.Rates = self.gimmeRates.rates
     126        File.write(str(GlobalVariables.Rates[0:]))#.strip('[]').replace(',', ''))
     127        #print str(GlobalVariables.Rates[0:]).strip('[]').replace(',', ''))
     128
     129     
     130        #print str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', '')
     131        #print "\n"
     132       
     133        File.write("\n")
     134        File.close();
     135
     136
     137
     138    def run(self):
     139        self.gimmeRates.start()
     140        while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"):
     141            time.sleep(0.001)
     142            time.sleep(self.interval)
     143            self.writeToFile()
     144
     145
     146
     147
     148
     149
     150
     151       
     152   
     153
     154
    66155class ParseUserInput(threading.Thread):
    67156    gimmeRates=GimmeRates()
     157    writeOutput=WriteOutput()
    68158    def Parse(self,Command):
    69159        Command = Command[1:]
     
    108198
    109199                if(V560Command=="GetRates" or V560Command=="GR"):
    110                     GlobalVariables.ServerMessage = "trying to get rates"
    111                     time.sleep(0.01)
     200                    #GlobalVariables.ServerMessage = "trying to get rates"
     201                    #time.sleep(0.01)
    112202                    try:
    113203                        module = int(sCommand[2])
     
    132222                    GlobalVariables.ServerMessage += "V812 SetHexPat <module#[1-10]> <HexPattern[0x0000-0xFFFF>\n"
    133223                    GlobalVariables.ServerMessage += "V812 SetThresh <module#[1-10]> <channel#[0-15]> <thresh[0-255]>\n"
     224                    GlobalVariables.ServerMessage += "V812 SetThreshDiff <module#[1-10]> <channel#[0-15]> <threshdiff[0-255]>\n"
    134225                    GlobalVariables.ServerMessage += "V812 SetAllThresh <module#[1-10]>  <thresh[0-255]>\n"
     226                    GlobalVariables.ServerMessage += "V812 SetAllThreshDiff <module#[1-10]>  <threshdiff[0-255]>\n"
    135227                    GlobalVariables.ServerMessage += "V812 SetMajLevel <module#[1-10]> <MajLev[1-20]>\n"
    136228                    GlobalVariables.ServerMessage += "V812 SetMajThresh <module#[1-10]> <MajThr[0-255]>\n"
    137229                    GlobalVariables.ServerMessage += "V812 SetDeadTime <module#[1-10]> <Block [0-1]> <DeadTime[0-255]>\n"
     230                    GlobalVariables.ServerMessage += "V812 SetOutputWidth <module#[1-10]> <Block [0-1]> <OutputWidth[0-255]>\n"
     231                    GlobalVariables.ServerMessage += "V812 Status\n"
     232                    GlobalVariables.ServerMessage += "V812 RateScan <module> <start> <step> <stop>\n"
     233                   
    138234                    time.sleep(0.05)
    139235                #set the hexpattern:
     
    152248                            error_code=_VME.V812_Set_Pattern_Inhibit_Hex(module, hexpat)
    153249                            GlobalVariables.ServerMessage = "success! "
     250                            GlobalVariables.HexPattern =str(hex(hexpat))
    154251                            time.sleep(0.05)
    155252                           
     
    163260                        time.sleep(0.05)
    164261                elif(V812Command=="SetAllThresh"):
    165                     print "trying to set all the threshold:"
     262                    print "trying to set the threshold:"
    166263                    try:
    167264                        module  = int(sCommand[2])
     
    171268                            GlobalVariables.ServerMessage += "setting threshold of channel: "+str(channel)+ " in module "+str(module)+" to: "+str(thresh)+"\n"
    172269                            GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel,  thresh))+"\n"
     270                            GlobalVariables.TriggerThresholds[channel] = thresh
    173271                        time.sleep(0.05)
    174272                           
     
    176274                    except:
    177275                        print "Syntax error (SetAllThresh) "
     276
     277                elif(V812Command=="SetAllThreshDiff"):
     278                    print "trying to set all the threshold (diff):"
     279                    try:
     280                        module      = int(sCommand[2])
     281                        threshdiff  = int(sCommand[3])
     282                        GlobalVariables.ServerMessage="Setting all thresholds (diff)...\n"
     283                        for channel in range(0,16):
     284                            GlobalVariables.ServerMessage += "increase threshold of channel: "+str(channel)+ " in module "+str(module)+" by: "+str(threshdiff)+"\n"
     285                            GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel, GlobalVariables.TriggerThresholds[channel]+ threshdiff))+"\n"
     286                            GlobalVariables.TriggerThresholds[channel]+= threshdiff
     287                        time.sleep(0.05)
     288                           
     289                           
     290                    except:
     291                        print "Syntax error (SetAllThreshDiff) "
     292
     293                elif(V812Command=="SetThreshDiff"):
     294                    GlobalVariables.ServerMessage = "trying to set the threshold:"
     295                    time.sleep(0.05)
     296                    try:
     297                        module      = int(sCommand[2])
     298                        channel     = int(sCommand[3])
     299                        threshdiff  = int(sCommand[4])
     300                        #print module, channel, threshdiff
     301                        #print GlobalVariables.TriggerThresholds[channel]
     302                        newthresh = GlobalVariables.TriggerThresholds[channel] + threshdiff
     303                        #print newthresh
     304                        GlobalVariables.ServerMessage = "increase threshold of channel: "+str(channel)+ " in module "+str(module)+" by: "+str(threshdiff)+"\n"
     305                        GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel,newthresh))
     306                        GlobalVariables.TriggerThresholds[channel] = newthresh
     307                        time.sleep(0.05)
     308                    except:
     309                        GlobalVariables.ServerMessage = "Syntax error (SetThreshDiff) "
     310                        time.sleep(0.05)
     311           
    178312                elif(V812Command=="SetThresh"):
    179313                    GlobalVariables.ServerMessage = "trying to set the threshold:"
     
    185319                        GlobalVariables.ServerMessage = "setting threshold of channel: "+str(channel)+ " in module "+str(module)+" to: "+str(thresh)
    186320                        GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel,  thresh))
     321                        GlobalVariables.TriggerThresholds[channel] = thresh
    187322                        time.sleep(0.05)
    188323                    except:
     
    197332                        GlobalVariables.ServerMessage = "setting maj. level of module "+str(module)+" to: "+str(level)
    198333                        GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Majority_Level(module,level))
     334                        GlobalVariables.MajorityLevel = level
    199335                        time.sleep(0.05)
    200336                    except:
     
    216352
    217353                elif(V812Command=="SetDeadTime"):
    218                     GlobalVariables.ServerMessage = "trying to set the majority threshold:"
     354                    GlobalVariables.ServerMessage = "trying to set deadtime threshold:"
    219355                    time.sleep(0.05)
    220356                    try:
     
    226362                        time.sleep(0.05)
    227363                    except:
    228                         GlobalVariables.ServerMessage = "Syntax error (SetMajThresh)"
    229                         time.sleep(0.05)
    230 
     364                        GlobalVariables.ServerMessage = "Syntax error (SetDeadTime)"
     365                        time.sleep(0.05)
     366                elif(V812Command=="SetOutputWidth"):
     367                    GlobalVariables.ServerMessage = "trying to set output width "
     368                    time.sleep(0.05)
     369                    try:
     370                        module  = int(sCommand[2])
     371                        block  = int(sCommand[3])
     372                        outputw= int(sCommand[4])
     373                        GlobalVariables.ServerMessage = "setting output width of module "+str(module)+" block: "+str(block)+ " to: "+str(outputw)
     374                        GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Output_Width(module, block, outputw))
     375                        time.sleep(0.05)
     376                    except:
     377                        GlobalVariables.ServerMessage = "Syntax error (SetOutputTime)"
     378                        time.sleep(0.05)
     379                elif(V812Command=="Status"):
     380                    GetStatus();
     381
     382                elif(V812Command=="RateScan"):
     383                    GlobalVariables.ServerMessage = "trying to scan the rates "
     384                    time.sleep(0.05)
     385                    try:
     386                        module = int(sCommand[2])
     387                        start  = int(sCommand[3])
     388                        step   = int(sCommand[4])
     389                        stop   = int(sCommand[5])
     390                        print module, start, step, stop
     391                        self.RateScan(module, start, step, stop)
     392
     393
     394                    except:
     395                        GlobalVariables.ServerMessage = "Syntax error (RateScan)"
     396                        time.sleep(0.05)
     397                       
     398
     399
     400                   
    231401                else:
    232402                    GlobalVariables.ServerMessage = "Syntax Error (V812)"
    233403                    time.sleep(0.05)
     404               
    234405                       
    235406        except:
    236407            GlobalVariables.ServerMessage = "syntax error"
    237408            time.sleep(0.05)
    238         #_VME.V812_Set_Threshold(1, 0,  255)
     409    def RateScan(self,module, start, step, stop):
     410        #print module, start, step,stop
     411        #print "IN\n"
     412        #First stop the automatic rate writeup
     413        oldinterval = self.writeOutput.interval
     414        self.writeOutput.interval=15
     415        rates      =  ["rates",
     416                   0,0,0,0,
     417                   0,0,0,0,
     418                   0,0,0,0,
     419                   0,0,0,0]
     420        print "rates:" ,rates
     421        CurrentThresholds =[0,0,0,0,
     422                   0,0,0,0,
     423                   0,0,0,0,
     424                   0,0,0,0]
     425        for pix in range(0,16):
     426            CurrentThresholds[pix] = GlobalVariables.TriggerThresholds[pix]
     427        print "Threshs: ",CurrentThresholds,"\n"
     428        NumberOfScanSteps = int((stop-start)/step) + 1
     429        print "NumberOfScanSteps: ",  NumberOfScanSteps
     430        for StepNumber in range(0,NumberOfScanSteps):
     431            print "Step#" ,StepNumber
     432            for pix in range(0,16):
     433                #print (CurrentThresholds[pix] + start) + step*StepNumber
     434                GlobalVariables.TriggerThresholds[pix] = (CurrentThresholds[pix] + start) + step*StepNumber
     435                #print pix, GlobalVariables.TriggerThresholds[pix]
     436                _VME.V812_Set_Threshold(module, pix,   GlobalVariables.TriggerThresholds[pix])
     437
     438            time.sleep(2.1)
     439            #print "Getting Rates: \n"
     440            rates = self.gimmeRates.rates#GetRates(module);
     441            self.writeOutput.writeToFile()
     442            #print "Got rates..."
     443            print  "Thresh: ",GlobalVariables.TriggerThresholds, " rates: ",rates    #_VME.V812_Set_Threshold(1, 0,  255)
     444
     445
     446        self.writeOutput.interval = oldinterval
    239447   
    240448    def __init__(self):
     449        #self.writeOutput.OpenNewFile("test.txt")
    241450        threading.Thread.__init__(self)
    242451    def run(self):
    243452        self.gimmeRates.start()
     453       
     454        self.writeOutput.start()
    244455        oldcommand=GlobalVariables.UserInput
    245456        while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"):
    246457            CommandToParse=GlobalVariables.UserInput
     458            #Filename to write:
     459            #year = datetime.date.today().year
     460            #month = datetime.date.today().month
     461            #day = datetime.date.today().day
     462            MyTime = time.time()
     463            #print MyTime
     464       
     465           
     466            hour = time.gmtime(MyTime)[3]
     467            minute = time.gmtime(MyTime)[4]
     468            #print hour, minute
     469            if(hour>=13):
     470                MyTime+=12*60*60
     471
     472            year = time.gmtime(MyTime)[0]
     473            month =time.gmtime(MyTime)[1]
     474            day = time.gmtime(MyTime)[2]
     475            date=str(year)
     476            #if (hour>=12):
     477            #    day = day+1
     478           
     479            date+="%(month)02d" % {"month":month}
     480            date+="%(day)02d" % {"day":day}
     481            #date+="_"
     482            #date+="%(hour)02d" % {"hour":hour}
     483            #date+="%(minute)02d" % {"minute":minute}
     484            self.writeOutput.filename="/ct3data/SlowData/Trigger_"+date+".slow"
     485
     486           
     487            #self.writeOutput.filename = "/ct3data/SlowData/Trigger_"
     488            #self.writeOutput.filename
    247489            if(oldcommand!=CommandToParse):
    248490                self.Parse(CommandToParse)
Note: See TracChangeset for help on using the changeset viewer.