def write_measurement(measurement,
                      root_file=None,
                      xml_path=None,
                      output_path=None,
                      output_suffix=None,
                      write_workspaces=False,
                      apply_xml_patches=True,
                      silence=False):
    """
    Write a measurement and RooWorkspaces for all contained channels
    into a ROOT file and write the XML files into a directory.

    Parameters
    ----------

    measurement : HistFactory::Measurement
        An asrootpy'd ``HistFactory::Measurement`` object

    root_file : ROOT TFile or string, optional (default=None)
        A ROOT file or string file name. The measurement and workspaces
        will be written to this file. If ``root_file is None`` then a
        new file will be created with the same name as the measurement and
        with the prefix ``ws_``.

    xml_path : string, optional (default=None)
        A directory path to write the XML into. If None, a new directory with
        the same name as the measurement and with the prefix ``xml_`` will be
        created.

    output_path : string, optional (default=None)
        If ``root_file is None``, create the ROOT file under this path.
        If ``xml_path is None``, create the XML directory under this path.

    output_suffix : string, optional (default=None)
        If ``root_file is None`` then a new file is created with the same name
        as the measurement and with the prefix ``ws_``. ``output_suffix`` will
        append a suffix to this file name (before the .root extension).
        If ``xml_path is None``, then a new directory is created with the
        same name as the measurement and with the prefix ``xml_``.
        ``output_suffix`` will append a suffix to this directory name.

    write_workspaces : bool, optional (default=False)
        If True then also write a RooWorkspace for each channel and for all
        channels combined.

    apply_xml_patches : bool, optional (default=True)
        Apply fixes on the output of ``Measurement::PrintXML()`` to avoid known
        HistFactory bugs. Some of the patches assume that the ROOT file
        containing the histograms will exist one directory level up from the
        XML and that hist2workspace, or any tool that later reads the XML will
        run from that same directory containing the ROOT file.

    silence : bool, optional (default=False)
        If True then capture and silence all stdout/stderr output from
        HistFactory.

    """
    context = silence_sout_serr if silence else do_nothing

    output_name = measurement.name
    if output_suffix is not None:
        output_name += '_{0}'.format(output_suffix)
    output_name = output_name.replace(' ', '_')

    if xml_path is None:
        xml_path = 'xml_{0}'.format(output_name)
        if output_path is not None:
            xml_path = os.path.join(output_path, xml_path)

    if not os.path.exists(xml_path):
        mkdir_p(xml_path)

    if root_file is None:
        root_file = 'ws_{0}.root'.format(output_name)
        if output_path is not None:
            root_file = os.path.join(output_path, root_file)

    own_file = False
    if isinstance(root_file, string_types):
        root_file = root_open(root_file, 'recreate')
        own_file = True

    with preserve_current_directory():
        root_file.cd()

        log.info("writing histograms and measurement in {0} ...".format(
            root_file.GetName()))
        with context():
            measurement.writeToFile(root_file)
        # get modified measurement
        out_m = root_file.Get(measurement.name)
        log.info("writing XML in {0} ...".format(xml_path))
        with context():
            out_m.PrintXML(xml_path)

        if write_workspaces:
            log.info("writing combined model in {0} ...".format(
                root_file.GetName()))
            workspace = make_workspace(measurement, silence=silence)
            workspace.Write()
            for channel in measurement.channels:
                log.info("writing model for channel `{0}` in {1} ...".format(
                    channel.name, root_file.GetName()))
                workspace = make_workspace(
                    measurement, channel=channel, silence=silence)
                workspace.Write()

    if apply_xml_patches:
        # patch the output XML to avoid HistFactory bugs
        patch_xml(glob(os.path.join(xml_path, '*.xml')),
                  root_file=os.path.basename(root_file.GetName()))

    if own_file:
        root_file.Close()
Write a measurement and RooWorkspaces for all contained channels
    into a ROOT file and write the XML files into a directory.

    Parameters
    ----------

    measurement : HistFactory::Measurement
        An asrootpy'd ``HistFactory::Measurement`` object

    root_file : ROOT TFile or string, optional (default=None)
        A ROOT file or string file name. The measurement and workspaces
        will be written to this file. If ``root_file is None`` then a
        new file will be created with the same name as the measurement and
        with the prefix ``ws_``.

    xml_path : string, optional (default=None)
        A directory path to write the XML into. If None, a new directory with
        the same name as the measurement and with the prefix ``xml_`` will be
        created.

    output_path : string, optional (default=None)
        If ``root_file is None``, create the ROOT file under this path.
        If ``xml_path is None``, create the XML directory under this path.

    output_suffix : string, optional (default=None)
        If ``root_file is None`` then a new file is created with the same name
        as the measurement and with the prefix ``ws_``. ``output_suffix`` will
        append a suffix to this file name (before the .root extension).
        If ``xml_path is None``, then a new directory is created with the
        same name as the measurement and with the prefix ``xml_``.
        ``output_suffix`` will append a suffix to this directory name.

    write_workspaces : bool, optional (default=False)
        If True then also write a RooWorkspace for each channel and for all
        channels combined.

    apply_xml_patches : bool, optional (default=True)
        Apply fixes on the output of ``Measurement::PrintXML()`` to avoid known
        HistFactory bugs. Some of the patches assume that the ROOT file
        containing the histograms will exist one directory level up from the
        XML and that hist2workspace, or any tool that later reads the XML will
        run from that same directory containing the ROOT file.

    silence : bool, optional (default=False)
        If True then capture and silence all stdout/stderr output from
        HistFactory.