Copy data after MultiPeak Fit

Hello,

I am trying to incorporate the MultiPeak fitter into my procedure for batch fitting. As part of this, I am having the user first perform the MultiPeak fit routine on a single trace so the batch fitting knows how many peaks to use (following DisplayHelpTopic "MPF2_DoMPFit()").

What I want, is to use PauseforUser to wait for the MultiPeak2Panel panel to close (user is done) and then copy the results (full datafolder) into a new folder. I have my data for each sample in it's own folder and I want to copy the multipeak fit results here.

Unfortunately, it seems that when you use PauseforUser, the right click menu used to add peaks for fitting is disabled preventing the use of the MultiPeak fitter. If I omit this line, the datafolders are copied before the fitting is performed.

        string DF                                                               //string containing path to sample datafolder
        multipeakfit2_initialize()                      //run initialize to make necessary folders
    dowindow/k MultiPeak2StarterPanel           //Destroy wave selection panel (we just needed the initialization for folder creation)
   
    NVAR currentset = root:Packages:MultiPeakFit2:CurrentSetNumber      
    currentset=0
   
    dowindow/k/z MultiPeakFit_Set1
    killdatafolder/z root:Packages:MultiPeakFit2:MPF_SetFolder_1        //kill existing folder & waves if needed
   
    MPF2_StartNewMPFit(0, "New Graph", max_wave, "energy", 1, 1)

    pauseforuser MultipeakFit_Set1#MultiPeak2Panel      //wait for MultiPeak panel to close
   
    DF+="MPF"                                           //duplicate MultiPeak results into Datafolder for storage/reference
    killdatafolder/z $DF
    duplicatedatafolder root:Packages:MultiPeakFit2:MPF_SetFolder_1, $(DF)


Any advice?
Maybe, instead of the pause for user, start a background task to monitor the existance of the MultiPeakFit panel. Then let the current function end w/o attempting to copy results. When the background task sees that the panel has closed, it can call a function to harvest the data that you need.
Thanks for the advice, I will try a background task.

If I understand correctly, every time interval (say 30 seconds) I will check to see if the multifit panel exists. If it does, I then call a function which 1) stops the background task and 2) copies the data I want.

Or, can these tasks be accomplished within an if-else statement within the background task itself? Can a background task stop itself?
proland wrote:
Or, can these tasks be accomplished within an if-else statement within the background task itself?


Yes, you should be able to do the wave/folder manipulations within the background task.

proland wrote:
Can a background task stop itself?


The background task cannot directly kill itself, but if you return the correct value, Igor will handle this for you.

For more information: displayhelptopic "Background Tasks"
How about this as an alternative to using a background task... in the function where you start multipeakfit2, set a hook function to be called when the MPF panel is killed. The hook function can handle your file manipulation when the window is killed. I suppose the hook function could respond to other events if necessary or useful.

This would seem to be relatively immune to changes that WM might make to the procedure (short of changing the name of the startup function).

Here's an example:
Function HandleMPF()
//start multipeakfit
    fStartMultipeakFit2()  
//set a hook function to handle killing the multipeakfit panle
    SetWindow MultiPeak2StarterPanel, hook(MoveFilesMPF)= MPF_Killed
End

Function MPF_Killed(s)  //hook function, handles file manipulation when MPF window is killed
    STRUCT WMWinHookStruct &s
   
    switch (s.eventCode)
        case 2: //"kill"
            print "killed"
            //do file manipulations
            break
    endswitch
   
    return 0
End
Setting a hook on the fit panel is an interesting idea...

But the hook needs to be on the MultiPeak2Panel rather than the MultiPeak2StarterPanel. And that requires knowing the name of the graph of interest. But since the OP is using PauseForUser with the relevant panel, I guess that problem is solved. So instead of
SetWindow MultiPeak2StarterPanel, hook(MoveFilesMPF)
use
SetWindow MultipeakFit_Set1#MultiPeak2Panel, hook(MoveFilesMPF)
This has the drawback that it will only work using a fresh experiment that hasn't been used with Multipeak Fit 2 before, since it assumes MultipeakFit_Set1. If that's under your control, then it should be OK.

This technique also assumes that the user will close the panel after the fit.

This will fail if sometime in the future I change the name of the panel, but I don't think that's too likely. But take warning- I consider the panel name to be an implementation detail, and not necessarily something I must preserve for backward compatibility.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
I decided to go with the background task checking for the panels existence. The user instructions for my code specify to close the panel to proceed.

I got around the panel naming by changing the global variable currentsetnumber to zero prior to running MPF2 routines. This essentially overwrites MPF_SetFolder_1 every time keeping the graph/panel names consistent. I know that this impedes the proper operation of the multi-peak fit routine but it was the only way I could see to do it. The subsequent batch fitting routine fits a large set of curves, including the one used to create MPF_SetFolder_1 so I have no concerns over loosing this data.

In order to allow users normal use of the Multi-peak fitter, I suppose i should check for the number of folders that start with "MPF_SetFolder*" and reset currentsetnumber back to an appropriate value after I am done. My code will continuously overwrite MPF_SetFolder_1 but every other MPF folder should remain untouched.
You could use the global variable root:Packages:MultiPeakFit2:currentSetNumber. You can probably guess what it means :)

Either set it to a very large number to guarantee that the user will never use that many data sets, or you could simply use the next set number available.

Again, the warning- this is an implementation detail, and the variable is my private package data folder, so I consider it to be something I can change without affecting backward compatibility. But again, I doubt it will change in the near future.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com