<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def after_retract(reference_analysis):
 """Function triggered after a 'retract' transition for the reference analysis passed in is performed. The reference analysis transitions to "retracted" state and a new copy of the reference analysis is created """

    reference = reference_analysis.getSample()
    service = reference_analysis.getAnalysisService()
    worksheet = reference_analysis.getWorksheet()
    instrument = reference_analysis.getInstrument()
    if worksheet:
        # This a reference analysis in a worksheet
        slot = worksheet.get_slot_position_for(reference_analysis)
        refgid = reference_analysis.getReferenceAnalysesGroupID()
        ref = worksheet.add_reference_analysis(reference, service, slot, refgid)
        if not ref:
            logger.warn("Cannot add a retest for reference analysis {} into {}"
                        .format(reference_analysis.getId(), worksheet.getId()))
            return

        ref.setRetestOf(reference_analysis)
        ref.setResult(reference_analysis.getResult())
        if instrument:
            ref.setInstrument(instrument)
            instrument.reindexObject()

        # Try to rollback the worksheet to prevent inconsistencies
        wf.doActionFor(worksheet, "rollback_to_open")

    elif instrument:
        # This is an internal calibration test
        instrument.addReferences(reference, [api.get_uid(service)])
        instrument.reindexObject()